
swagger api tool auth prefix duplicate
快速结论:该报错发生在 Dify 中用户创建 Swagger API 工具并在 Agent 中调用时,通常由 api_key_value 认证前缀(如 Bearer )在多次调用中被重复拼接导致,优先排查 Dify 版本是否已包含修复。
问题场景
用户使用 Dify 1.15.0 自托管(Docker)环境,创建自定义 Swagger API 工具,然后在 Agent 中调用该工具。调用失败,返回 HTTP 401 认证错误。
报错原文
HTTP Request: GET https://langgenius.zendesk.com/api/v2/tickets/3358.json "HTTP/1.1 401 Unauthorized"
Traceback (most recent call last):
File "/app/api/core/tools/tool_engine.py", line 231, in _invoke
yield from tool.invoke(session, user_id, tool_parameters, conversation_id, app_id, message_id)
File "/app/api/core/tools/custom_tool/tool.py", line 401, in _invoke
parsed_response = self.validate_and_parse_response(response)
File "/app/api/core/tools/custom_tool/tool.py", line 140, in validate_and_parse_response
raise ToolInvokeError(f"Request failed with status code {response.status_code} and {response.text}")
core.tools.errors.ToolInvokeError: Request failed with status code 401 and {"error":"Couldn't authenticate you"}
原因分析
这是一个已知 bug,根源在于 ApiTool 的 assembling_request() 方法中对 runtime.credentials["api_key_value"] 进行了原地修改(in-place mutation)。由于 ToolRuntime 实例在多次调用间会被复用,导致认证前缀被重复拼接:
- 第 1 次调用:
Authorization: Bearer my-token✅ - 第 2 次调用:
Authorization: Bearer Bearer my-token❌ - 第 3 次调用:
Authorization: Bearer Bearer Bearer my-token❌
该问题已在 PR #36350 修复,但修复可能尚未包含在 1.15.0 版本中。
环境排查
- Dify 版本:1.15.0(或其他低于修复合并的版本)
- 部署方式:Self Hosted(Docker)
- 相关源码路径:
api/core/tools/custom_tool/tool.py中assembling_request()方法
解决步骤
- 方案一(推荐):升级 Dify – 升级到包含 PR #36350 修复的版本(2026年6月之后合并的版本)。
- 方案二(手动修补): 修改
api/core/tools/custom_tool/tool.py,在assembling_request()方法中使用局部变量处理认证头,避免对credentials["api_key_value"]直接修改。
验证方法
修复后,多次连续调用同一个 Swagger API 工具,检查认证是否始终正常,不再出现 HTTP 401 错误或 ToolInvokeError。



