fix: handle JSON parsing errors in planner agent
Add try-except for json.JSONDecodeError in parse_agent_tasks() to gracefully handle malformed JSON from LLM responses (e.g. Gemini 2.5 Flash via OpenRouter). Returns empty list on parse failure, which triggers the existing retry mechanism in make_plan(). Add test suite for parse_agent_tasks() covering valid JSON, malformed JSON, truncated JSON, and invalid agent name cases.
This commit is contained in:
@@ -76,7 +76,12 @@ class PlannerAgent(Agent):
|
||||
if blocks == None:
|
||||
return []
|
||||
for block in blocks:
|
||||
line_json = json.loads(block)
|
||||
try:
|
||||
line_json = json.loads(block)
|
||||
except json.JSONDecodeError as e:
|
||||
self.logger.warning(f"Failed to parse JSON block: {e}")
|
||||
pretty_print(f"JSON parsing error: {e}", color="warning")
|
||||
return []
|
||||
if 'plan' in line_json:
|
||||
for task in line_json['plan']:
|
||||
if task['agent'].lower() not in [ag_name.lower() for ag_name in self.agents.keys()]:
|
||||
|
||||
Reference in New Issue
Block a user