Repository navigation
fix(llmcore): 空 tool_use_id 的 tool_result 不再反转顺序/插到正文前 - #879
Closed
leonsong09 wants to merge 1 commit into
Closed
leonsong09 wants to merge 1 commit into
leonsong09 wants to merge 1 commit into
Conversation
chat() 里 tool_use_id 为空的 tool_result(文本兜底还原出来的 MockToolCall
默认 id='')原本走「前置插入裸文本」分支:
else: combined_content = [{"type": "text", "text": f'<tool_result>{content}</tool_result>'}] + combined_content
每次 prepend 都会把上一次插入的挤到后面,导致两个缺陷:
1. 多个空 id 结果顺序反转(实测 R1,R2 发出 R2,R1)
2. 与有 id 的 tool_result_blocks(append,顺序正确)语义不一致
改为按出现顺序收进 orphan_results,循环结束后 extend 到 combined_content 尾部。
所有 id 齐全时的行为逐字节不变。
验证(抽取该函数体 + 桩依赖,4 个用例):
- 两个空 id + 一个真 id -> R1 在 R2 前,且都在用户正文之后
- 全部有 id -> 顺序与正文位置不变
- 只有空 id -> 正文在结果之前
- _pending_tool_ids 补空 content 行为不变
This was referenced Nov 13, 2026
Author
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.
问题
NativeToolClient.chat()里,tool_use_id为空的tool_result(文本兜底还原出来的MockToolCall默认id='')走的是「前置插入裸文本」分支:每次 prepend 会把上一次插入的挤到后面,导致:
tool_result_blocks(append,顺序正确)语义不一致实测
3 个结果 R1 / R2(无 id)+ R3(有 id):
修复前:
{"role": "user", "content": [ {"type": "tool_result", "tool_use_id": "call_X", "content": "R3"}, {"type": "text", "text": "<tool_result>R2</tool_result>"}, {"type": "text", "text": "<tool_result>R1</tool_result>"}, {"type": "text", "text": "USER_TEXT"}]}修复后:
{"role": "user", "content": [ {"type": "tool_result", "tool_use_id": "call_X", "content": "R3"}, {"type": "text", "text": "USER_TEXT"}, {"type": "text", "text": "<tool_result>R1</tool_result>"}, {"type": "text", "text": "<tool_result>R2</tool_result>"}]}改法
空 id 结果先按出现顺序收进
orphan_tails,循环结束后按序拼到combined_content尾部。所有 id 齐全时的行为逐字节不变(那条路径完全没动)。
这个场景真的会发生吗
会。触发条件是模型不调用原生 tool 接口、把调用写成正文伪 XML(
_parse_text_tool_calls兜底路径),而一次响应返回 2 个伪 XML 调用是实测见过的。
本地 24 个生产会话日志的统计:
[ERROR] Your tool call was emitted as plain XML text (...)6 次(welfare ×5、buddy ×1),5 次当轮自愈_parse_text_tool_calls全部可还原(43 处返 1 个调用,2 处返 2 个)→ 后两例就会踩到顺序反转
后果:模型拿到逆序的工具结果,多步脚本类工具(先查表结构再查数据)容易误判;
排查日志时看到 R2 在 R1 前,还会被误当成并发/竞态。
测试
新增
tests/test_tool_result_order.py:test_orphan_results_keep_order—— 两个空 id 不再反转test_orphan_after_user_text—— 裸文本结果排在用户正文之后test_pending_ids_filled——_pending_tool_ids补空 content 的行为不受影响未包含在本 PR 的两点
tool_result记成裸文本<tool_result>{...}塞进 user 消息 —— 这是日志格式问题,改动会影响重放/审计链路,建议单独评估。
textual_tool_residue()对上述 45 处全部返回None(即可还原),检测器工作正常,无需改动。