Nihar Shah commited on
Commit
8577b4b
·
1 Parent(s): 86241b5

Fix: Support both Pydantic models and dicts in step() and reward calculation

Browse files

- Updated env.py step() to handle both dict and Action Pydantic models
- Updated reward.py calculate() to handle both dict and Pydantic models
- All 120 tests pass
- API endpoints validated and working correctly
- Supports flexible action format: dict or Action model

Files changed (2) hide show
  1. app/env.py +9 -4
  2. app/reward.py +15 -4
app/env.py CHANGED
@@ -104,7 +104,7 @@ class CRMQueryEnv:
104
  Execute action in environment.
105
 
106
  Args:
107
- action: Action dict with 'tool' and 'arguments' keys
108
 
109
  Returns:
110
  (observation, reward, done, info)
@@ -118,9 +118,14 @@ class CRMQueryEnv:
118
  self.step_count += 1
119
  task = get_task_by_id(self.current_task_id)
120
 
121
- # Validate action
122
- tool = action.get("tool", "")
123
- arguments = action.get("arguments", {})
 
 
 
 
 
124
 
125
  # Execute action
126
  action_result: Dict[str, Any] = {}
 
104
  Execute action in environment.
105
 
106
  Args:
107
+ action: Action dict with 'tool' and 'arguments' keys, or Action Pydantic model
108
 
109
  Returns:
110
  (observation, reward, done, info)
 
118
  self.step_count += 1
119
  task = get_task_by_id(self.current_task_id)
120
 
121
+ # Validate action - handle both dict and Pydantic models
122
+ if isinstance(action, dict):
123
+ tool = action.get("tool", "")
124
+ arguments = action.get("arguments", {})
125
+ else:
126
+ # Pydantic model
127
+ tool = action.tool
128
+ arguments = action.arguments
129
 
130
  # Execute action
131
  action_result: Dict[str, Any] = {}
app/reward.py CHANGED
@@ -34,7 +34,7 @@ class RewardCalculator:
34
  Calculate dense reward with shape.
35
 
36
  Args:
37
- action: The action taken
38
  action_result: Result of the action
39
  done: Whether episode ended
40
  task_ground_truth: Ground truth answer
@@ -49,8 +49,14 @@ class RewardCalculator:
49
  components: Dict[str, float] = {}
50
  base_reward = 0.0
51
 
52
- tool = action.get("tool", "")
53
- arguments = action.get("arguments", {})
 
 
 
 
 
 
54
 
55
  # Schema validation reward
56
  if tool in ["search_customers", "search_orders", "search_tickets", "submit_answer"]:
@@ -103,7 +109,12 @@ class RewardCalculator:
103
 
104
  # Submit answer reward
105
  if tool == "submit_answer":
106
- submitted = action.get("arguments", {}).get("customer_ids", [])
 
 
 
 
 
107
  ground_truth = task_ground_truth.get("customer_ids", [])
108
 
109
  if isinstance(submitted, list) and isinstance(ground_truth, list):
 
34
  Calculate dense reward with shape.
35
 
36
  Args:
37
+ action: The action taken (dict or Pydantic model)
38
  action_result: Result of the action
39
  done: Whether episode ended
40
  task_ground_truth: Ground truth answer
 
49
  components: Dict[str, float] = {}
50
  base_reward = 0.0
51
 
52
+ # Handle both dict and Pydantic model
53
+ if isinstance(action, dict):
54
+ tool = action.get("tool", "")
55
+ arguments = action.get("arguments", {})
56
+ else:
57
+ # Pydantic model
58
+ tool = action.tool
59
+ arguments = action.arguments
60
 
61
  # Schema validation reward
62
  if tool in ["search_customers", "search_orders", "search_tickets", "submit_answer"]:
 
109
 
110
  # Submit answer reward
111
  if tool == "submit_answer":
112
+ # Handle both dict and Pydantic model
113
+ if isinstance(action, dict):
114
+ submitted = action.get("arguments", {}).get("customer_ids", [])
115
+ else:
116
+ submitted = action.arguments.get("customer_ids", [])
117
+
118
  ground_truth = task_ground_truth.get("customer_ids", [])
119
 
120
  if isinstance(submitted, list) and isinstance(ground_truth, list):