fix: sanitize malformed tool call arguments to prevent vLLM 400 errors - #13092
Open
thanhnnict wants to merge 1 commit into
Open
fix: sanitize malformed tool call arguments to prevent vLLM 400 errors#13092thanhnnict wants to merge 1 commit into
thanhnnict wants to merge 1 commit into
Conversation
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
Contributor
|
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. |
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.
Problem
When LLMs generate malformed JSON in
tool_calls.function.arguments(e.g., missing commas, truncated due tomax_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
{"path": "/tmp/x"}{"path": "/tmp/x" "content": "hi"}{"_raw": "..."}""(empty)"{}"(fallback)null/undefined"{}"(fallback)Root Causes of Malformed JSON
Trade-offs
JSON.parse()on every tool_call argument — negligible cost (microseconds){"_raw": "..."}may confuse model, but strictly better than 400 error_rawfieldRelated