fix(lite_llm): add JSON-tolerant tool call argument parsing with stri… - #6811
Open
Your7Maxx wants to merge 1 commit into
Open
fix(lite_llm): add JSON-tolerant tool call argument parsing with stri…#6811Your7Maxx wants to merge 1 commit into
Your7Maxx wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
…ct/non-strict modes
Lightweight models (e.g. DeepSeek V4 Flash) sometimes emit malformed JSON
in tool call arguments — missing commas, unquoted keys, trailing garbage,
or wrapped in markdown code fences. The existing `_parse_tool_call_arguments`
had repair strategies (ast.literal_eval, unquoted-key quoting) but still
raised `json.JSONDecodeError` as a last resort, and the only callsite in
`_message_to_generate_content_response` did not catch it, causing a hard
crash that terminated the entire agent pipeline.
Changes:
1. Add a `strict` keyword parameter (default True) to
`_parse_tool_call_arguments`. Existing callers keep the exception-raising
behavior. The streaming `_finalize_tool_call_response` depends on the
exception for truncation detection and is unaffected.
2. Add two new repair strategies before the final fallback:
- Strip markdown code fences (```json ... ```) and retry parsing
- Extract the first balanced {…} block via `JSONDecoder.raw_decode`
to tolerate trailing garbage text
3. When strict=False (non-strict mode), log a warning and return {} instead
of raising — this is used by `_message_to_generate_content_response`
so the pipeline continues with default tool call arguments.
4. Add 15 unit tests covering valid JSON, empty input, unquoted keys,
markdown fences, trailing text, trailing commas, single quotes,
nested objects, array values, escaped strings, and strict/non-strict
error modes.
Your7Maxx
force-pushed
the
fix/json-tolerant-tool-call-parsing
branch
from
August 19, 2026 06:13
fd116f0 to
734858f
Compare
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.
…ct/non-strict modes
Lightweight models (e.g. DeepSeek V4 Flash) sometimes emit malformed JSON in tool call arguments — missing commas, unquoted keys, trailing garbage, or wrapped in markdown code fences. The existing
_parse_tool_call_argumentshad repair strategies (ast.literal_eval, unquoted-key quoting) but still raisedjson.JSONDecodeErroras a last resort, and the only callsite in_message_to_generate_content_responsedid not catch it, causing a hard crash that terminated the entire agent pipeline.Changes:
strictkeyword parameter (default True) to_parse_tool_call_arguments. Existing callers keep the exception-raising behavior. The streaming_finalize_tool_call_responsedepends on the exception for truncation detection and is unaffected.json ...) and retry parsingJSONDecoder.raw_decodeto tolerate trailing garbage text_message_to_generate_content_responseso the pipeline continues with default tool call arguments.Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
If applicable, please follow the issue templates to provide as much detail as
possible.
Problem:
A clear and concise description of what the problem is.
Solution:
A clear and concise description of what you want to happen and why you choose
this solution.
Testing Plan
Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.
Unit Tests:
Ran all 15 new unit tests and existing related tests:
tests/unittests/models/test_litellm.py::TestParseToolCallArguments 15/15 PASSED
tests/unittests/models/test_litellm.py -k "extract_json_from_deepseek_args or parse_tool_calls_from_text" 5/5 PASSED
All pre-existing test failures are unrelated to this change.
Please include a summary of passed
pytestresults.Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.
Checklist
Additional context
Add any other context or screenshots about the feature request here.