Fix OpenAI tool-call handling for Qwen - #155
Open
alexinthesky wants to merge 1 commit into
Open
Conversation
NotPunchnox
approved these changes
Aug 14, 2026
Owner
|
@copilot resolve the merge conflicts in this pull request |
There was a problem hiding this comment.
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
contentvariants (e.g., list/dict forms) into plain text beforeapply_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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
• Title:
Fix OpenAI tool-call handling for Qwen
Description:
Summary
Fixes OpenAI-compatible tool calling for Qwen-style models.
This PR:
<tool_call>responses when generation stops after a partial JSON objectserver_utils.pyremains valid on Python 3.9+Context
Qwen chat templates emit tool calls as: