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
- Update the
ChatCompletionMessageGenericParam class to include support for tool_calls
- 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
- The full version of Qwen3 chat template:
Checklist
Motivation
SGLang currently supports
tool_callsin output responses, but does not properly handle them in input messages. When users includetool_callsin the assistant turns within messages, these are not reflected in the tokens during model inference.Current Implementation
The current implementation only allows for
roleandcontentin assistant messages:sglang/python/sglang/srt/openai_api/protocol.py
Lines 253 to 255 in 1acca3a
sglang/python/sglang/srt/openai_api/adapter.py
Lines 968 to 983 in 1acca3a
Impact
This limitation causes the framework to deviate from the intended behavior of models like Qwen3, which explicitly handle
tool_callsin their chat templates. For example, Qwen's chat template includes specific handling for tool calls:Requested Enhancement
ChatCompletionMessageGenericParamclass to include support for tool_callsopenai_compatible_messagelist processing logic in theopenai_api/adapter.pyto handletool_callsin assistant messages properly, ensuring they are correctly incorporated when formatting messages for the modelWithout 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