执行摘要
- 一句话:修复 /v1/responses API 在请求无工具时仍会触发模型幻觉函数调用的问题。
- 推荐动作:该 PR 值得快速浏览,以理解 /v1/responses API 工具调用逻辑的关键修复。关注点在于
construct_tool_dicts 函数中条件判断的调整,这是修复的核心。虽然变更简单,但揭示了 API 层默认行为与工具检查的交互设计,对于维护前端入口点有参考价值。
功能与动机
根据 PR body 描述,/v1/responses API 存在一个行为不一致的问题:当请求中没有提供工具时,由于 construct_tool_dicts 函数只检查 tools 是否为 None,而不检查空列表,且 API 层无条件默认设置 tool_choice="auto",导致即使请求没有工具,模型仍会尝试进行函数调用,产生幻觉输出。这需要与 /v1/chat/completions 的行为对齐,后者仅在确实提供工具时才设置 tool_choice="auto"。
实现拆解
- 核心逻辑调整:修改
vllm/entrypoints/openai/responses/utils.py 中的 construct_tool_dicts 函数,将条件判断从 if tools is None or (tool_choice == "none"): 改为 if not tools or (tool_choice == "none"):。这样,当 tools 为空列表或 None 时,都会返回 None,从而禁用工具调用。
- 影响范围:此变更直接影响 /v1/responses API 的工具调用逻辑,确保无工具请求不会触发模型生成函数调用。它修复了与 /v1/chat/completions 的行为差异,提升 API 一致性。
- 测试与配置:本次变更仅涉及源码文件修改,没有看到直接对应的测试文件变更或配置调整。这可能意味着需要依赖现有测试覆盖,或后续补充测试以确保回归安全。
关键文件:
vllm/entrypoints/openai/responses/utils.py(模块 入口点;类别 source;类型 core-logic;符号 construct_tool_dicts): 这是唯一变更的文件,包含修复工具调用逻辑的核心函数 construct_tool_dicts。
关键符号:construct_tool_dicts
关键源码片段
vllm/entrypoints/openai/responses/utils.py
这是唯一变更的文件,包含修复工具调用逻辑的核心函数 construct_tool_dicts。
def construct_tool_dicts(
tools: list[Tool], tool_choice: ToolChoice
) -> list[dict[str, Any]] | None:
# 修复:使用 not tools 替代 tools is None,以同时处理 None 和空列表情况
# 当工具列表为空或 tool_choice 为 "none" 时,返回 None 以禁用工具调用
if not tools or (tool_choice == "none"):
tool_dicts = None
else:
# 否则,将工具列表转换为 API 所需的格式
tool_dicts = [
convert_tool_responses_to_completions_format(tool.model_dump())
for tool in tools
]
return tool_dicts
评论区精华
review 评论较少,主要来自自动化工具:
风险与影响
关联脉络
- PR #39892 [Bugfix][Responses API] Fix streaming tool calls on /v1/responses: 同属 /v1/responses API 的工具调用修复,涉及工具解析器逻辑,可视为相关功能线。
- PR #39352 [Frontend] Preserve structured output special tokens in offline LLM.chat: 同属前端入口点(entrypoints)的变更,关注 API 行为一致性。
参与讨论