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:
Br1an67
2026-03-02 00:03:36 +08:00
parent 9611bf4081
commit f350378170
2 changed files with 114 additions and 1 deletions
+6 -1
View File
@@ -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()]: