Skip to content

[Feature] Support for tool_calls in Assistant Messages Input #5983

Description

@kibitzing

Checklist

Motivation

SGLang currently supports tool_calls in output responses, but does not properly handle them in input messages. When users include tool_calls in the assistant turns within messages, these are not reflected in the tokens during model inference.

Current Implementation

The current implementation only allows for role and content in assistant messages:

class ChatCompletionMessageGenericParam(BaseModel):
role: Literal["system", "assistant", "tool"]
content: Union[str, List[ChatCompletionMessageContentTextPart], None]

openai_compatible_messages = []
for message in request.messages:
if message.content is None:
message.content = ""
if isinstance(message.content, str):
openai_compatible_messages.append(
{"role": message.role, "content": message.content}
)
else:
content_list = message.dict()["content"]
for content in content_list:
if content["type"] == "text":
openai_compatible_messages.append(
{"role": message.role, "content": content["text"]}
)

Impact

This limitation causes the framework to deviate from the intended behavior of models like Qwen3, which explicitly handle tool_calls in their chat templates. For example, Qwen's chat template includes specific handling for tool calls:

{%- if message.tool_calls %}
    {%- for tool_call in message.tool_calls %}
        {%- if (loop.first and content) or (not loop.first) %}
            {{- '\\n' }}
        {%- endif %}
        {%- if tool_call.function %}
            {%- set tool_call = tool_call.function %}
        {%- endif %}
        {{- '<tool_call>\\n{\"name\": \"' }}
        {{- tool_call.name }}
        {{- '\", \"arguments\": ' }}
        {%- if tool_call.arguments is string %}
            {{- tool_call.arguments }}
        {%- else %}
            {{- tool_call.arguments | tojson }}
        {%- endif %}
        {{- '}\\n</tool_call>' }}
    {%- endfor %}
{%- endif %}

Requested Enhancement

  1. Update the ChatCompletionMessageGenericParam class to include support for tool_calls
  2. Modify the openai_compatible_message list processing logic in the openai_api/adapter.py to handle tool_calls in assistant messages properly, ensuring they are correctly incorporated when formatting messages for the model

Without these capabilities, the inference operates differently from what both users and model creators would expect, potentially leading to reduced functionality and unexpected behavior in tool-using scenarios.

I have implemented a fix for this issue and am ready to submit a PR once this feature request is confirmed. This should help avoid duplicate effort from other contributors.

Related resources

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions