Skip to content

Conversation

@eric8810
Copy link

@eric8810 eric8810 commented Aug 26, 2025

Fix regex patterns in DeepSeekV31ToolParser to use non-greedy matching

Purpose

Fix tool call parsing issues in DeepSeek V3.1 model by changing greedy regex patterns (.) to non-greedy patterns (.?) in the tool parser. This prevents over-matching when parsing function names and
arguments in tool calls, which could lead to incorrect extraction of tool call information from model outputs.

The issue occurs when the model output contains multiple tool separators or special tokens, causing the greedy patterns to match beyond the intended boundaries.

Test Plan

Test the regex pattern changes with various tool call scenarios:

  1. Single tool call with simple function name and arguments
  2. Multiple tool calls in sequence
  3. Tool calls containing special characters or nested structures
  4. Edge cases with malformed tool call syntax
# Test with DeepSeek V3.1 model using tool calls
python -m vllm.entrypoints.openai.api_server --model deepseek-ai/DeepSeek-V3.1-Base --enable-tool-calls
# Send requests with various tool call patterns to verify parsing

Test Result

The regex pattern changes ensure:
- Function names are correctly extracted without including separator tokens
- Function arguments are properly bounded and don't overflow into subsequent content
- Multiple tool calls are parsed independently without cross-contamination
- Streaming tool call parsing maintains consistency with non-streaming parsing

Before: Greedy patterns could match function_name<|tool▁sep|>args<|tool▁sep|>more_content incorrectly
After: Non-greedy patterns correctly match only function_name and args portions

---- The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
- The test plan, such as providing test command.
- The test results, such as pasting the results comparison before and after, or e2e results
- (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
- (Optional) Release notes update. If your change is user facing, please update the release notes draft in the https://docs.google.com/document/d/1YyVqrgX4gHTtrstbq8oWUImOyPCKSGnJ7xtTpmXzlRs/edit?tab=t.0.

BEFORE SUBMITTING, PLEASE READ https://docs.vllm.ai/en/latest/contributing (anything written below this line will be removed by GitHub Actions)

Change greedy .* to non-greedy .*? in tool call regex patterns to prevent
over-matching when parsing function names and arguments in tool calls.
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to fix issues with greedy regex patterns in the DeepSeekV31ToolParser by making them non-greedy. The changes to tool_call_regex are correct and comprehensive. However, the change to stream_tool_call_portion_regex is incomplete. While the function_name is now non-greedy, the function_arguments group remains greedy (.*), which can cause it to match beyond the intended arguments, including the <|tool call end|> token. I've provided a critical review comment with a suggested fix to make the argument parsing robust and prevent incorrect tool call extraction during streaming.

@github-actions
Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors.

You ask your reviewers to trigger select CI tests on top of fastcheck CI.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

🚀

@eric8810
Copy link
Author

#23454
@Xu-Wenqing please have a look.

@Xu-Wenqing
Copy link
Contributor

#23454 @Xu-Wenqing please have a look.

Looks good, thanks for your contribution.

@eric8810
Copy link
Author

@aarnphm
Could you please check this out?
This fix could solve complex arguments tool call problem for dsv3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek Related to DeepSeek models frontend tool-calling

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants