Skip to content

fix: place Qwen tools before system prompt#10579

Open
he-yufeng wants to merge 1 commit into
hiyouga:mainfrom
he-yufeng:fix/qwen-tools-before-system
Open

fix: place Qwen tools before system prompt#10579
he-yufeng wants to merge 1 commit into
hiyouga:mainfrom
he-yufeng:fix/qwen-tools-before-system

Conversation

@he-yufeng

Copy link
Copy Markdown

Summary

  • render qwen3_5-style tool descriptions before user system content
  • keep the old system-before-tools order for other tool formats
  • add a regression test for the qwen3_5_nothink encode path

Fixes #10491.

To verify

  • PYTHONPATH=src python -m pytest tests/data/test_template.py -k "qwen35_tools_precede_system_content" -q
  • python -m py_compile src/llamafactory/data/template.py tests/data/test_template.py
  • python -m ruff check src/llamafactory/data/template.py tests/data/test_template.py
  • git diff --check

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 introduces a helper method _format_system_content to correctly order and format system instructions and tool definitions, specifically ensuring that tools precede system content for the qwen3_5 tool format. A corresponding unit test was also added to verify this behavior. The review feedback suggests improving the robustness of this new method by handling cases where the system argument might be None to prevent potential runtime TypeError during string concatenation.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +60 to +65
def _format_system_content(self, system: str, tool_text: str) -> str:
if tool_text and self.format_tools.tool_format == "qwen3_5":
tool_text = tool_text.lstrip("\n")
return tool_text + ("\n\n" + system if system else "")

return system + tool_text

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure robust defensive programming, _format_system_content should handle cases where system might be None (e.g., if self.default_system is not set or is set to None in custom templates). Accepting Optional[str] and defaulting to an empty string prevents potential runtime TypeError during string concatenation.

Suggested change
def _format_system_content(self, system: str, tool_text: str) -> str:
if tool_text and self.format_tools.tool_format == "qwen3_5":
tool_text = tool_text.lstrip("\n")
return tool_text + ("\n\n" + system if system else "")
return system + tool_text
def _format_system_content(self, system: Optional[str], tool_text: str) -> str:
system = system or ""
if tool_text and self.format_tools.tool_format == "qwen3_5":
tool_text = tool_text.lstrip("\n")
return tool_text + ("\n\n" + system if system else "")
return system + tool_text

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.

训练qwen3.6的tool_call时prompt拼接方式与模型原生拼接方式不一致

1 participant