Skip to content

fix(agent): preserve malformed tool-call arguments - #2306

Open
primorLee wants to merge 1 commit into
THUDM:mainfrom
primorLee:fix/2299-preserve-malformed-tool-args
Open

fix(agent): preserve malformed tool-call arguments#2306
primorLee wants to merge 1 commit into
THUDM:mainfrom
primorLee:fix/2299-preserve-malformed-tool-args

Conversation

@primorLee

Copy link
Copy Markdown

Summary

  • preserve the model-produced tool argument string alongside its parsed mapping
  • return the original string unchanged as OpenAI function.arguments
  • keep the parsed mapping for chat-template replay and trajectory matching
  • fail closed in the Anthropic adapter when arguments cannot be represented as an input object
  • mark valid non-object JSON arguments as ill-formed instead of emitting invalid tool inputs

Problem

When SGLang recognizes a tool call but its arguments are malformed JSON, the parser currently replaces the argument text with a {"_raw_arguments": ...} sentinel mapping.

The OpenAI adapter then serializes that sentinel, so clients receive a different argument string from the one produced by the model. The Anthropic adapter emits the sentinel as a normal executable tool_use.input, even though Anthropic requires an input object and the model output was not valid.

For the model output:

{"cmd": "pytest"

current main returns:

OpenAI: {"_raw_arguments": "{\"cmd\": \"pytest\""}
Anthropic: tool_use(input={"_raw_arguments": ...})

This PR keeps two representations at the parsing boundary: the original wire text and the canonical mapping used for history rendering. OpenAI receives the former unchanged. Anthropic only exposes calls whose arguments are representable as an object; an invalid call becomes a normal non-tool response and the turn remains marked ill_formed.

Valid object-valued tool calls and XML fallback calls keep their existing behavior.

Validation

  • full CPU agent suite: 70 passed, 1 skipped
  • changed-file pre-commit: case-conflict, private-key, large-file, Ruff, autoflake, isort, and Black hooks passed
  • Python compilation and git diff --check passed
  • focused coverage includes malformed JSON, valid raw formatting, non-object JSON, OpenAI wire preservation, and Anthropic fail-closed behavior

Closes #2299

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.

[Bug] Agent adapters rewrite malformed tool-call arguments before the client sees them

1 participant