Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): keep detected type for Other File Types uploads (#41292)

Sourav Rajvib3c326793d41
diff --git a/api/factories/file_factory/builders.py b/api/factories/file_factory/builders.py
index 660cd95228c..b3375421bd6 100644
--- a/api/factories/file_factory/builders.py
+++ b/api/factories/file_factory/builders.py
@@ -173,13 +173,16 @@ def _resolve_file_type(
``custom`` is a configured extension bucket rather than a MIME-derived type,
so strict validation must leave extension checks to the upload config.
+ Files accepted through that bucket still keep their detected type
+ (image/document/audio/video) so LLM nodes can attach them as prompt content.
+ Unknown files remain ``custom``.
"""
if not specified_type:
return detected_file_type
specified_file_type = FileType(specified_type)
if specified_file_type == FileType.CUSTOM:
- return FileType.CUSTOM
+ return detected_file_type
if strict_type_validation and detected_file_type != specified_file_type:
raise ValueError("Detected file type does not match the specified type. Please verify the file.")
diff --git a/api/tests/e2e_tests/contracts/core/workflow/nodes/llm/test_llm_utils.py b/api/tests/e2e_tests/contracts/core/workflow/nodes/llm/test_llm_utils.py
index aeec0709be5..e8c03714f27 100644
--- a/api/tests/e2e_tests/contracts/core/workflow/nodes/llm/test_llm_utils.py
+++ b/api/tests/e2e_tests/contracts/core/workflow/nodes/llm/test_llm_utils.py
@@ -489,6 +489,45 @@ def test_handle_list_messages_splits_text_and_file_content():
mock_to_prompt.assert_called_once()
+def test_handle_list_messages_skips_custom_file_types():
+ """LLM prompt assembly omits FileType.CUSTOM, which is why Other File Types
+ must be rewritten to the detected MIME type before reaching this path (#41236).
+ """
+ variable_pool = VariablePool.empty()
+ custom_file = File(
+ file_id="notes",
+ file_type=FileType.CUSTOM,
+ filename="notes.txt",
+ transfer_method=FileTransferMethod.REMOTE_URL,
+ remote_url="https://example.com/notes.txt",
+ related_id="notes-related",
+ extension=".txt",
+ mime_type="text/plain",
+ storage_key="",
+ )
+ variable_pool.add(["input", "file"], custom_file)
+
+ with mock.patch(
+ "graphon.nodes.llm.llm_utils.file_manager.to_prompt_message_content",
+ ) as mock_to_prompt:
+ prompt_messages = llm_utils.handle_list_messages(
+ messages=[
+ LLMNodeChatModelMessage(
+ text="Read {{#input.file#}}",
+ role=PromptMessageRole.USER,
+ edition_type="basic",
+ )
+ ],
+ context="",
+ jinja2_variables=[],
+ variable_pool=variable_pool,
+ vision_detail_config=ImagePromptMessageContent.DETAIL.HIGH,
+ )
+
+ mock_to_prompt.assert_not_called()
+ assert prompt_messages == [UserPromptMessage(content=[TextPromptMessageContent(data="Read ")])]
+
+
def test_handle_list_messages_supports_array_file_segments():
variable_pool = VariablePool.empty()
first_file = _build_image_file(file_id="first", related_id="first-related", remote_url="https://example.com/1.png")
diff --git a/api/tests/unit_tests/factories/file_factory/test_builders.py b/api/tests/unit_tests/factories/file_factory/test_builders.py
index c3f5467eac6..7c5337b9a1a 100644
--- a/api/tests/unit_tests/factories/file_factory/test_builders.py
+++ b/api/tests/unit_tests/factories/file_factory/test_builders.py
@@ -1,5 +1,5 @@
import uuid
-from collections.abc import Iterator
+from collections.abc import Callable, Iterator
from dataclasses import dataclass
from datetime import UTC, datetime
from unittest.mock import patch
@@ -18,9 +18,20 @@ from core.app.file_access import (
)
from core.workflow.file_reference import build_file_reference, parse_file_reference, resolve_file_record_id
from extensions.storage.storage_type import StorageType
+from factories.file_factory.builders import _resolve_file_type
from factories.file_factory.builders import build_from_mapping as _build_from_mapping
from factories.file_factory.builders import build_from_mappings as _build_from_mappings
from graphon.file import File, FileTransferMethod, FileType, FileUploadConfig
+from graphon.file.file_manager import to_prompt_message_content
+from graphon.model_runtime.entities import (
+ DocumentPromptMessageContent,
+ ImagePromptMessageContent,
+ PromptMessageRole,
+ TextPromptMessageContent,
+)
+from graphon.nodes.llm import llm_utils
+from graphon.nodes.llm.entities import LLMNodeChatModelMessage
+from graphon.runtime import VariablePool
from models import CreatorUserRole, ToolFile, UploadFile
# Test Data
@@ -510,7 +521,11 @@ def test_disallowed_extensions(file_records: FileRecords):
def test_custom_file_type_uses_extension_validation_under_strict_mode(file_records: FileRecords):
- """Custom form uploads are classified by the configured extension list."""
+ """Custom form uploads are classified by the configured extension list.
+
+ Strict MIME matching is skipped for the custom bucket, but the File still
+ carries the detected type so LLM nodes can read known formats (#41236).
+ """
file_records.upload_file.extension = "txt"
file_records.upload_file.name = "notes.txt"
file_records.upload_file.mime_type = "text/plain"
@@ -533,4 +548,198 @@ def test_custom_file_type_uses_extension_validation_under_strict_mode(file_recor
strict_type_validation=True,
)
- assert file.type == FileType.CUSTOM
+ assert file.type == FileType.DOCUMENT
+
+
[email protected]("strict_type_validation", [False, True])
+ ("extension", "name", "mime_type", "expected_type"),
+ [
+ ("jpg", "photo.jpg", "image/jpeg", FileType.IMAGE),
+ ("pdf", "report.pdf", "application/pdf", FileType.DOCUMENT),
+ ("m4a", "notes.m4a", "audio/mp4", FileType.AUDIO),
+ ("mp4", "clip.mp4", "video/mp4", FileType.VIDEO),
+ ("wma", "meeting.wma", "audio/x-ms-wma", FileType.AUDIO),
+ ("xyz", "blob.xyz", "application/octet-stream", FileType.CUSTOM),
+ ],
+)
+def test_custom_bucket_preserves_detected_type_for_llm(
+ file_records: FileRecords,
+ extension: str,
+ name: str,
+ mime_type: str,
+ expected_type: FileType,
+ strict_type_validation: bool,
+):
+ """Other File Types is an upload bucket, not a prompt-content type.
+
+ Graphon LLM nodes skip ``FileType.CUSTOM``, so known files accepted through
+ that bucket must keep their detected image/document/audio/video type.
+ """
+ file_records.upload_file.extension = extension
+ file_records.upload_file.name = name
+ file_records.upload_file.mime_type = mime_type
+ file_records.session.commit()
+
+ custom_config = FileUploadConfig(
+ allowed_file_types=[FileType.CUSTOM],
+ allowed_file_extensions=[f".{extension}"],
+ )
+ mapping = {
+ "transfer_method": "local_file",
+ "upload_file_id": TEST_UPLOAD_FILE_ID,
+ "type": "custom",
+ }
+
+ file = build_from_mapping(
+ mapping=mapping,
+ tenant_id=TEST_TENANT_ID,
+ config=custom_config,
+ strict_type_validation=strict_type_validation,
+ )
+
+ assert file.type == expected_type
+ assert file.filename == name
+
+
[email protected]("strict_type_validation", [False, True])
[email protected]("allow_documents", [False, True])
+def test_custom_bucket_rejects_disallowed_extension_for_detected_document(
+ file_records: FileRecords,
+ strict_type_validation: bool,
+ allow_documents: bool,
+):
+ """Detecting a document must not bypass the submitted custom bucket's allowlist."""
+ file_records.upload_file.extension = "pdf"
+ file_records.upload_file.name = "report.pdf"
+ file_records.upload_file.mime_type = "application/pdf"
+ file_records.session.commit()
+
+ allowed_types = [FileType.CUSTOM]
+ if allow_documents:
+ allowed_types.append(FileType.DOCUMENT)
+
+ with pytest.raises(ValueError, match="File validation failed"):
+ build_from_mapping(
+ mapping=local_file_mapping(file_type="custom"),
+ tenant_id=TEST_TENANT_ID,
+ config=FileUploadConfig(
+ allowed_file_types=allowed_types,
+ allowed_file_extensions=[".txt"],
+ ),
+ strict_type_validation=strict_type_validation,
+ )
+
+
+ ("detected", "specified", "strict", "expected"),
+ [
+ (FileType.IMAGE, "custom", True, FileType.IMAGE),
+ (FileType.DOCUMENT, "custom", False, FileType.DOCUMENT),
+ (FileType.CUSTOM, "custom", True, FileType.CUSTOM),
+ (FileType.IMAGE, None, True, FileType.IMAGE),
+ (FileType.AUDIO, "audio", True, FileType.AUDIO),
+ ],
+)
+def test_resolve_file_type_custom_bucket_keeps_detected_type(
+ detected: FileType,
+ specified: str | None,
+ strict: bool,
+ expected: FileType,
+):
+ assert (
+ _resolve_file_type(
+ detected_file_type=detected,
+ specified_type=specified,
+ strict_type_validation=strict,
+ )
+ == expected
+ )
+
+
+def test_custom_bucket_document_is_attached_to_llm_prompt(file_records: FileRecords):
+ """A Start-node Other File Types PDF must reach LLM prompt conversion (#41236)."""
+ file_records.upload_file.extension = "pdf"
+ file_records.upload_file.name = "report.pdf"
+ file_records.upload_file.mime_type = "application/pdf"
+ file_records.session.commit()
+
+ file = build_from_mapping(
+ mapping={
+ "transfer_method": "local_file",
+ "upload_file_id": TEST_UPLOAD_FILE_ID,
+ "type": "custom",
+ },
+ tenant_id=TEST_TENANT_ID,
+ config=FileUploadConfig(
+ allowed_file_types=[FileType.CUSTOM],
+ allowed_file_extensions=[".pdf"],
+ ),
+ )
+ assert file.type == FileType.DOCUMENT
+
+ variable_pool = VariablePool.empty()
+ variable_pool.add(["start", "file"], file)
+ prompt_content = DocumentPromptMessageContent(
+ format="pdf",
+ url="https://example.com/report.pdf",
+ mime_type="application/pdf",
+ filename="report.pdf",
+ )
+
+ with patch(
+ "graphon.nodes.llm.llm_utils.file_manager.to_prompt_message_content",
+ return_value=prompt_content,
+ ) as mock_to_prompt:
+ prompt_messages = llm_utils.handle_list_messages(
+ messages=[
+ LLMNodeChatModelMessage(
+ text="Read {{#start.file#}}",
+ role=PromptMessageRole.USER,
+ edition_type="basic",
+ )
+ ],
+ context="",
+ jinja2_variables=[],
+ variable_pool=variable_pool,
+ vision_detail_config=ImagePromptMessageContent.DETAIL.HIGH,
+ )
+
+ mock_to_prompt.assert_called_once()
+ assert mock_to_prompt.call_args.args[0] is file
+ last_content = prompt_messages[-1].content
+ assert isinstance(last_content, list)
+ assert any(part is prompt_content for part in last_content)
+
+
+def test_custom_bucket_pdf_converts_to_document_prompt_content(
+ file_records: FileRecords, config_overrides: Callable[..., None]
+):
+ """Detected type must produce document prompt content, not an unsupported placeholder."""
+ file_records.upload_file.extension = "pdf"
+ file_records.upload_file.name = "report.pdf"
+ file_records.upload_file.mime_type = "application/pdf"
+ file_records.session.commit()
+
+ file = build_from_mapping(
+ mapping={
+ "transfer_method": "local_file",
+ "upload_file_id": TEST_UPLOAD_FILE_ID,
+ "type": "custom",
+ },
+ tenant_id=TEST_TENANT_ID,
+ config=FileUploadConfig(
+ allowed_file_types=[FileType.CUSTOM],
+ allowed_file_extensions=[".pdf"],
+ ),
+ )
+ assert file.type == FileType.DOCUMENT
+
+ config_overrides(MULTIMODAL_SEND_FORMAT="url")
+ with patch("graphon.file.helpers.resolve_file_url", return_value="https://files.example/report.pdf"):
+ content = to_prompt_message_content(file)
+
+ assert isinstance(content, DocumentPromptMessageContent)
+ assert content.mime_type == "application/pdf"
+ assert content.filename == "report.pdf"
+ assert not isinstance(content, TextPromptMessageContent)