[PR #1567] [MERGED] fix(gemini): filter unsupported file types before uploading to Gemini API #1960

Closed
opened 2026-02-16 11:15:43 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langgenius/dify-official-plugins/pull/1567
Author: @QIN2DIM
Created: 8/23/2025
Status: Merged
Merged: 8/24/2025
Merged by: @crazywoola

Base: mainHead: fix-files-memory


📝 Commits (10+)

  • d10b5e9 refactor(llm): restructure GoogleLargeLanguageModel methods for better organization
  • 22e9ebe fix(llm): filter unsupported file types before uploading to Gemini API
  • 8e602ff fix(gemini): update manifest version to 0.5.1
  • 09d4789 Update llm.py
  • fdf327f test(gemini): add comprehensive document filtering tests for Gemini API
  • 1cf232d fix(gemini): resolve file memory issues and update test configurations
  • 1c80c3c fix(test): update file URI assertion in document filtering test
  • 900d33f docs(tests): add comprehensive test instructions for gemini document filtering
  • b0c7470 fix(models): resolve file memory issues and rename test class
  • 66d13d1 fix(gemini): update uv command in claude settings

📊 Changes

9 files changed (+1256 additions, -180 deletions)

View changed files

📝 models/gemini/.claude/settings.local.json (+1 -1)
📝 models/gemini/manifest.yaml (+1 -1)
📝 models/gemini/models/llm/llm.py (+179 -161)
📝 models/gemini/models/llm/utils.py (+42 -0)
models/gemini/models/tests/TEST_INSTRUCTIONS.md (+131 -0)
models/gemini/models/tests/test_document_filtering.py (+814 -0)
📝 models/gemini/models/tests/test_feature_compatibility.py (+44 -6)
📝 models/gemini/models/tests/test_llm.py (+11 -11)
📝 models/gemini/pyproject.toml (+33 -0)

📄 Description

Related Issues or Context

Title: feat: Enhance Document Filtering and Add Comprehensive Tests for Gemini API

Description

This pull request addresses a significant memory and performance issue by introducing a robust pre-upload filtering mechanism for documents sent to the Gemini API. It prevents unsupported file types (e.g., Microsoft Office, OpenDocument formats) from being uploaded, which reduces unnecessary API calls, prevents potential errors, and improves overall efficiency.

To ensure the reliability of this new functionality, a complete and comprehensive test suite has been added. Additionally, the core GoogleLargeLanguageModel class has been refactored for better code clarity and maintainability.

Key Changes:

  • 📝 Document Filtering: Implemented logic to filter unsupported documents based on MIME types and file extensions before they are sent to the Gemini API.
  • Comprehensive Testing: Introduced a new test suite that includes:
    • Unit tests for supported/unsupported document types, MIME type filtering, and extension handling.
    • Integration tests making real API calls to verify end-to-end functionality for valid documents (PDF, TXT, HTML, etc.).
    • An in-memory file cache (MemoryFileCache) to enable efficient testing without disk persistence.
    • Verification of memory management, file caching, and cost-optimized API calls during tests.
  • 🧹 Code Refactoring: Restructured and reordered methods within the GoogleLargeLanguageModel class to improve the logical flow, making the code easier to understand and maintain. This change is purely internal and does not affect the external API.
  • ⬆️ Version Bump: Updated the Gemini plugin manifest to version 0.5.1 to incorporate these fixes.
Chatflow before after
PixPin_2025-08-23_20-25-15
PixPin_2025-08-23_20-23-53
PixPin_2025-08-23_20-28-00

This PR contains Changes to Non-Plugin

  • Documentation
  • Other

This PR contains Changes to Non-LLM Models Plugin

  • I have Run Comprehensive Tests Relevant to My Changes

This PR contains Changes to LLM Models Plugin

  • My Changes Affect Message Flow Handling (System Messages and User→Assistant Turn-Taking)
  • My Changes Affect Tool Interaction Flow (Multi-Round Usage and Output Handling, for both Agent App and Agent Node)
  • My Changes Affect Multimodal Input Handling (Images, PDFs, Audio, Video, etc.)
  • My Changes Affect Multimodal Output Generation (Images, Audio, Video, etc.)
  • My Changes Affect Structured Output Format (JSON, XML, etc.)
  • My Changes Affect Token Consumption Metrics
  • My Changes Affect Other LLM Functionalities (Reasoning Process, Grounding, Prompt Caching, etc.)
  • Other Changes (Add New Models, Fix Model Parameters etc.)

Version Control (Any Changes to the Plugin Will Require Bumping the Version)

  • I have Bumped Up the Version in Manifest.yaml (Top-Level Version Field, Not in Meta Section)

Dify Plugin SDK Version

  • I have Ensured dify_plugin>=0.3.0,<0.5.0 is in requirements.txt (SDK docs)

Environment Verification (If Any Code Changes)

Local Deployment Environment

  • Dify Version is: , I have Tested My Changes on Local Deployment Dify with a Clean Environment That Matches the Production Configuration.

SaaS Environment

  • I have Tested My Changes on cloud.dify.ai with a Clean Environment That Matches the Production Configuration

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langgenius/dify-official-plugins/pull/1567 **Author:** [@QIN2DIM](https://github.com/QIN2DIM) **Created:** 8/23/2025 **Status:** ✅ Merged **Merged:** 8/24/2025 **Merged by:** [@crazywoola](https://github.com/crazywoola) **Base:** `main` ← **Head:** `fix-files-memory` --- ### 📝 Commits (10+) - [`d10b5e9`](https://github.com/langgenius/dify-official-plugins/commit/d10b5e925a1ec1fbc8b5fa9480d3f9570aaffbf9) refactor(llm): restructure GoogleLargeLanguageModel methods for better organization - [`22e9ebe`](https://github.com/langgenius/dify-official-plugins/commit/22e9ebe3c3fa2889b0d459bb57e077e8e35ece43) fix(llm): filter unsupported file types before uploading to Gemini API - [`8e602ff`](https://github.com/langgenius/dify-official-plugins/commit/8e602ffd87aa0e711b60683122d841229c86dfc1) fix(gemini): update manifest version to 0.5.1 - [`09d4789`](https://github.com/langgenius/dify-official-plugins/commit/09d4789a98487072e3a7247716dbd43d7effdf7e) Update llm.py - [`fdf327f`](https://github.com/langgenius/dify-official-plugins/commit/fdf327f4a8820a52d09349f5c1a683983506e82c) test(gemini): add comprehensive document filtering tests for Gemini API - [`1cf232d`](https://github.com/langgenius/dify-official-plugins/commit/1cf232d2173451e684b03dc8268d872bf6489534) fix(gemini): resolve file memory issues and update test configurations - [`1c80c3c`](https://github.com/langgenius/dify-official-plugins/commit/1c80c3c8a4e3f488af7f1016e3179b8624d1eff7) fix(test): update file URI assertion in document filtering test - [`900d33f`](https://github.com/langgenius/dify-official-plugins/commit/900d33f411fad9cd428fb070b26dc23a3474288e) docs(tests): add comprehensive test instructions for gemini document filtering - [`b0c7470`](https://github.com/langgenius/dify-official-plugins/commit/b0c7470e3a1832f30e35d50c4bd9293a751c8252) fix(models): resolve file memory issues and rename test class - [`66d13d1`](https://github.com/langgenius/dify-official-plugins/commit/66d13d1bad4d7e73d5ecb2ddd190bdf1f00005c8) fix(gemini): update uv command in claude settings ### 📊 Changes **9 files changed** (+1256 additions, -180 deletions) <details> <summary>View changed files</summary> 📝 `models/gemini/.claude/settings.local.json` (+1 -1) 📝 `models/gemini/manifest.yaml` (+1 -1) 📝 `models/gemini/models/llm/llm.py` (+179 -161) 📝 `models/gemini/models/llm/utils.py` (+42 -0) ➕ `models/gemini/models/tests/TEST_INSTRUCTIONS.md` (+131 -0) ➕ `models/gemini/models/tests/test_document_filtering.py` (+814 -0) 📝 `models/gemini/models/tests/test_feature_compatibility.py` (+44 -6) 📝 `models/gemini/models/tests/test_llm.py` (+11 -11) 📝 `models/gemini/pyproject.toml` (+33 -0) </details> ### 📄 Description ## Related Issues or Context <!-- ⚠️ NOTE: This repository is for Dify Official Plugins only. For community contributions, please submit to https://github.com/langgenius/dify-plugins instead. - Link Related Issues if Applicable: #issue_number - Or Provide Context about Why this Change is Needed --> ### Title: feat: Enhance Document Filtering and Add Comprehensive Tests for Gemini API ### Description This pull request addresses a significant memory and performance issue by introducing a robust pre-upload filtering mechanism for documents sent to the Gemini API. It prevents unsupported file types (e.g., Microsoft Office, OpenDocument formats) from being uploaded, which reduces unnecessary API calls, prevents potential errors, and improves overall efficiency. To ensure the reliability of this new functionality, a complete and comprehensive test suite has been added. Additionally, the core `GoogleLargeLanguageModel` class has been refactored for better code clarity and maintainability. #### Key Changes: * **📝 Document Filtering:** Implemented logic to filter unsupported documents based on MIME types and file extensions before they are sent to the Gemini API. * **✅ Comprehensive Testing:** Introduced a new test suite that includes: * Unit tests for supported/unsupported document types, MIME type filtering, and extension handling. * Integration tests making real API calls to verify end-to-end functionality for valid documents (PDF, TXT, HTML, etc.). * An in-memory file cache (`MemoryFileCache`) to enable efficient testing without disk persistence. * Verification of memory management, file caching, and cost-optimized API calls during tests. * **🧹 Code Refactoring:** Restructured and reordered methods within the `GoogleLargeLanguageModel` class to improve the logical flow, making the code easier to understand and maintain. This change is purely internal and does not affect the external API. * **⬆️ Version Bump:** Updated the Gemini plugin manifest to version `0.5.1` to incorporate these fixes. | Chatflow | before | after | | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | <img alt="PixPin_2025-08-23_20-25-15" src="https://github.com/user-attachments/assets/7eebf67a-b8c9-4e5e-a80f-834069fc6405" /> | | | | | <img alt="PixPin_2025-08-23_20-23-53" src="https://github.com/user-attachments/assets/5ec45cb4-5275-40a6-a238-106239731402" /> | | | | | <img alt="PixPin_2025-08-23_20-28-00" src="https://github.com/user-attachments/assets/80c97de7-b872-4fe1-8980-ccd057454f03" /> | ## This PR contains Changes to *Non-Plugin* <!-- Put an `x` in all the boxes that apply by replacing [ ] with [x] For example: - [x] Documentation --> - [ ] Documentation - [ ] Other ## This PR contains Changes to *Non-LLM Models Plugin* - [x] I have Run Comprehensive Tests Relevant to My Changes <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> ## This PR contains Changes to *LLM Models Plugin* <!-- LLM Models Test Example: --> <!-- https://github.com/langgenius/dify-official-plugins/blob/main/.assets/test-examples/llm-plugin-tests/llm_test_example.md --> - [x] My Changes Affect Message Flow Handling (System Messages and User→Assistant Turn-Taking) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] My Changes Affect Tool Interaction Flow (Multi-Round Usage and Output Handling, for both Agent App and Agent Node) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [x] My Changes Affect Multimodal Input Handling (Images, PDFs, Audio, Video, etc.) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] My Changes Affect Multimodal Output Generation (Images, Audio, Video, etc.) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] My Changes Affect Structured Output Format (JSON, XML, etc.) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] My Changes Affect Token Consumption Metrics <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] My Changes Affect Other LLM Functionalities (Reasoning Process, Grounding, Prompt Caching, etc.) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> - [ ] Other Changes (Add New Models, Fix Model Parameters etc.) <!-- 📷 Include Screenshots/Videos Demonstrating the Fix, New Feature, or the Behavior Before/After Breaking Changes. --> ## Version Control (Any Changes to the Plugin Will Require Bumping the Version) - [x] I have Bumped Up the Version in Manifest.yaml (Top-Level `Version` Field, Not in Meta Section) <!-- ⚠️ NOTE: Version Format: MAJOR.MINOR.PATCH - MAJOR (0.x.x): Reserved for Significant architectural changes or incompatible API modifications - MINOR (x.0.x): For New feature additions while maintaining backward compatibility - PATCH (x.x.0): For Backward-compatible bug fixes and minor improvements - Note: Each Version Component (MAJOR, MINOR, PATCH) Can Be 2 Digits, e.g., 10.11.22 --> ## Dify Plugin SDK Version - [x] I have Ensured `dify_plugin>=0.3.0,<0.5.0` is in requirements.txt ([SDK docs](https://github.com/langgenius/dify-plugin-sdks/blob/main/python/README.md)) ## Environment Verification (If Any Code Changes) <!-- ⚠️ NOTE: At Least One Environment Must Be Tested. --> ### Local Deployment Environment - [x] Dify Version is: <!-- Specify Your Version (e.g., 1.2.0) -->, I have Tested My Changes on Local Deployment Dify with a Clean Environment That Matches the Production Configuration. <!-- - Python Virtual Env Matching Manifest.yaml & requirements.txt - No Breaking Changes in Dify That May Affect the Testing Result --> ### SaaS Environment - [x] I have Tested My Changes on cloud.dify.ai with a Clean Environment That Matches the Production Configuration <!-- - Python Virtual Env Matching Manifest.yaml & requirements.txt --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 11:15:43 -05:00
yindo closed this issue 2026-02-16 11:15:43 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#1960