Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Send JSON parsing exceptions to LLM (#1342)

Engel Nyst2318ceae352b
diff --git a/agenthub/monologue_agent/utils/prompts.py b/agenthub/monologue_agent/utils/prompts.py
index 688e139f9a..25f503ba59 100644
--- a/agenthub/monologue_agent/utils/prompts.py
+++ b/agenthub/monologue_agent/utils/prompts.py
@@ -172,10 +172,14 @@ def parse_action_response(response: str) -> Action:
return len(match[2]) if match[1] == 'think' else 130 # Crudely rank multiple responses by length
try:
action_dict = json.loads(max(response_json_matches, key=rank)[0]) # Use the highest ranked response
- except ValueError as e:
+ except (ValueError, JSONDecodeError):
raise LLMOutputError(
- "Output from the LLM isn't properly formatted. The model may be misconfigured."
- ) from e
+ 'Invalid JSON, the response must be well-formed JSON as specified in the prompt.'
+ )
+ except ValueError:
+ raise LLMOutputError(
+ 'Invalid JSON, the response must be well-formed JSON as specified in the prompt.'
+ )
if 'content' in action_dict:
# The LLM gets confused here. Might as well be robust
action_dict['contents'] = action_dict.pop('content')
diff --git a/opendevin/controller/agent_controller.py b/opendevin/controller/agent_controller.py
index fc5265fa7c..ac2f799e3c 100644
--- a/opendevin/controller/agent_controller.py
+++ b/opendevin/controller/agent_controller.py
@@ -16,7 +16,7 @@ from opendevin.observation import (
NullObservation,
)
from opendevin.agent import Agent
-from opendevin.exceptions import AgentMalformedActionError, AgentNoActionError, MaxCharsExceedError
+from opendevin.exceptions import AgentMalformedActionError, AgentNoActionError, MaxCharsExceedError, LLMOutputError
from opendevin.logger import opendevin_logger as logger
from opendevin.plan import Plan
from opendevin.state import State
@@ -211,7 +211,7 @@ class AgentController:
action = self.agent.step(self.state)
if action is None:
raise AgentNoActionError('No action was returned')
- except (AgentMalformedActionError, AgentNoActionError) as e:
+ except (AgentMalformedActionError, AgentNoActionError, LLMOutputError) as e:
observation = AgentErrorObservation(str(e))
logger.info(action, extra={'msg_type': 'ACTION'})