Skip to content

Commit

Permalink
langchain: support the situation when action_input is null in json ou…
Browse files Browse the repository at this point in the history
…tput_parser (#29680)

Description:
This PR fixes handling of null action_input in
[langchain.agents.output_parser]. Previously, passing null to
action_input could cause OutputParserException with unclear error
message which cause LLM don't know how to modify the action. The changes
include:

Added null-check validation before processing action_input
Implemented proper fallback behavior with default values
Maintained backward compatibility with existing implementations

Error Examples:
```
{
  "action":"some action",
  "action_input":null
}
```

Issue:
None

Dependencies:
None
  • Loading branch information
J-CIC authored Feb 8, 2025
1 parent beb75b2 commit 5d581ba
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions libs/langchain/langchain/agents/output_parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
if response["action"] == "Final Answer":
return AgentFinish({"output": response["action_input"]}, text)
else:
return AgentAction(
response["action"], response.get("action_input", {}), text
)
action_input = response.get("action_input", {})
if action_input is None:
action_input = {}
return AgentAction(response["action"], action_input, text)
except Exception as e:
raise OutputParserException(f"Could not parse LLM output: {text}") from e

Expand Down

0 comments on commit 5d581ba

Please sign in to comment.