fix: handle malformed JSON in LiteLLM tool call arguments gracefully#5010
Open
yasumorishima wants to merge 2 commits intogoogle:mainfrom
Open
fix: handle malformed JSON in LiteLLM tool call arguments gracefully#5010yasumorishima wants to merge 2 commits intogoogle:mainfrom
yasumorishima wants to merge 2 commits intogoogle:mainfrom
Conversation
When a LiteLLM model returns malformed or truncated JSON in tool_call.function.arguments, the non-streaming response parser now catches JSONDecodeError, logs a warning, and skips the affected tool call instead of crashing the entire invocation. The streaming path already handled this case; this fix aligns the non-streaming path with the same behavior. Fixes google#5008
Per CodeRabbit review: logger.warning no longer logs raw tool call arguments or tool call id, which may contain user data.
c670d6c to
3809fd5
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.
Summary
Fixes #5008
When a LiteLLM model returns malformed or truncated JSON in
tool_call.function.arguments, the non-streaming response parser (_message_to_generate_content_response) previously crashed the entire invocation withJSONDecodeError.This PR wraps the
json.loadscall in atry/except JSONDecodeErrorblock, skips the affected tool call with a warning log, and continues processing any remaining valid tool calls.Key design decisions:
_finalize_tool_call_response), which already handles this caseTesting plan
Unit tests added in
tests/unittests/models/test_litellm.py:test_message_to_generate_content_response_malformed_tool_call_json— mixed valid/malformed tool calls; verifies only valid ones are included in the responsetest_message_to_generate_content_response_all_malformed_tool_calls— all tool calls malformed; verifies empty parts (no crash)