Skip to content

fix(models): read Llama's "parameters" key in inline LiteLlm tool calls - #8382

Open
Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/litellm-inline-tool-call-parameters
Open

Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/litellm-inline-tool-call-parameters

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

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:

Describe the Bug

When LiteLLM returns a Llama 3.x JSON tool call as message text, LiteLlm does not turn it into a function call. Llama 3.x documents the form {"name": ..., "parameters": {...}}, optionally after <|python_tag|>, which ADK already strips as a call prefix. _build_tool_call_from_json_dict in src/google/adk/models/lite_llm.py reads only arguments, so the text is returned as model text and the tool never runs.

Steps to Reproduce (offline, no model needed)

from google.adk.models.lite_llm import _message_to_generate_content_response

for text in (
    '{"name": "get_weather", "parameters": {"city": "Paris"}}',
    '<|python_tag|>{"name": "get_weather", "parameters": {"city": "Paris"}}',
):
  resp = _message_to_generate_content_response({"role": "assistant", "content": text})
  print([("call", p.function_call.name, p.function_call.args) if p.function_call else ("text", p.text) for p in resp.content.parts])

Expected Behavior

A function call get_weather with {"city": "Paris"}, as for the same object written with arguments.

Observed Behavior

Base SHA 03725e8:

[('text', '{"name": "get_weather", "parameters": {"city": "Paris"}}')]
[('text', '<|python_tag|>{"name": "get_weather", "parameters": {"city": "Paris"}}')]

With the fix (5b3dd94):

[('call', 'get_weather', {'city': 'Paris'})]
[('text', '<|python_tag|>'), ('call', 'get_weather', {'city': 'Paris'})]

The leftover <|python_tag|> text part is existing behaviour for that prefix.

Environment Details

Python 3.14.7, google-adk at 03725e8 (editable install), litellm and openai installed per constraints-3.14.txt, Windows 11.

Model Information

Llama 3.x (3.1 and 3.3 JSON tool calling) through LiteLLM. No live model run was done. Which provider routes (Ollama, vLLM, others via LiteLLM) actually return this form as text, instead of translating it to tool_calls, is not confirmed. The format comes from Meta's Llama 3.1 and 3.3 prompt format docs and Ollama's Llama 3.1 template; vLLM's llama3_json parser (v0.10.0) accepts parameters when arguments is absent.

Solution:

In _build_tool_call_from_json_dict, read parameters when the object has no arguments key and no description key. arguments still wins whenever the key is present (including null, which stays text as on main). A tool definition also carries parameters, and its description keeps it as text. parameters as a dict or JSON string follows the same path arguments does. One change covers the non-streaming and streaming paths, which share _parse_tool_calls_from_text.

One trade-off to be aware of: an answer that is an object with name and parameters and no description is now read as a call, the same way an object with name and arguments already is. The fallback is not gated on declared tools on main either. parameters given as a list or a non-JSON string was text before and now yields MALFORMED_FUNCTION_CALL, as the arguments form does on main.

Testing Plan

Unit Tests:

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

Three tests (9 cases) in tests/unittests/models/test_litellm.py: parameters bare and after <|python_tag|>, as a dict and as a JSON string; arguments preferred when both exist; non-tool JSON stays text, including a tool definition with description and parameters.

On the base source the new tests give 4 failed, 5 passed, 890 deselected in 6.09s; with the fix 9 passed, 890 deselected in 5.08s. The whole file: 899 passed, 5 warnings in 8.60s. pyink, isort and ruff are clean and mypy reports no issues on lite_llm.py. The full unit suite was not run locally.

Manual End-to-End (E2E) Tests:

None against a live model. The offline snippet above is the check, with its output on the base commit and on this branch.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Llama 3.x JSON tool calling answers with {"name": ..., "parameters": {...}},
optionally after <|python_tag|>. When LiteLLM hands that text back in the
message content, LiteLlm left it as text and the tool never ran.

The inline tool call fallback in _build_tool_call_from_json_dict read only
the "arguments" key and returned None when it was missing.

It now falls back to "parameters" when the object has no "arguments" key and
no "description" key. A tool definition also has "parameters" and a
"description", so it stays text. JSON without either key is treated as
before. An object with "name" and "parameters" and no "description" is now
read as a call, the same way an object with "name" and "arguments" already
is.
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.

1 participant