fix(agent): repair malformed tool_call arguments before API send#13005
Merged
Conversation
Cherry-picked from PR #12252 by @sirEven. Models like GLM-5.1 via Ollama can produce malformed tool_call arguments (truncated JSON, trailing commas, Python None). The existing except Exception: pass silently passes broken args to the API, which rejects them with HTTP 400, crashing the session. Adds a multi-stage repair pipeline at the pre-send normalization point: 1. Empty/whitespace-only → {} 2. Python None literal → {} 3. Strip trailing commas 4. Auto-close unclosed brackets 5. Remove excess closing delimiters 6. Last resort: replace with {} (logged at WARNING)
Follow-up for PR #12252 salvage: - Extract 75-line inline repair block to _repair_tool_call_arguments() module-level helper for testability and readability - Remove redundant 'import re as _re' (re already imported at line 33) - Bound the while-True excess-delimiter removal loop to 50 iterations - Add 17 tests covering all 6 repair stages - Add sirEven to AUTHOR_MAP in release.py
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
Salvage of PR #12252 by @sirEven. Replaces the silent
except Exception: passin the pre-send tool_call argument normalizer with a multi-stage JSON repair pipeline, preventing HTTP 400 session death spirals from models producing broken JSON (GLM-5.1 via Ollama, etc.).Changes
run_agent.py: New_repair_tool_call_arguments()helper — 6-stage pipeline: empty→{}, Python None→{}, strip trailing commas, auto-close unclosed brackets, remove excess closing delimiters (bounded loop), last resort{}with WARNING logrun_agent.py: Call site in pre-send normalizer replaced from 75 inline lines to 4-line callscripts/release.py: Added @sirEven to AUTHOR_MAPtests/run_agent/test_repair_tool_call_arguments.py: 17 tests covering all repair stagesFollow-up cleanup (vs raw cherry-pick)
import re as _re(re already at line 33)Validation
Closes #12252