Skip to content

fix: sanitize malformed tool call arguments to prevent vLLM 400 errors - #13092

Open
thanhnnict wants to merge 1 commit into
continuedev:mainfrom
thanhnnict:fix/sanitize-tool-arguments
Open

fix: sanitize malformed tool call arguments to prevent vLLM 400 errors#13092
thanhnnict wants to merge 1 commit into
continuedev:mainfrom
thanhnnict:fix/sanitize-tool-arguments

Conversation

@thanhnnict

Copy link
Copy Markdown

Problem

When LLMs generate malformed JSON in tool_calls.function.arguments (e.g., missing commas, truncated due to max_tokens), Continue stores this in conversation history and sends it back to the server on subsequent turns. vLLM validates JSON in tool_calls arguments during request preprocessing and rejects malformed input with a 400 error, breaking the conversation.

Solution

Validate JSON before sending tool_calls arguments back to server. If invalid, wrap in a valid JSON envelope ({"_raw": original}) to prevent server rejection while preserving the original content.

Behavior

Input arguments Valid JSON? Output
{"path": "/tmp/x"} Pass-through unchanged
{"path": "/tmp/x" "content": "hi"} {"_raw": "..."}
"" (empty) "{}" (fallback)
null / undefined "{}" (fallback)

Root Causes of Malformed JSON

  1. max_tokens truncation — cuts off mid-JSON
  2. Reasoning token leak — thinking tokens leak into arguments field
  3. Model hallucination — LLM occasionally produces invalid syntax
  4. Encoding issues — special characters not properly escaped

Trade-offs

  • Performance: JSON.parse() on every tool_call argument — negligible cost (microseconds)
  • Semantics: Wrapped {"_raw": "..."} may confuse model, but strictly better than 400 error
  • No data loss: Original malformed string preserved in _raw field

Related

When LLMs generate malformed JSON in tool_calls.function.arguments
(e.g., missing commas, truncated due to max_tokens), Continue stores
this in conversation history and sends it back to the server on
subsequent turns. vLLM validates JSON in tool_calls arguments during
request preprocessing and rejects malformed input with a 400 error.

Fix: validate JSON before sending tool_calls arguments back to server.
If invalid, wrap in a valid JSON envelope ({ _raw: original }) to
prevent server rejection while preserving the original content.

Root causes of malformed JSON:
1. max_tokens truncation cutting off mid-JSON
2. Reasoning token leak into arguments field
3. Model hallucination producing invalid syntax
4. Special characters not properly escaped

Related: vllm-project/vllm#43995
@thanhnnict
thanhnnict requested a review from a team as a code owner August 5, 2026 15:40
@thanhnnict
thanhnnict requested review from sestinj and removed request for a team August 5, 2026 15:40
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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