Repository navigation
fix(api): treat an empty tool file mimetype as missing - #45156
Conversation
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
Pyrefly Diffbase → 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]
|
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
GYJ99
left a comment
There was a problem hiding this comment.
Thanks for the focused fix. Two regression cases would make the reported failure harder to reintroduce:
- Exercise the transformer → extractor boundary in one test. The existing
test_transform_tool_invoke_messages_mimetype_key_present_but_noneasserts thatToolFileMessageTransformerpreservesmeta["mime_type"] is None, while the new tests construct extractor input directly. Reusing that transformer's fake file manager and passing its actualBINARY_LINKresult toToolEngine._extract_tool_response_binary_and_textwould verify that the agent receives an attachment withapplication/octet-stream, not just that a manually constructed message is accepted. - Since the fix deliberately handles empty MIME values via
or, parameterizing the relevant tests over bothNoneand""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.
|
Thanks for the careful review, @GYJ99 — both suggestions are now covered in 5c1c4a3:
Full CI on the new head is green (9/9, including Main CI Pipeline). |
|
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! |
Co-authored-by: Byron.wang <[email protected]> Co-authored-by: Byron Wang <[email protected]> (cherry picked from commit d2aee05)
Co-authored-by: Byron.wang <[email protected]> Co-authored-by: Byron Wang <[email protected]> (cherry picked from commit d2aee05)
Problem
ToolEngine._extract_tool_response_binary_and_textread the tool file mime type withresponse.meta.get("mime_type", default).dict.getonly falls back when the key is absent, so a meta carryingmime_type: NonepassedNonetoToolInvokeMessageBinary.mimetypeand pydantic rejected it:ToolFileMessageTransformerkeeps thatNoneon purpose —test_transform_tool_invoke_messages_mimetype_key_present_but_noneasserts 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 theorform so an empty value behaves likea missing one.
IMAGE/IMAGE_LINK/BINARY_LINKmeta.get("mime_type", mimetype)meta.get("mime_type") or mimetype(keeps the url-extension guess)BLOBmeta.get("mime_type", "application/octet-stream")meta.get("mime_type") or "application/octet-stream"LINKmeta.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: Noneacross all five message types:the image paths keep their guessed type (
image/png), the others fall back toapplication/octet-stream, and theurls are preserved.
test_extract_binary_messages_keep_an_explicit_mimetype— an explicit value still wins over the fallback.Verification
ast, against areplica of the two pydantic models): 5/10 before — exactly the five
ValidationErrors from the report — and10/10 after; a message without meta still raises
ValueError("missing meta data").python -m py_compileon both files;ruff format --checkclean againstapi/.ruff.toml(line-length 120).apipytest 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
mainbefore opening this PR.