Skip to content

fix(litellm): parse noncanonical tool call arguments#5756

Open
fancydirty wants to merge 3 commits into
google:mainfrom
fancydirty:fix/litellm-tool-call-args
Open

fix(litellm): parse noncanonical tool call arguments#5756
fancydirty wants to merge 3 commits into
google:mainfrom
fancydirty:fix/litellm-tool-call-args

Conversation

@fancydirty
Copy link
Copy Markdown

@fancydirty fancydirty commented May 19, 2026

Summary

LiteLLM tool calls normally provide function.arguments as a strict JSON string, but some OpenAI-compatible providers can finalize a complete tool call with noncanonical object-literal syntax such as Python dict literals or unquoted JSON object keys. ADK currently passes the string directly to json.loads(...), which raises JSONDecodeError and aborts response conversion before the tool call can be surfaced.

This PR adds a small parser helper for LiteLLM tool call arguments:

  • strict JSON remains the primary path
  • Python dict literal strings are accepted via ast.literal_eval
  • simple unquoted object keys are quoted without touching string contents, then parsed
  • unrecoverable malformed payloads still raise the original JSONDecodeError

The same parser is used for the streaming max-token/truncation check so complete noncanonical arguments are not mistaken for truncated arguments.

Why

Without this, a provider can stream a valid/intended tool call all the way to completion, but ADK crashes while converting the finalized LiteLLM message into LlmResponse.

testing plan

Passed locally on Python 3.10:

$ UV_PROJECT_ENVIRONMENT=.venv310 uv run --python 3.10 --extra test pytest tests/unittests/models/test_litellm.py tests/unittests/cli/utils/test_gcp_utils.py -q
269 passed, 1 skipped, 2 warnings in 3.03s

Pre-commit passed after applying formatting hooks:

$ UV_PROJECT_ENVIRONMENT=.venv310 uv run --python 3.10 --extra dev pre-commit run --all-files
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
pyproject-fmt............................................................Passed
isort....................................................................Passed
pyink....................................................................Passed
addlicense...............................................................Passed
Check new Python files have _ prefix.....................................Passed
mdformat.................................................................Passed

Bug-fix verification for the noncanonical argument forms:

$ UV_PROJECT_ENVIRONMENT=.venv310 uv run --python 3.10 --extra test pytest tests/unittests/models/test_litellm.py::test_message_to_generate_content_response_tool_call_accepts_python_literal_arguments tests/unittests/models/test_litellm.py::test_message_to_generate_content_response_tool_call_accepts_unquoted_json_keys -q
..                                                                       [100%]
2 passed in 2.47s

@fancydirty
Copy link
Copy Markdown
Author

Note on the failing �gent-triage-pull-request check: this appears unrelated to this PR diff. The workflow runs python -m adk_pr_triaging_agent.main with PYTHONPATH=contributing/samples, while the package lives under contributing/samples/adk_team/adk_pr_triaging_agent, so the job fails before inspecting this change with ModuleNotFoundError: No module named 'adk_pr_triaging_agent'.

@fancydirty fancydirty marked this pull request as ready for review May 19, 2026 16:31
@adk-bot adk-bot added the models [Component] Issues related to model support label May 20, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented May 20, 2026

Response from ADK Triaging Agent

Hello @fancydirty, thank you for creating this PR!

We appreciate your contribution to improve LiteLLM tool call parsing. This PR is looking great, but to help our reviewers process it more efficiently and in accordance with our contribution guidelines, could you please update the PR description to include the following:

  • Summary of passed pytest results: Please run the tests and paste a quick summary showing that the tests passed.
  • Logs or Screenshots: Since this is a bug fix, please provide logs or a screenshot demonstrating the behavior after the fix is applied (e.g., showing a successful tool call parse with the noncanonical arguments).

This information will help reviewers to review your PR more efficiently. Thanks!

@rohityan rohityan self-assigned this May 20, 2026
@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label May 20, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

Hi @fancydirty , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please fix the failing unit tests before we can proceed with the review.

@fancydirty
Copy link
Copy Markdown
Author

Hi @rohityan, thanks for the review. I pushed a follow-up commit to fix the pre-commit formatting failures, and updated the PR description with a dedicated testing plan plus local pytest/pre-commit output and the noncanonical-arguments verification logs requested by the triage bot. Current PR checks are green on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants