Skip to content

fix(api): treat an empty tool file mimetype as missing - #45156

Merged
41tair merged 4 commits into
langgenius:mainfrom
uhhgxg:fix-tool-file-mimetype-none
Nov 19, 2026
Merged

41tair merged 4 commits into
langgenius:mainfrom
uhhgxg:fix-tool-file-mimetype-none

Conversation

@uhhgxg

@uhhgxg uhhgxg commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Problem

ToolEngine._extract_tool_response_binary_and_text read the tool file mime type with
response.meta.get("mime_type", default). dict.get only falls back when the key is absent, so a meta carrying
mime_type: None passed None to ToolInvokeMessageBinary.mimetype and pydantic rejected it:

ValidationError: 1 validation error for ToolInvokeMessageBinary
mimetype
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

ToolFileMessageTransformer keeps that None on purpose — test_transform_tool_invoke_messages_mimetype_key_present_but_none
asserts it — so a plugin that reports a missing content type made the agent tool call fail with
unknown error: 1 validation error ... and the file was dropped (reported in #45086).

Change

api/core/tools/tool_engine.py, the three binary construction sites: use the or form so an empty value behaves like
a missing one.

message types before after
IMAGE / IMAGE_LINK / BINARY_LINK meta.get("mime_type", mimetype) meta.get("mime_type") or mimetype (keeps the url-extension guess)
BLOB meta.get("mime_type", "application/octet-stream") meta.get("mime_type") or "application/octet-stream"
LINK meta.get("mime_type", "application/octet-stream") if meta else "application/octet-stream" meta.get("mime_type") or "application/octet-stream" (the redundant conditional goes away)

The stored metadata contract is untouched — the transformer still keeps whatever the plugin reported, only the
extraction boundary normalizes it.

Tests

In api/tests/unit_tests/core/tools/test_tool_engine.py:

  • test_extract_binary_messages_treat_an_empty_mimetype_as_missing — mime_type: None across all five message types:
    the image paths keep their guessed type (image/png), the others fall back to application/octet-stream, and the
    urls are preserved.
  • test_extract_binary_messages_keep_an_explicit_mimetype — an explicit value still wins over the fallback.

Verification

  • Before/after behaviour harness that runs the real method body (extracted from the module with ast, against a
    replica of the two pydantic models): 5/10 before — exactly the five ValidationErrors from the report — and
    10/10 after; a message without meta still raises ValueError("missing meta data").
  • python -m py_compile on both files; ruff format --check clean against api/.ruff.toml (line-length 120).
  • The full api pytest suite was not runnable locally (no project virtualenv here), so CI is the gate for it.

Disclosure: the analysis, patch and tests here were prepared with an AI coding agent (WorkBuddy). The failing
expression was reproduced locally against the current main before opening this PR.

A tool message whose meta carries ``mime_type: None`` reached
``ToolEngine._extract_tool_response_binary_and_text`` and passed ``None`` to
``ToolInvokeMessageBinary.mimetype``, because ``dict.get(key, default)`` only falls
back when the key is absent. ``ToolFileMessageTransformer`` keeps that ``None`` on
purpose (``test_transform_tool_invoke_messages_mimetype_key_present_but_none``), so a
plugin that reports a missing content type made the agent tool call fail with
"unknown error: 1 validation error for ToolInvokeMessageBinary".

Use the ``or`` form at the three binary construction sites so an empty value behaves
like a missing one: the image paths keep their guessed mime type, the others fall
back to ``application/octet-stream``. The redundant conditional for LINK messages is
dropped by the same change.

Fixes langgenius#45086
@uhhgxg
uhhgxg requested a review from QuantumGhost as a code owner October 30, 2026 10:18
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 30, 2026
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-19 12:03:24.642526081 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-19 12:03:08.098495785 +0000
@@ -4132,29 +4132,25 @@
 ERROR Argument `_FakeFlaskApp` is not assignable to parameter `flask_app` with type `Flask` in function `core.tools.utils.dataset_retriever.dataset_multi_retriever_tool.DatasetMultiRetrieverTool._retriever` [bad-argument-type]
    --> tests/unit_tests/core/tools/utils/dataset_retriever/test_dataset_multi_retriever_tool.py:469:31
 ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:103:16
-ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:104:16
-ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:147:16
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:155:16
 ERROR Argument `Literal['not a dict']` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:371:32
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:379:32
 ERROR Argument `list[int]` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:374:32
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:382:32
 ERROR Argument `Literal[42]` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:377:32
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:385:32
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:416:48
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:424:48
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:453:48
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:461:48
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:499:24
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:507:24
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:522:48
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:530:48
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:543:48
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:551:48
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:600:48
+   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:608:48
 ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]
   --> tests/unit_tests/core/tools/workflow_as_tool/test_tool.py:78:56
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `variable_name` [missing-attribute]

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 86.97% 86.97% +0.00%
Strict coverage 86.63% 86.63% +0.00%
Typed symbols 95,696 95,701 +5
Untyped symbols 14,395 14,394 -1
Modules 4633 4633 0

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.27%. Comparing base (ab67a2f) to head (20c4422).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #45156   +/-   ##
=======================================
  Coverage   90.27%   90.27%           
=======================================
  Files        6459     6459           
  Lines      407411   407411           
  Branches    81799    81799           
=======================================
+ Hits       367782   367793   +11     
+ Misses      33221    33211   -10     
+ Partials     6408     6407    -1     
Flag Coverage Δ
api 90.09% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GYJ99 GYJ99 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the focused fix. Two regression cases would make the reported failure harder to reintroduce:

  1. Exercise the transformer → extractor boundary in one test. The existing test_transform_tool_invoke_messages_mimetype_key_present_but_none asserts that ToolFileMessageTransformer preserves meta["mime_type"] is None, while the new tests construct extractor input directly. Reusing that transformer's fake file manager and passing its actual BINARY_LINK result to ToolEngine._extract_tool_response_binary_and_text would verify that the agent receives an attachment with application/octet-stream, not just that a manually constructed message is accepted.
  2. Since the fix deliberately handles empty MIME values via or, parameterizing the relevant tests over both None and "" would cover that stated behavior (while retaining the explicit MIME precedence test).

These are test-coverage suggestions, not a claim that the patch is incorrect. I reviewed the diff and relevant source/tests; I have not run Dify's pytest suite locally.

@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Oct 31, 2026
@uhhgxg

uhhgxg commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, @GYJ99 — both suggestions are now covered in 5c1c4a3:

  1. Transformer → extractor boundary: test_transform_tool_invoke_messages_with_empty_mimetype_can_be_extracted now feeds the transformer's actual BINARY_LINK output through ToolEngine._extract_tool_response_binary_and_text and asserts the agent receives one attachment with application/octet-stream and the transformed URL — closing the loop end-to-end instead of hand-constructing extractor input.
  2. None vs "" parameterization: both the transformer test and the extractor test are now @pytest.mark.parametrized over [None, ""] (ids none/empty), while the explicit MIME-precedence test is retained.

Full CI on the new head is green (9/9, including Main CI Pipeline).

41tair
41tair previously approved these changes Nov 17, 2026
@41tair
41tair added this pull request to the merge queue Nov 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 17, 2026
@uhhgxg

uhhgxg commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping @41tair — this was APPROVED on Nov 17 and briefly entered the merge queue, but hasn't merged. The current head (5c1c4a3) is fully green on CI (33 passed / 14 skipped / 0 failed) and there's been no further feedback since GYJ99's review points were addressed. Is there anything blocking this? Happy to help if anything needs adjusting. Thanks!

@41tair
41tair added this pull request to the merge queue Nov 19, 2026
@41tair
41tair removed this pull request from the merge queue due to a manual request Nov 19, 2026
@41tair
41tair added this pull request to the merge queue Nov 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 19, 2026
@41tair
41tair added this pull request to the merge queue Nov 19, 2026
Merged via the queue into langgenius:main with commit d2aee05 Nov 19, 2026
51 checks passed
xhdnoah pushed a commit that referenced this pull request Dec 2, 2026
Co-authored-by: Byron.wang <[email protected]>
Co-authored-by: Byron Wang <[email protected]>
(cherry picked from commit d2aee05)
xhdnoah pushed a commit that referenced this pull request Dec 2, 2026
Co-authored-by: Byron.wang <[email protected]>
Co-authored-by: Byron Wang <[email protected]>
(cherry picked from commit d2aee05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants