Skip to content

fix: return undecodable tool call arguments to the model instead of failing the turn - #438

Merged
frostming merged 4 commits into
mainfrom
fix/malformed-tool-call-arguments
Dec 2, 2026
Merged

frostming merged 4 commits into
mainfrom
fix/malformed-tool-call-arguments

Conversation

@frostming

@frostming frostming commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

A subagent run ended with [invalid_input] Expected a function tool call with JSON object arguments after 24 healthy steps. The model had spent almost all of its max_tokens on reasoning (16,033 of 16,384 output tokens on the previous step), so the next tool call's arguments were cut off mid-JSON. Bub treated that as fatal and ended the turn, and recorded nothing about the failing step.

  • Undecodable arguments are a failed tool result, not a fatal error. Such a call resolves to a placeholder tool that raises, the same way an unknown tool name does, so ToolExecutor turns the error into the call's result. The real tool does not run; the model gets an invalid_input result and can retry.
  • Truncation is named. When the response's finish reason is length, the error on its last call says the arguments were cut off at the output token limit and asks for a shorter call. This includes arguments cut off before their first character, which arrive as {} and previously would have run the tool.
  • The call is recorded with {} arguments. The raw string would break replay on providers that parse arguments when building a request. The original text (first and last 500 characters when longer than 1,000) and its length are kept in the error details.
  • A step that fails after the model answered is recorded. If resolving or executing tool calls raises, ChatFinished is emitted with the error, the new messages, the calls, and usage, before the exception propagates. Each call's result says its outcome is unknown, because the step failed before results were collected. An error raised while recording is logged and does not replace the step's own error.
  • finish_reason is recorded. ChatFinished has a new finish_reason field, and the tape's run event stores it.

Behavior to review

  • Previously a step that failed after the model answered left nothing on the tape, so the user prompt of that step was not recorded. It now is, together with the calls. A retry of the same prompt will therefore see the earlier attempt.
  • ToolCallStarted and ToolCallFinished handlers see a call with undecodable arguments, under the tool's real name and with empty arguments. An approval handler may therefore ask about a call that will fail anyway.

Known gaps, not addressed here

  • An LLMCallFinished handler that raises after a complete response still leaves the step unrecorded.
  • Replayed failed tool results are not marked as errors for the Anthropic and Gemini formats (is_error stays false). This is existing replay behavior and also applies to the new error results.

Impacted modules

  • src/bub/core/agent.py: tool call resolution and step recording
  • src/bub/core/events.py, recorder.py, tape.py: finish_reason on ChatFinished and the run event
  • website/src/content/docs/{docs,zh-cn/docs}/reference/python-api.mdx: ChatFinished reference

Verification

  • uv run pytest -q: 787 passed, 20 skipped
  • uv run ruff check, uv run ty check: clean
  • Against a real model with BUB_MAX_TOKENS=1200, on an earlier revision of this branch: a tool call was cut off, the model received the truncation error and answered normally, and the tape's run event showed finish_reason: length
  • Docs build not run

Tests: the existing test that asserted invalid arguments raise now asserts the model receives a failed result and that the replayed call carries {}. New tests cover the truncation message, empty arguments at the token limit, arguments that fail to decode with a non-JSON error, and a mixed batch where one call's handler raises after another call succeeded.

🤖 Generated with Claude Code

…ailing the turn

A tool call whose arguments are not a JSON object, usually because the
response was cut off at max_tokens, raised and ended the whole turn. It is
now a failed tool result the model can correct, saying so when the response
stopped at the output token limit. The call is recorded with `{}` arguments
so the tape replays on any provider.

A step that fails after the model answered is now recorded with its usage,
and ChatFinished and the tape's run event carry the finish reason.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bub 1fdcd99 Commit Preview URL

Branch Preview URL
Dec 02 2026, 11:15 AM

frostming and others added 3 commits December 2, 2026 18:14
…honest

Review follow-ups on returning undecodable tool call arguments to the model:

- Such a call no longer reaches ToolExecutor, so no ToolCallStarted handler
  is asked to approve or replace a call that cannot run.
- Arguments cut off before their first character, which arrive as `{}`,
  are treated as cut off instead of running the tool. Only the last call of
  a response that stopped at the token limit counts as cut off.
- Any ValueError or RecursionError from decoding is handled, not only
  JSONDecodeError.
- A step that fails after the model answered records each call's outcome as
  unknown instead of copying the step's error onto calls that succeeded, and
  an error from recording no longer replaces the step's own error.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Undecodable arguments resolve to a placeholder tool that raises, the same
way an unknown tool name does, so ToolExecutor turns the error into the
call's result. This replaces the agent's own path that kept such calls out
of the executor and merged their errors back into the results.

ToolCallStarted and ToolCallFinished handlers see these calls again, with
empty arguments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The agent built a raising placeholder tool by hand for both an unknown tool
name and undecodable arguments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@frostming
frostming merged commit 7a89516 into main Dec 2, 2026
7 checks passed
@frostming
frostming deleted the fix/malformed-tool-call-arguments branch December 2, 2026 11: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