Repository navigation
fix(agent): treat malformed ReAct action payloads as text instead of crashing - #45228
Open
TINGyu123644 wants to merge 1 commit into
Open
TINGyu123644 wants to merge 1 commit into
TINGyu123644 wants to merge 1 commit into
Conversation
…crashing
CotAgentOutputParser.parse_action assumed the model's JSON payload is
always a single action-shaped object. Four shapes violated that and each
aborted the run with an uncaught exception (the error is not caught in
CotAgentRunner.run):
- a JSON list with more than one item (e.g. a direct answer that happens
to be JSON data) or a non-object list ([] / [1]) raised
AttributeError: 'list'/'int' object has no attribute 'items';
- a non-string action name ({"action": ["search"], ...}) raised a pydantic
ValidationError from AgentScratchpadUnit.Action, as did a non-dict/str
action_input;
- an extra field after the action ({"action": "search", ..., "reason":
"need data"}) silently changed the tool: the last non-input key won, so
the agent called a tool named "need data".
parse_action now:
- runs the first action-shaped item of a list (the prompt allows exactly
one action per turn) and keeps a list without one as text;
- keeps a payload as text when the action name is not a string or the
action input is not a dict/str, instead of raising;
- takes the first non-input key as the action name so trailing extra keys
cannot override the tool;
- serializes non-action payloads with ensure_ascii=False so non-ASCII
text is not escaped to \uXXXX.
Adds regression tests for all of the above (verified RED without the
fix); the existing 65 parser tests still pass.
Fixes langgenius#45200.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #45200.
CotAgentOutputParser.parse_actionassumed the model's JSON payload is always a single action-shaped object. Four shapes violate that, and each one aborts the agent run with an uncaught exception (the error is not caught inCotAgentRunner.run):[]/[1]) raisedAttributeError: 'list'/'int' object has no attribute 'items';{"action": ["search"], ...}) raised a pydanticValidationErrorfromAgentScratchpadUnit.Action- as did a non-dict/straction_input([1, 2],5);{"action": "search", "action_input": {...}, "reason": "need data"}) silently changed the tool: the last non-input key won, so the agent looked for a tool namedneed data.parse_actionnow:ensure_ascii=False, so non-ASCII text is no longer escaped to backslash-u sequences.No change to the happy path: a single action-shaped object (or cohere's single-item list) still produces the same
AgentScratchpadUnit.Action.Screenshots
AttributeError/ValidationError, or calls the wrong toolChecklist
api/.ruff.toml, line-length 120) plusruff format --check: clean. (The twounsorted-importsfindings ruff 0.17.1 reports on these files also reproduce on the untouched upstream versions - a ruff-version isort difference, not from this change.) I could not run the fullmake lint/make type-checkin my environment (the full backend toolchain isn't available); the added tests are self-contained and need no services.Tests: 9 new cases in
api/tests/unit_tests/core/agent/output_parser/test_cot_output_parser.pycovering every shape above (RED verified without the fix - the crash cases raise, the wrong-tool case asserts the correct tool, the escape case asserts literal non-ASCII). The pre-existing 65 tests in that file still pass.From WorkBuddy