Prhub

#45867 [Bugfix][Gemma4] Render reasoning on assistant turns without tool_calls

原始 PR 作者 lucianommartins 合并时间 2026-06-18 04:44 文件变更 1 提交数 3 评论 8 代码增减 +2 / -2

执行摘要

修复 Gemma4 模板中 reasoning 在无工具调用时被丢弃

修复 #45553 引入的模板回归:thinking channel 守卫 and message.get('tool_calls') 导致 assistant 消息没有 tool_calls 时 reasoning 内容被静默丢弃,例如模型在使用工具推理后给出最终回答的场景。

建议阅读以了解聊天模板中 thinking channel 的渲染逻辑,以及如何通过微小条件修复纠正模型行为。该 PR 展示了回归修复的典型协作流程。

讨论亮点

· m4r1k 指出 #45832 已覆盖 adjust_request 部分,本 PR 只需专注于模板修改,作者同意在 #45832 合并后 rebase。
· bbrowning 审查时指出注释 (tool-call turns only) 不再准确,lucianommartins 已修正。

实现拆解

  1. 定位回归源:#45553 在聊天模板中为 thinking channel 加上了 message.get('tool_calls') 条件,意图是只在工具调用轮次渲染 reasoning,却误过滤了没有 tool_calls 的 assistant 消息。
  2. 修改条件判断:将 {%- if thinking_text and thinking_gate and message.get('tool_calls') -%} 改为 {%- if thinking_text and thinking_gate -%},使 reasoning 渲染不再依赖 tool_calls 存在。
  3. 同步注释:将 (tool-call turns only) 注释更新为通用描述。
  4. 测试验证:运行 124 个现有单元测试全部通过,且作者提供了需要展示推理的多轮对话样例。
文件 模块 状态 重要度
examples/tool_chat_template_gemma4.jinja 聊天模板 modified 3.69

分析完成后,这里会展示 LLM 生成的相对完整源码片段和详细注释。

评论区精华

覆盖 adjust_request 修复的范围 设计

m4r1k 评论:@lucianommartins, nice catch! The adjust_request change here is the same fix as my #45832 which is already green... it should land shortly. Once it does, the adjust_request part here becomes redundant, but the chat-template reasoning fix is still needed and isn't covered by #45832. Cleanest is probably to rebase #45867 down to just the template change...

结论:作者同意在 #45832 合并后 rebase,最终 PR 只保留模板修改。 · 已解决

同步注释反映新行为 documentation

bbrowning 审查指出:注释 `(tool-call turns only)` 不再准确。lucianommartins 回复已修正。

结论:注释更新为通用描述。 · 已解决

风险与影响

变更仅 2 行(条件移除 + 注释更新),逻辑简单,但可能引发极低风险的 reasoning 过渲染(当 thinking_text 和 thinking_gate 都满足但本不该渲染时)。然而 thinking_gate 本身有控制逻辑(仅用户之后或 preserve_thinking),且现有测试覆盖正常场景,风险可控。

影响范围局限于使用 Gemma4 模型并启用 reasoning 和工具调用的用户。修复后,工具链末尾的 assistant 回答能正确展示推理内容,提升多轮交互体验。由于改动极小且经测试验证,对系统稳定性和性能无影响。

低风险

关联 Issue

#45553 [Bugfix][Gemma4] Fix offline parser truncation, adjust_request token leak, and chat template sync
#45832 [Bugfix][Gemma4] Fix parsing when thinking is disabled

完整报告

参与讨论