[Bug]: A tool file with meta mime_type=None crashes agent file extraction with a ValidationError

当工具返回的 BLOB 、 IMAGE_LINK 或 LINK 消息中 meta 带有 {"mime_type": None} 时,Agent 侧文件提取会把 None 直接传给 ToolInvokeMessageBinary(mimetype=...) ,从而触发 pydantic 校验失败,工具

快速结论:当工具返回的 BLOB、IMAGE_LINK 或 LINK 消息中 meta 带有 {"mime_type": None} 时,Agent 侧文件提取会把 None 直接传给 ToolInvokeMessageBinary(mimetype=...),从而触发 pydantic 校验失败,工具调用被报成“unknown error”且文件卡片丢失。优先排查工具输出 MIME 类型是否为 None,以及 tool_engine.py 中三处 response.meta.get("mime_type", mimetype) 的取值逻辑。

适用环境:Dify 自托管(Source)方式部署,版本为 main at e5acabc3d(2026-10-29),Issue 标签记录为 1.18.0。Issue 未提供操作系统、Python、CUDA、显卡等环境信息,此处不补写。

最快修复方案:暂无确认的一步修复方案。Issue 中的建议修复是把三处调用点改为 mimetype = response.meta.get("mime_type") or mimetype,使 None 回落到默认的 application/octet-stream;该改法来自报告者的建议,并非已合并的代码。

注意事项:如果由插件侧临时规避,应提供非空 MIME 字符串(未知类型用 application/octet-stream);对 LINK 消息不要直接删除 mime_type 键,因为提取器只有在元数据包含 mime_type 时才把 LINK 当作附件处理,删键会绕过附件提取而不是恢复文件卡片。

问题场景

在 Dify 中运行带文件输出的工具或插件时,插件发出的 BLOB、IMAGE_LINK 或 LINK 消息的 meta 为 {"mime_type": None}(例如插件未拿到 content-type)。消息经 api/core/tools/utils/message_transformer.py 转换后仍保留该 None 值,随后进入 Agent 调用路径 api/core/tools/tool_engine.py 构建二进制消息,触发 pydantic ValidationError,工具调用最终报“unknown error”,文件未能附加到 Agent 消息上。

报错原文

[Bug]: A tool file with meta mime_type=None crashes agent file extraction with a ValidationError

transformed: binary_link {'mime_type': None, 'filename': 'greeting', 'tool_file_id': 'abc123'}
ValidationError: 1 validation error for ToolInvokeMessageBinary
mimetype
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

unknown error: 1 validation error ...

原因分析

可能原因是取值方式使用了带默认值的 dict.get:response.meta.get("mime_type", mimetype) 只在键不存在时返回默认值,而此处键存在、值为 None,因此 None 被传给 ToolInvokeMessageBinary(mimetype=...),导致字符串类型校验失败。

Issue 指出,消息转换器的回退逻辑(api/core/tools/utils/message_transformer.py:142)在存储文件时已回退到 application/octet-stream,且仓库自带测试 test_transform_tool_invoke_messages_mimetype_key_present_but_none 明确把该输入视为受支持;但 Agent 路径的三处调用点(tool_engine.py 约 :322、:330、:337)没有做同样的 None 回退。异常在 ToolEngine.agent_invoke 中被捕获并转换为“unknown error”,因此工具调用失败、文件卡片被丢弃。

环境排查

  • 确认 Dify 版本与部署方式,是否与 Issue 中的 main at e5acabc3d、自托管(Source)一致。
  • 确认触发问题的工具或插件类型(内置工具、插件),检查其输出消息的 meta.mime_type 实际取值是否为 None。
  • 检查 api/core/tools/tool_engine.py 中二进制消息构建处是否仍为 response.meta.get("mime_type", mimetype)。
  • Issue 未涉及 Python、CUDA、PyTorch、显卡等依赖版本,暂无相关证据可核对。

解决步骤

  1. 先定位问题来源:打印或记录插件返回消息的 meta,确认是否出现 {"mime_type": None}。
  2. 可优先尝试(源码修复):将 tool_engine.py 中三处 response.meta.get("mime_type", mimetype) 改为 response.meta.get("mime_type") or mimetype,使 None 回退到默认 MIME 类型;此为 Issue 中的建议修复,未经验证为官方合入方案。
  3. 如能控制发消息的插件,可优先尝试临时规避:改为提供非空 MIME 字符串,未知类型填 application/octet-stream。
  4. 对 LINK 消息不要简单删除 mime_type 键,否则提取器不会把它当作附件处理。
  5. 可优先尝试补充回归测试:复用转换器测试的 fake file manager,把 mime_type=None 的转换输出送入 _extract_tool_response_binary_and_text(),断言结果为 application/octet-stream。

验证方法

重新运行同一工具调用,确认不再出现 ValidationError for ToolInvokeMessageBinary,工具结果不再显示“unknown error”,且 Agent 消息中能正常附带文件卡片;MIME 类型应为 application/octet-stream 或插件提供的有效类型。若添加了回归测试,测试通过即可作为修复生效的依据。

参考来源

langgenius/dify #45086

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 36899

发表回复

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