fix(langgraph): filter plain-text tool_call markers from model history - #53
Open
pengliang100 wants to merge 1 commit into
Open
fix(langgraph): filter plain-text tool_call markers from model history#53pengliang100 wants to merge 1 commit into
pengliang100 wants to merge 1 commit into
Conversation
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.
问题描述
当使用 LangChain/LangGraph 框架时,模型偶尔会输出

[tool_call] xxx纯文本格式,而不是正确的 Function Calling 结构化调用。这导致工具无法执行,用户体验受损。根因分析
context.py在构建对话历史时,将结构化的tool_call/tool_result事件拍平为带[tool_call]前缀的纯文本格式:text = f"[tool_call] {text}"
当这些带前缀的消息通过 history 传入 LangGraph Runner 时,模型从历史中学习到了错误的输出格式,直接模仿输出 [tool_call] xxx 纯文本,而非触发真正的工具调用。
复现步骤
修复方案
方案选择
采用消费端过滤方案(而非修改 context.py 的生成逻辑),原因:
改动内容
- 跳过 [tool_call]/[tool_result]/[approval_request]/[approval_response] 格式的纯文本消息
- 防止模型从历史中学到错误格式
- 增加对方括号格式 [tool_call] 的过滤
- 防止模型错误输出污染最终结果
测试