From 8af34657c07c63bfc44466a1e9cf9e44ba5b85ad Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Thu, 26 Nov 2026 23:13:28 +0100 Subject: [PATCH] fix(models): parse DeepSeek-V3.1 inline tool calls in LiteLlm When LiteLLM leaves DeepSeek-V3.1 tool call tokens in the message content, LiteLlm finds no tool call and returns the raw tokens as model text, so the tool never runs. The inline DeepSeek parser only knew the V3 layout, where the call starts with the word "function", a separator, the name and a newline. DeepSeek-V3.1 writes the name directly after the call begin token and the JSON arguments directly after the separator token. _DS_TOOL_CALL_RE now accepts both layouts. Well-formed V3 calls parse as before. A V3.1 call must end before the next call begin token, so an unclosed call does not take the arguments of the call after it. Several calls in one block, nested JSON arguments, surrounding text and malformed arguments behave as they do for V3, and streaming uses the same text parser. --- src/google/adk/models/lite_llm.py | 24 +++++-- tests/unittests/models/test_litellm.py | 90 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/src/google/adk/models/lite_llm.py b/src/google/adk/models/lite_llm.py index 7300fab9ca7..848ae2aefda 100644 --- a/src/google/adk/models/lite_llm.py +++ b/src/google/adk/models/lite_llm.py @@ -2416,12 +2416,23 @@ def _build_tool_call_from_json_dict( _DS_TCALL_END = "\u003c\uff5ctool\u2581call\u2581end\uff5c\u003e" _DS_TSEP = "\u003c\uff5ctool\u2581sep\uff5c\u003e" -# Pattern: <|tool▁call▁begin|>function<|tool▁sep|>NAME \n ARGS <|tool▁call▁end|> +# Two layouts, one call each: +# V3: <|tool▁call▁begin|>function<|tool▁sep|>NAME \n ARGS <|tool▁call▁end|> +# V3.1: <|tool▁call▁begin|>NAME<|tool▁sep|>ARGS<|tool▁call▁end|> +# Group 1 is the V3 name, group 2 the V3.1 name, group 3 the arguments text. _DS_TOOL_CALL_RE = re.compile( re.escape(_DS_TCALL_BEGIN) - + r"function" + + r"(?:function" + re.escape(_DS_TSEP) - + r"([^\n\r]+?)\s*?\n(.*?)" + + r"([^\n\r]+?)\s*?\n|(?!function" + + re.escape(_DS_TSEP) + + r")([^<\n\r]+)" + + re.escape(_DS_TSEP) + + r"(?=(?:(?!" + + re.escape(_DS_TCALL_BEGIN) + + r").)*?" + + re.escape(_DS_TCALL_END) + + r"))(.*?)" + re.escape(_DS_TCALL_END), re.DOTALL, ) @@ -2471,7 +2482,8 @@ def _parse_deepseek_tool_calls_from_text( Token reference ``<|tool▁calls▁begin|>`` … ``<|tool▁calls▁end|>`` → outer wrapper - ``<|tool▁call▁begin|>function<|tool▁sep|>NAME`` → single call start + ``<|tool▁call▁begin|>function<|tool▁sep|>NAME`` → V3 call start + ``<|tool▁call▁begin|>NAME<|tool▁sep|>`` → V3.1 call start ``<|tool▁call▁end|>`` → single call end Args: @@ -2535,8 +2547,8 @@ def _parse_deepseek_tool_calls_from_text( # Parse individual tool calls inside the block. for match in _DS_TOOL_CALL_RE.finditer(block): - func_name = match.group(1).strip() - args_raw = match.group(2).strip() + func_name = (match.group(1) or match.group(2)).strip() + args_raw = match.group(3).strip() args_json = _extract_json_from_deepseek_args(args_raw) if not func_name or not args_json: continue diff --git a/tests/unittests/models/test_litellm.py b/tests/unittests/models/test_litellm.py index af993c02fb2..6496c916635 100644 --- a/tests/unittests/models/test_litellm.py +++ b/tests/unittests/models/test_litellm.py @@ -5114,6 +5114,96 @@ def test_parse_deepseek_unwrapped_call_before_wrapped_block(): assert remainder is None +def _ds_v31_tool_call(name: str, args_json: str) -> str: + """Build a single DeepSeek-V3.1 tool call: no type word, no code fence.""" + return f"{_DS_BEGIN_CALL}{name}{_DS_SEP}{args_json}{_DS_END_CALL}" + + +def test_parse_deepseek_v31_single_tool_call(): + """DeepSeek-V3.1 puts the name right after the begin token.""" + text = _ds_wrapped(_ds_v31_tool_call("get_weather", '{"city": "Paris"}')) + tool_calls, remainder = _parse_deepseek_tool_calls_from_text(text) + assert len(tool_calls) == 1 + assert tool_calls[0].function.name == "get_weather" + assert json.loads(tool_calls[0].function.arguments) == {"city": "Paris"} + assert remainder is None + + +def test_parse_deepseek_v31_multi_tool_call(): + """DeepSeek-V3.1 chains calls without separators.""" + text = _ds_wrapped( + _ds_v31_tool_call("get_weather", '{"city": "Paris"}') + + _ds_v31_tool_call("get_time", '{"tz": "CET"}') + ) + tool_calls, remainder = _parse_deepseek_tool_calls_from_text(text) + assert [tc.function.name for tc in tool_calls] == ["get_weather", "get_time"] + assert [tc.index for tc in tool_calls] == [0, 1] + assert remainder is None + + +def test_parse_deepseek_v31_nested_json_args(): + """Braces inside nested arguments do not end the JSON early.""" + args = '{"filter": {"tags": ["a", "b"], "range": {"min": 1}}, "n": 2}' + text = _ds_wrapped(_ds_v31_tool_call("search", f"\n {args}\n")) + tool_calls, remainder = _parse_deepseek_tool_calls_from_text(text) + assert len(tool_calls) == 1 + assert tool_calls[0].function.name == "search" + assert json.loads(tool_calls[0].function.arguments) == json.loads(args) + assert remainder is None + + +def test_parse_deepseek_v31_with_surrounding_text(): + """Text before and after a V3.1 block is kept.""" + text = ( + "Let me check. " + + _ds_wrapped(_ds_v31_tool_call("get_weather", '{"city": "Paris"}')) + + " Done." + ) + tool_calls, remainder = _parse_deepseek_tool_calls_from_text(text) + assert len(tool_calls) == 1 + assert tool_calls[0].function.name == "get_weather" + assert remainder == "Let me check. Done." + + +def test_parse_deepseek_v31_malformed_json_is_dropped(): + """A V3.1 call with broken JSON is dropped, as in the V3 layout.""" + text = _ds_wrapped( + _ds_v31_tool_call("broken", '{"a": ') + + _ds_v31_tool_call("get_time", '{"tz": "CET"}') + ) + tool_calls, remainder = _parse_deepseek_tool_calls_from_text(text) + assert [tc.function.name for tc in tool_calls] == ["get_time"] + assert remainder is None + + +def test_parse_deepseek_v3_call_missing_newline_is_not_a_v31_call(): + """A V3 call without its newline is not read as a tool named `function`.""" + text = _ds_wrapped( + f'{_DS_BEGIN_CALL}function{_DS_SEP}get_weather{{"city":"Paris"}}' + f"{_DS_END_CALL}" + ) + tool_calls, _ = _parse_deepseek_tool_calls_from_text(text) + assert tool_calls == [] + + +def test_parse_deepseek_v31_unclosed_call_does_not_swallow_next_call(): + """A stray unclosed call begin does not take the next real call's JSON.""" + text = _ds_wrapped( + f"{_DS_BEGIN_CALL}trunc{_DS_SEP}" + + _ds_v31_tool_call("get_weather", '{"city": "Paris"}') + ) + tool_calls, _ = _parse_deepseek_tool_calls_from_text(text) + assert [tc.function.name for tc in tool_calls] == ["get_weather"] + + +def test_parse_tool_calls_from_text_handles_deepseek_v31_format(): + """Integration: the generic parser reads the V3.1 layout too.""" + text = _ds_wrapped(_ds_v31_tool_call("fetch_page", '{"url": "x"}')) + tool_calls, remainder = _parse_tool_calls_from_text(text) + assert [tc.function.name for tc in tool_calls] == ["fetch_page"] + assert remainder is None + + def test_extract_json_from_deepseek_args_invalid_fence_returns_none(): """Invalid JSON inside a code fence is rejected rather than returned.""" assert _extract_json_from_deepseek_args('```json\n{"a": 1,}\n```') is None