swagger api tool auth prefix duplicate

用户使用 Dify 1.15.0 自托管(Docker)环境,创建自定义 Swagger API 工具,然后在 Agent 中调用该工具。调用失败,返回 HTTP 401 认证错误。

快速结论:该报错发生在 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,根源在于 ApiToolassembling_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.pyassembling_request() 方法

解决步骤

  1. 方案一(推荐):升级 Dify – 升级到包含 PR #36350 修复的版本(2026年6月之后合并的版本)。
  2. 方案二(手动修补): 修改 api/core/tools/custom_tool/tool.py,在 assembling_request() 方法中使用局部变量处理认证头,避免对 credentials["api_key_value"] 直接修改。

验证方法

修复后,多次连续调用同一个 Swagger API 工具,检查认证是否始终正常,不再出现 HTTP 401 错误或 ToolInvokeError

参考来源

langgenius/dify #38615

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 20085

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注