Repository navigation
fix(models): parse DeepSeek-V3.1 inline tool calls in LiteLlm - #8383
Open
Yasser-Ameur wants to merge 1 commit into
Open
Yasser-Ameur wants to merge 1 commit into
Yasser-Ameur wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
LiteLlmdoes 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_REinsrc/google/adk/models/lite_llm.pynever matches, the tokens come back as model text and the tool never runs.Steps to Reproduce (offline, no model needed)
Expected Behavior
A function call
get_weatherwith{"city": "Paris"}, as for the V3 layout.Observed Behavior
Base SHA 03725e8: a single text part that holds the raw tokens.
With the fix:
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.pyregex. That LiteLLM returns these tokens as text instead oftool_callsis not confirmed; #5024 reports it intermittently for V3.Solution:
Extend
_DS_TOOL_CALL_REto accept both layouts: the V3 form first and unchanged, otherwiseNAME<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_argsas 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 namedfunctionis not parsed, same as on main.This touches a different function than #8382 (the Llama
parameterskey in the same fallback); the two merge without conflicts.Testing Plan
Unit Tests:
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 fix8 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 onlite_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