Skip to content

fix(agents): malformed tool-call JSON no longer kills the rollout#480

Open
mariobrajkovski wants to merge 1 commit into
mainfrom
fix/malformed-toolcall-args
Open

fix(agents): malformed tool-call JSON no longer kills the rollout#480
mariobrajkovski wants to merge 1 commit into
mainfrom
fix/malformed-toolcall-args

Conversation

@mariobrajkovski

@mariobrajkovski mariobrajkovski commented Jul 4, 2026

Copy link
Copy Markdown

A completion truncated at the output-token limit mid-arguments (e.g. Qwen emitting {"command": " and stopping) raised JSONDecodeError from the bare json.loads in get_response, which escaped the agent loop and errored the whole trace with "Unterminated string starting at: line 1 column 13".

Parse failures now produce the tool call with a sentinel argument (MALFORMED_TOOL_ARGS_KEY); the dispatcher returns an isError tool result for that call id without executing anything, so the provider contract holds (every call id gets a result) and the model can re-issue the call instead of the rollout dying. Applied to both the chat.completions and Responses parse sites.

Side effect: the Responses path previously crashed on valid-but-non-dict arguments; it now coerces to {}, matching the chat.completions path.


Note

Medium Risk
Changes shared tool parsing and dispatch on the core ToolAgent loop used by OpenAI agents; behavior is defensive but affects every rollout when arguments are malformed.

Overview
Truncated or invalid tool-call argument JSON (e.g. output cut mid-string at the token limit) used to raise JSONDecodeError during get_response, which aborted the whole agent rollout.

parse_tool_arguments in tool_agent now catches parse failures and attaches a sentinel key (MALFORMED_TOOL_ARGS_KEY) instead of crashing. Both chat.completions and Responses agents use this helper instead of bare json.loads. _dispatch_call detects that sentinel and returns an isError tool result for the call id without running the tool, so the provider contract is satisfied and the model can retry with complete JSON.

Non-dict JSON on the Responses path is coerced to {}, matching chat.completions. Tests cover malformed get_response parsing and dispatch error text.

Reviewed by Cursor Bugbot for commit b02daeb. Bugbot is set up for automated code reviews on this repo. Configure here.

A completion truncated at the output-token limit mid-arguments (e.g. Qwen
emitting '{"command": "' and stopping) raised JSONDecodeError from the bare
json.loads in get_response, which escaped the agent loop and errored the whole
trace with 'Unterminated string starting at: line 1 column 13'.

Parse failures now produce the tool call with a sentinel argument
(MALFORMED_TOOL_ARGS_KEY); the dispatcher returns an isError tool result for
that call id without executing anything, so the provider contract holds
(every call id gets a result) and the model can re-issue the call instead of
the rollout dying. Applied to both the chat.completions and Responses parse
sites.
@mariobrajkovski mariobrajkovski force-pushed the fix/malformed-toolcall-args branch from 73dfcf2 to b02daeb Compare July 4, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant