Skip to content

fix(litellm): survive malformed JSON in tool call arguments - #6867

Open
adnanahamed66772ndpc wants to merge 2 commits into
google:mainfrom
adnanahamed66772ndpc:fix-litellm-malformed-tool-args-json
Open

fix(litellm): survive malformed JSON in tool call arguments#6867
adnanahamed66772ndpc wants to merge 2 commits into
google:mainfrom
adnanahamed66772ndpc:fix-litellm-malformed-tool-args-json

Conversation

@adnanahamed66772ndpc

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

When the upstream model returns malformed JSON in tool_call.function.arguments (truncated strings, unclosed objects, partial streams committed to the message), _message_to_generate_content_response raised json.JSONDecodeError. The exception propagated out of LiteLLM response parsing — upstream of any tool or callback boundary — so the entire invocation aborted with a generic streaming error and no information about the malformed tool call. This is a regression of #5008, still present at v2.0.0.

Solution:

Parse tool call arguments defensively inside _message_to_generate_content_response: on json.JSONDecodeError, log a warning carrying the tool call id, function name and parse error, then fall back to empty args on the function-call part. Downstream tool dispatch then surfaces a structured argument mismatch back to the model, which can retry with corrected JSON — the agent loop survives.

Strict parsing is preserved in _parse_tool_call_arguments itself because the streaming path (_finalize_tool_call_response) relies on it to detect arguments truncated by finish_reason == "length" and report a dedicated MAX_TOKENS error response; only the final conversion step now recovers instead of crashing.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.

  • All unit tests pass locally.

  • Added test_message_to_generate_content_response_tool_call_with_malformed_json_arguments: truncated arguments produce a function-call part with empty args while preserving name and id.

  • Added test_message_to_generate_content_response_tool_call_malformed_json_warns: the recovery emits a warning-level record.

  • Full model suite: pytest tests/unittests/models/test_litellm.py395 passed, including the existing Python-literal and unquoted-key repair tests which confirm no behavior change for valid inputs.

  • Reproduced the exact crash from [LiteLlm] JSONDecodeError on malformed tool_call.function.arguments still crashes invocation at v2.0.0 (regression of #5008) #5896 before the fix (JSONDecodeError propagating out of _message_to_generate_content_response) and verified it no longer reproduces after the fix.

  • pre-commit run --files (ruff, isort, pyink, addlicense, compliance checks) and mypy src/google/adk/models/lite_llm.py all pass.

Manual End-to-End (E2E) Tests:

Not applicable — the fix is covered by unit tests that construct the LiteLLM message directly, exactly matching the minimal reproduction from the issue. No network or provider credentials are required.

A truncated or otherwise malformed arguments string in a LiteLLM tool
call raised json.JSONDecodeError from _message_to_generate_content_response,
aborting the whole invocation before any callback or tool boundary could
handle it. Parse the arguments defensively, log a warning with the tool
call id and function name, and fall back to empty args so downstream tool
dispatch reports the mismatch back to the model for a retry.

Fixes google#5896
@google-cla

google-cla Bot commented Aug 23, 2026

Copy link
Copy Markdown

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.

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.

[LiteLlm] JSONDecodeError on malformed tool_call.function.arguments still crashes invocation at v2.0.0 (regression of #5008)

2 participants