Skip to content

Conversation

Xu-Wenqing
Copy link
Contributor

@Xu-Wenqing Xu-Wenqing commented Jun 10, 2025

Motivation

Fix this: #7048

Modifications

Test Script:

from openai import OpenAI

openai_api_base = ""
openai_api_key = ""

client = OpenAI(
    api_key=openai_api_key,
    base_url=openai_api_base + "/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_temperature",
            "description": "Get current temperature at a location.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": 'The location to get the temperature for, in the format "City, State, Country".',
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": 'The unit to return the temperature in. Defaults to "celsius".',
                    },
                },
                "required": ["location"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "get_temperature_date",
            "description": "Get temperature at a location and date.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": 'The location to get the temperature for, in the format "City, State, Country".',
                    },
                    "date": {
                        "type": "string",
                        "description": 'The date to get the temperature for, in the format "Year-Month-Day".',
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": 'The unit to return the temperature in. Defaults to "celsius".',
                    },
                },
                "required": ["location", "date"],
            },
        },
    },
]


tool_calls_stream = client.chat.completions.create(
    model=client.models.list().data[0].id,
    messages=[
        {
            "role": "system",
            "content": "现在的日期是: 2024-09-30",
        },
        {
            "role": "user",
            "content": "北京今天的天气如何?明天呢?",
        },
    ],
    tools=tools,
    tool_choice="auto",
    stream=True,
    max_completion_tokens=8192,
)

for chunk in tool_calls_stream:
    if chunk.choices[0].delta.tool_calls:
        print(chunk.choices[0].delta.tool_calls)

Test Result:

[ChoiceDeltaToolCall(index=0, id='call_2C5AP3LBS0GVXLWKsLa1zA', function=ChoiceDeltaToolCallFunction(arguments='', name='get_current_temperature'), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='{"location": "', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='Be', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='ijing', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments=',', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments=' China"', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments=', "unit": "', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='c', name=None), type='function')]
[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='elsius"}', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id='call_BOfUCJghQvizNT4gf8b-FA', function=ChoiceDeltaToolCallFunction(arguments='', name='get_temperature_date'), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='{"location": "', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='Be', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='ijing', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments=',', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments=' China"', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments=', "date": "', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='2', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='0', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='2', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='4', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='-', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='1', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='0', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='-', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='0', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='1"', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments=', "unit": "', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='c', name=None), type='function')]
[ChoiceDeltaToolCall(index=1, id=None, function=ChoiceDeltaToolCallFunction(arguments='elsius"}', name=None), type='function')]

Checklist

… output.

Signed-off-by: 许文卿 <xwq391974@alibaba-inc.com>
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.

Summary of Changes

Hello @Xu-Wenqing, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a specific bug in the streaming API response for tool calls. Previously, only the very first tool call object in the stream would receive an id. This change updates the streaming logic to assign a unique id whenever a new tool call object is encountered, identified by a change in the tool_index, thus fixing the reported issue.

Highlights

  • Bug Fix: Resolved an issue where tool_call.id was not included in streaming output for tool calls with an index greater than 0.
  • Streaming Tool Calls: Modified the logic within the streaming response generation to correctly identify the start of a new tool call object based on its index, ensuring a unique ID is generated for each distinct tool call in the stream.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 effectively fixes a bug where tool call IDs were missing for subsequent tool calls in streaming outputs. The change from using a tool_call_first flag to tracking tool_index_previous and tool_index_current is a clear and correct solution. The updated logic ensures that a unique ID is generated for the first chunk of each new tool call, while subsequent chunks of the same tool call correctly have a null ID, which aligns with standard API behavior for streaming tool calls.

@Xu-Wenqing Xu-Wenqing marked this pull request as ready for review June 10, 2025 12:09
@Xu-Wenqing
Copy link
Contributor Author

@zhyncs @CatherineSue

Copy link
Collaborator

@CatherineSue CatherineSue left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for fixing this @Xu-Wenqing
Can you add a screenshot of a local test if possible? Maybe reuse the script in the issue

@zhyncs zhyncs merged commit a0e4d4e into sgl-project:main Jun 10, 2025
47 of 53 checks passed
@Xu-Wenqing
Copy link
Contributor Author

LGTM. Thanks for fixing this @Xu-Wenqing Can you add a screenshot of a local test if possible? Maybe reuse the script in the issue

@CatherineSue Added script and streaming return in PR description.

almaslof pushed a commit to mpashkovskii/sglang that referenced this pull request Jun 11, 2025
… output. (sgl-project#7049)

Signed-off-by: 许文卿 <xwq391974@alibaba-inc.com>
jianan-gu pushed a commit to jianan-gu/sglang that referenced this pull request Jun 12, 2025
… output. (sgl-project#7049)

Signed-off-by: 许文卿 <xwq391974@alibaba-inc.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants