Skip to content

feat: support files as context#33341

Open
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:issue-33340
Open

feat: support files as context#33341
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:issue-33340

Conversation

@fatelei
Copy link
Contributor

@fatelei fatelei commented Mar 12, 2026

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

fix #33340

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

Copilot AI review requested due to automatic review settings March 12, 2026 02:51
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 12, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Dify graph workflow by introducing the capability for LLM nodes to process files as context. This allows for more dynamic and data-rich interactions, where the LLM can directly utilize information from provided files, thereby expanding the range of problems it can address. The changes also include improved error visibility in tool nodes through added logging.

Highlights

  • LLM Node Context: Enabled LLM nodes to accept files as part of their context, allowing for richer data processing.
  • File Handling: Implemented logic to recognize and process both File objects and dictionary representations of files within the context array.
  • Error Logging: Enhanced error handling in tool nodes by adding warning-level logging for various exceptions, improving debuggability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • api/dify_graph/nodes/llm/node.py
    • Imported maybe_file_object from dify_graph.file.constants and build_from_mapping from factories.file_factory.
    • Modified the _fetch_context method to identify and append File objects or file mapping dictionaries to context_files.
  • api/dify_graph/nodes/tool/tool_node.py
    • Added logging import and initialized a logger for the module.
    • Introduced logger.warning(e, exc_info=True) calls within exception blocks for ToolNodeError, ToolInvokeError, and PluginInvokeError to log errors more effectively.
  • api/tests/unit_tests/core/workflow/nodes/llm/test_node.py
    • Added test_fetch_context_accepts_file_objects_as_context_files to verify that _fetch_context correctly handles File objects in the context array.
    • Added test_fetch_context_accepts_file_dicts_as_context_files to ensure _fetch_context can process file mapping dictionaries as context.
Activity
  • The author, fatelei, initiated this pull request to implement support for files as context.
  • The pull request addresses issue support files as context #33340.
  • The author has completed the necessary checklist items, including adding tests and updating documentation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the 💪 enhancement New feature or request label Mar 12, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for using files as context in LLM nodes, a valuable enhancement. The core logic in llm/node.py appears correct, and the new tests in test_node.py provide good coverage for this functionality. However, I've identified a critical syntax error in the test file due to incorrect indentation that needs to be addressed. Additionally, there are a few opportunities for minor improvements, such as removing redundant checks and updating test decorators now that the feature is implemented. The logging enhancements in tool_node.py are also a welcome addition for better observability.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to add support for using uploaded/attached files as “context” for the LLM node (so context variables can include file objects/dicts, not only text / retrieval chunks), addressing issue #33340.

Changes:

  • Extend LLMNode._fetch_context() to treat File objects and dify_model_identity file dicts inside context arrays as context_files.
  • Add unit tests intended to cover the new _fetch_context behavior for file objects and file dicts.
  • Add warning-level exception logging in ToolNode error paths.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
api/dify_graph/nodes/llm/node.py Adds handling for File objects and file-like dicts when building LLM context.
api/tests/unit_tests/core/workflow/nodes/llm/test_node.py Adds tests for _fetch_context accepting files in context arrays (currently marked xfail and with structural issues).
api/dify_graph/nodes/tool/tool_node.py Adds logger.warning(..., exc_info=True) in several exception handlers for better diagnosability.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-12 03:33:09.261578592 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-12 03:33:00.699537594 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4495,6 +4495,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

Copilot AI review requested due to automatic review settings March 12, 2026 04:31
@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-12 04:32:56.682624191 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-12 04:32:47.515651143 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4735,6 +4735,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

@dosubot dosubot 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 Mar 12, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Mar 12, 2026
@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-12 14:14:05.157013624 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-12 14:13:56.391108236 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:893:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:910:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4911,6 +4911,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

Copilot AI review requested due to automatic review settings March 13, 2026 01:32
@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-13 01:33:06.619371501 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-13 01:32:57.334319137 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:894:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:894:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:911:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:911:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4911,6 +4911,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-13 01:35:24.341715579 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-13 01:35:14.452869574 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:892:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:892:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:909:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:909:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4911,6 +4911,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

@github-actions
Copy link
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-13 01:35:55.567512520 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-13 01:35:46.459424979 +0000
@@ -488,13 +488,13 @@
 ERROR Object of class `BaseOxmlElement` has no attribute `body` [missing-attribute]
    --> dify_graph/nodes/document_extractor/node.py:446:19
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:892:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:881:65
+   --> dify_graph/nodes/llm/node.py:892:65
 ERROR `+` is not supported between `list[Unknown]` and `str` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:909:65
 ERROR `+` is not supported between `list[Unknown]` and `None` [unsupported-operation]
-   --> dify_graph/nodes/llm/node.py:898:65
+   --> dify_graph/nodes/llm/node.py:909:65
 ERROR Expected a callable, got `Literal[SegmentType.get_zero_value]` [not-callable]
   --> dify_graph/nodes/variable_assigner/v1/node.py:92:32
 ERROR `updated_variable` may be uninitialized [unbound-name]
@@ -4911,6 +4911,10 @@
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:680:9
 ERROR Object of class `FunctionType` has no attribute `assert_called_once_with` [missing-attribute]
    --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:711:9
+ERROR `tuple[Literal['nodeA'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:794:52
+ERROR `tuple[Literal['nodeB'], Literal['ctx']]` is not assignable to attribute `variable_selector` with type `list[str] | None` [bad-assignment]
+   --> tests/unit_tests/core/workflow/nodes/llm/test_node.py:827:52
 ERROR Object of class `RetryConfig` has no attribute `enabled` [missing-attribute]
   --> tests/unit_tests/core/workflow/nodes/template_transform/entities_spec.py:92:16
 ERROR `dict[str, list[Unknown] | str]` is not assignable to TypedDict key `data` with type `BaseNodeData` [bad-typed-dict-key]

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support files as context

2 participants