Skip to content

fix(models): parse DeepSeek-V3.1 inline tool calls in LiteLlm - #8383

Open
Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/litellm-deepseek-v31-tool-calls
Open

Yasser-Ameur wants to merge 1 commit into
google:mainfrom
Yasser-Ameur:fix/litellm-deepseek-v31-tool-calls

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 leaves DeepSeek-V3.1 tool call tokens in the message content, LiteLlm does not turn them into function calls. The inline DeepSeek parser added in #5654 for #5024 only knows the V3 layout (function, separator, name, newline, fenced JSON). DeepSeek-V3.1 writes the name right after the call begin token and the JSON arguments right after the separator token, so _DS_TOOL_CALL_RE in src/google/adk/models/lite_llm.py never matches, the tokens come back 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

BAR, LOW = "|", "▁"


def tok(name):
  return f"<{BAR}{name.replace('_', LOW)}{BAR}>"


text = (
    tok("tool_calls_begin") + tok("tool_call_begin") + "get_weather" + tok("tool_sep")
    + '{"city": "Paris"}' + tok("tool_call_end") + tok("tool_calls_end")
)
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 V3 layout.

Observed Behavior

Base SHA 03725e8: a single text part that holds the raw tokens.

With the fix:

[('call', 'get_weather', {'city': 'Paris'})]

Environment Details

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

Model Information

DeepSeek-V3.1 through LiteLLM. No live run was done. The format comes from the model card "ToolCall" section (https://huggingface.co/deepseek-ai/DeepSeek-V3.1), its tokenizer chat template, and vLLM v0.11.0's deepseekv31_tool_parser.py regex. That LiteLLM returns these tokens as text instead of tool_calls is not confirmed; #5024 reports it intermittently for V3.

Solution:

Extend _DS_TOOL_CALL_RE to accept both layouts: the V3 form first and unchanged, otherwise NAME<sep>ARGS<call end>. A V3.1 call must end before the next call begin token, so an unclosed call cannot take the arguments of the call after it. The arguments go through _extract_json_from_deepseek_args as before, so nested objects and whitespace work, and unparseable JSON is dropped as in V3. Several calls in a block, surrounding text and the streaming path (same text parser) need no other change. Known limit: a V3.1 tool literally named function is not parsed, same as on main.

This touches a different function than #8382 (the Llama parameters key in the same fallback); the two merge without conflicts.

Testing Plan

Unit Tests:

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

Eight tests in tests/unittests/models/test_litellm.py, beside the existing DeepSeek ones: a single call, two calls, nested JSON with whitespace, surrounding text, malformed JSON dropped, an unclosed call that must not take the next call's arguments, a V3 call without its newline that must not be read as V3.1, and the path through _parse_tool_calls_from_text.

On the base source the new tests give 7 failed, 1 passed; with the fix 8 passed, 890 deselected in 4.99s. The whole file: 898 passed, 5 warnings in 8.43s. pyink, isort and ruff are clean and mypy reports no issues on lite_llm.py. The full unit suite was not run locally.

The old and new parsers were also compared on generated inputs: 20000 well-formed V3 blocks give identical results, and across 60000 random inputs no call that the base parser returned changed or disappeared.

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.

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.
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