From 5b3dd949662f7803ca99fffe3af6eeea0da1b154 Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Thu, 26 Nov 2026 22:54:26 +0100 Subject: [PATCH] fix(models): read Llama's "parameters" key in inline LiteLlm tool calls 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. --- src/google/adk/models/lite_llm.py | 4 ++ tests/unittests/models/test_litellm.py | 54 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/google/adk/models/lite_llm.py b/src/google/adk/models/lite_llm.py index 7300fab9ca7..a55d1917f6c 100644 --- a/src/google/adk/models/lite_llm.py +++ b/src/google/adk/models/lite_llm.py @@ -2370,6 +2370,10 @@ def _build_tool_call_from_json_dict( name = candidate.get("name") args = candidate.get("arguments") + # Llama 3.x writes "parameters" where other models write "arguments". A tool + # definition also has "parameters", and is told apart by its "description". + if "arguments" not in candidate and "description" not in candidate: + args = candidate.get("parameters") if not isinstance(name, str) or args is None: return None diff --git a/tests/unittests/models/test_litellm.py b/tests/unittests/models/test_litellm.py index af993c02fb2..908bc781ca2 100644 --- a/tests/unittests/models/test_litellm.py +++ b/tests/unittests/models/test_litellm.py @@ -4938,6 +4938,60 @@ def test_parse_tool_calls_from_text_multiple_calls(): assert remainder == "ignored suffix" +@pytest.mark.parametrize( + "prefix", + ["", "<|python_tag|>"], + ids=["bare", "python_tag"], +) +@pytest.mark.parametrize( + "parameters", + [{"city": "Paris"}, '{"city": "Paris"}'], + ids=["dict", "json_string"], +) +def test_parse_tool_calls_from_text_accepts_llama_parameters_key( + prefix, parameters +): + """Llama 3.x names the argument object "parameters", not "arguments".""" + text = prefix + json.dumps({"name": "get_weather", "parameters": parameters}) + + tool_calls, remainder = _parse_tool_calls_from_text(text) + + assert [call.function.name for call in tool_calls] == ["get_weather"] + assert json.loads(tool_calls[0].function.arguments) == {"city": "Paris"} + assert remainder == (prefix or None) + + +def test_parse_tool_calls_from_text_prefers_arguments_over_parameters(): + text = ( + '{"name": "get_weather", "arguments": {"city": "Paris"},' + ' "parameters": {"city": "Rome"}}' + ) + + tool_calls, _ = _parse_tool_calls_from_text(text) + + assert len(tool_calls) == 1 + assert json.loads(tool_calls[0].function.arguments) == {"city": "Paris"} + + +@pytest.mark.parametrize( + "text", + [ + '{"name": "get_weather"}', + '{"title": "Report", "parameters": {"city": "Paris"}}', + '{"name": 7, "parameters": {"city": "Paris"}}', + ( + '{"name": "get_weather", "description": "Get the weather",' + ' "parameters": {"type": "object", "properties": {}}}' + ), + ], +) +def test_parse_tool_calls_from_text_leaves_non_tool_json_as_text(text): + tool_calls, remainder = _parse_tool_calls_from_text(text) + + assert tool_calls == [] + assert remainder == text + + def test_parse_tool_calls_from_text_stops_at_text_between_calls(): """A tool call that follows prose is quoted text, not another call.""" beta = '{"name":"beta","arguments":{"timezone":"Asia/Taipei"}}'