[BugFix] fix tool call parser#7369
Conversation
|
Thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
该 PR 聚焦修复 OpenAI 入口下 ErnieX1 工具调用解析(tool call parser)在流式场景中的边界问题,尤其是 arguments={}(空字典)以及以数字/布尔值/嵌套 } 结尾时的收尾解析与增量输出。
Changes:
- 调整
ErnieX1ToolParser.extract_tool_calls()的tools_called判定逻辑,避免在未提取到工具调用时仍返回tools_called=True。 - 修复流式解析 close 分支与参数分支中对
arguments的判定:由“真假值判断”改为“None 判断”,从而正确处理空字典等 falsy 参数。 - 增加多组回归/集成测试,覆盖
arguments={}、数字/布尔结尾、嵌套对象结尾等流式边界切分。
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| fastdeploy/entrypoints/openai/tool_parsers/ernie_x1_tool_parser.py | 修复流式参数分支与 close 分支对空参数的错误判定,并调整 tools_called 返回逻辑 |
| tests/entrypoints/openai/tool_parsers/test_ernie_x1_tool_parser.py | 新增回归/集成测试覆盖空参数与多种 close 结尾场景 |
| if self.tool_call_start_token not in model_output: | ||
| return ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=model_output) | ||
| else: | ||
| try: | ||
| tool_call_json_list = self.tool_call_regex.findall(model_output) | ||
| tool_calls = [] | ||
| for tool_call_json in tool_call_json_list: | ||
| tool_call_dict = json.loads(tool_call_json) | ||
| args_str = json.dumps(tool_call_dict.get("arguments", {}), ensure_ascii=False) | ||
| tool_calls.append( | ||
| ToolCall( | ||
| type="function", | ||
| id=random_tool_call_id(), | ||
| function=FunctionCall( | ||
| name=tool_call_dict.get("name", ""), | ||
| arguments=args_str, | ||
| ), | ||
| ) | ||
| ) | ||
| return ExtractedToolCallInformation( | ||
| tools_called=True, | ||
| tools_called=len(tool_calls) > 0, | ||
| tool_calls=tool_calls, | ||
| ) |
| diff = self.prev_tool_call_arr[self.current_tool_id].get("arguments") | ||
| if diff: | ||
| if '"}' not in delta_text: | ||
| if diff is not None: | ||
| if "}" not in delta_text: | ||
| return None | ||
| end_loc = delta_text.rindex("}") | ||
| diff = delta_text[:end_loc] | ||
| if not diff: | ||
| return None |
| def test_streaming_empty_arguments_full_flow(self): | ||
| """Integration: streaming tool call with arguments={} must not lose arguments. | ||
|
|
||
| Simulates a complete streaming flow where the tool call has empty | ||
| arguments. Verifies the name is sent and arguments are streamed. | ||
| """ |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7369 +/- ##
==========================================
Coverage ? 73.57%
==========================================
Files ? 383
Lines ? 53626
Branches ? 8415
==========================================
Hits ? 39456
Misses ? 11483
Partials ? 2687
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 AI Code Review | 2025-04-14
📋 Review 摘要
PR 概述:修复 ErnieX1ToolParser 中空参数判断错误、tools_called 误判、以及流式输出结尾处理过于严格的问题
变更范围:entrypoints/openai/tool_parsers/
影响面 Tag:[APIServer]
📝 PR 规范检查
问题:PR 标题包含 [BugFix] Tag 且符合格式要求,描述有 Motivation 和 Modifications,但 Checklist 未勾选任何项。
建议:
- 标题格式符合要求,无需修改
- 建议更新 Checklist,至少勾选格式化和单元测试相关项
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🟡 建议 | ernie_x1_tool_parser.py:188 |
rindex('}') 在 delta_text 不包含 } 时会抛出 ValueError |
总体评价
PR 修复逻辑正确,测试覆盖全面,但存在一个边界情况未处理。
| diff = self.prev_tool_call_arr[self.current_tool_id].get("arguments") | ||
| if diff: | ||
| if '"}' not in delta_text: | ||
| if diff is not None: |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
review不对,并不会出现这个问题
* fix tool call parser * add unit test * fix unit test * add unit test
|
✅ Cherry-pick successful! Created PR: #7417 |
* fix tool call parser * add unit test * fix unit test * add unit test
|
ℹ️ Cherry-pick PR already exists: #7417 |
* fix tool call parser * add unit test * fix unit test * add unit test
|
✅ Cherry-pick successful! Created PR: #7418 |
* fix tool call parser * add unit test * fix unit test * add unit test
|
ℹ️ Cherry-pick PR already exists: #7417 |
* fix tool call parser * add unit test * fix unit test * add unit test
|
ℹ️ Cherry-pick PR already exists: #7418 |
* fix tool call parser * add unit test * fix unit test * add unit test
|
✅ Cherry-pick successful! Created PR: #7419 |
Motivation
extract_tool_calls中tools_called在tool_calls为空时仍返回True的问题{}被误判为无参数的问题'"}'检查过于严格导致数字/布尔值结尾的参数无法正确流式传输的问题Modifications
tools_called=True改为tools_called=len(tool_calls) > 0if diff:改为if diff is not None:,正确处理空字典if '"}' not in delta_text:改为if "}" not in delta_text:,并使用rindex("}")获取正确位置if not cur_arguments改为if cur_arguments is None,避免空字典误判Usage or Command
Accuracy Tests
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.