Skip to content

Fix OpenAI tool-call handling for Qwen - #155

Open
alexinthesky wants to merge 1 commit into
NotPunchnox:mainfrom
alexinthesky:fix/qwen-openai-tool-calls
Open

Fix OpenAI tool-call handling for Qwen#155
alexinthesky wants to merge 1 commit into
NotPunchnox:mainfrom
alexinthesky:fix/qwen-openai-tool-calls

Conversation

@alexinthesky

@alexinthesky alexinthesky commented May 18, 2026

Copy link
Copy Markdown

• Title:
Fix OpenAI tool-call handling for Qwen

Description:

Summary

Fixes OpenAI-compatible tool calling for Qwen-style models.

This PR:

  • normalizes OpenAI message content before applying tokenizer chat templates
  • recovers Qwen <tool_call> responses when generation stops after a partial JSON object
  • avoids emitting empty tool-call responses as successful tool calls
  • fixes required f-string quoting so server_utils.py remains valid on Python 3.9+

Context

Qwen chat templates emit tool calls as:

<tool_call>
{"name": "...", "arguments": {...}}
</tool_call>

Some small RKLLM Qwen builds can stop after emitting a partial JSON object. Previously that
produced either no usable OpenAI tool_calls field or an empty tool-call response.

@NotPunchnox

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves OpenAI-compatible tool-calling behavior for Qwen-style models by normalizing message content before applying tokenizer chat templates, and by repairing truncated <tool_call> JSON outputs so tool calls can still be recovered reliably.

Changes:

  • Normalize OpenAI/Ollama message content variants (e.g., list/dict forms) into plain text before apply_chat_template.
  • Add best-effort repair logic to recover Qwen <tool_call> outputs when generation stops mid-JSON.
  • Tighten streaming tool-call emission to avoid treating empty tool-call parses as successful tool calls, and fix Python 3.9+ f-string quoting.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/rkllama/api/server_utils.py Normalizes messages for chat templates and adjusts streaming tool-call handling (including Python 3.9-safe f-strings).
src/rkllama/api/format_utils.py Adds truncated-JSON repair and integrates it into get_tool_calls as a fallback for Qwen outputs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 470 to 478
# If tool calls detected, send them as final response
if tools and tool_calls:
if tools and tool_calls and json_tool_calls:
chunk_tool_call = cls.format_streaming_chunk(model_name=model_name, token=json_tool_calls, tool_calls=tool_calls)
yield f"{json.dumps(chunk_tool_call)}\n"
elif len(final_response_tokens) < max_token_to_wait_for_tool_call:
for temp_token in response_tokens:
time.sleep(0.1) # Simulate delay to stream previos tokens
chunk = cls.format_streaming_chunk(model_name=model_name, token=temp_token,tool_calls=tool_calls)
chunk = cls.format_streaming_chunk(model_name=model_name, token=temp_token)
yield f"{json.dumps(chunk)}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants