mirror of
https://github.com/langgenius/dify-official-plugins.git
synced 2026-07-21 17:45:23 -04:00
[PR #1488] [MERGED] fix(gemini): dealing with a series of nasty bugs that cause the model to perform poorly #1920
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/langgenius/dify-official-plugins/pull/1488
Author: @QIN2DIM
Created: 8/11/2025
Status: ✅ Merged
Merged: 8/11/2025
Merged by: @crazywoola
Base:
main← Head:gemini-think-parts📝 Commits (8)
752cf3afix(llm): improve Gemini LLM integration methods98cee91Merge branch 'main' into gemini-think-partseecd981fix(llm): restructure gemini content building and message handlingedeaba5Merge branch 'main' into gemini-think-parts394fab1build(manifest): update version to 0.4.08700045feat(gemini-llm): add support for multimodal content handling and grounding sources2b7deecrefactor(llm): remove debug prints from GoogleLargeLanguageModel9c51b72test(llm): add comprehensive tests for multimodal content conversion📊 Changes
4 files changed (+889 additions, -337 deletions)
View changed files
📝
models/gemini/manifest.yaml(+1 -1)📝
models/gemini/models/llm/llm.py(+218 -271)📝
models/gemini/models/llm/test_llm.py(+669 -65)📝
models/gemini/pyproject.toml(+1 -0)📄 Description
Related Issues or Context
This PR addresses several core issues in the integration between Dify Chatflow and the Gemini GenAI SDK.
1.
system_instructionsReplay IssueIn Dify, the
system_promptwas incorrectly parsed asuser_textand inserted into the conversationhistorycontents. This caused the same system prompt to be duplicated in the session state.https://github.com/langgenius/dify-official-plugins/blob/39b6920bf928e44650a75676b0aebaf239fb55e8/models/gemini/models/llm/llm.py#L379-L380 https://github.com/langgenius/dify-official-plugins/blob/39b6920bf928e44650a75676b0aebaf239fb55e8/models/gemini/models/llm/llm.py#L192-L193
Transmit the system prompt via
config.system_instructionin the Gemini configuration instead of injecting it into thehistory.2.
thinkingContent LeakageProblem:
The original flow included reasoning content (e.g.,
<think>xxx</think>) in the conversationcontents.As shown in the first example flow, when Gemini 2.5 Flash is placed after another LLM in the same branch, its content cannot be reliably removed. Although regex could strip matching pairs from raw text, this is risky—without a channel-level structure identifier, the block is indistinguishable from legitimate parts of the final answer.
As shown in the second example flow, when multiple reasoning models operate in different conditional branches (i.e., not sequentially in the same branch), the current method works safely, since reasoning outputs from one model do not enter the conversation state of another.
Fix:
When constructing
contents, attempt to remove reasoning-channel data from memory.3.
function callingRole MislabelingThe Gemini GenAI SDK only supports
userandmodelroles—there is nofunctionrole.model.user.https://googleapis.github.io/python-genai/genai.html#genai.types.Content.role
https://github.com/langgenius/dify-official-plugins/blob/39b6920bf928e44650a75676b0aebaf239fb55e8/models/gemini/models/llm/llm.py#L381-L389
Correct role assignments for ToolCall-related messages.
4. Token Counter Miscalculation
The Gemini GenAI SDK directly returns the total token usage (including reasoning, responses, and tool calls). The correct behavior is to use this total value directly instead of summing it with other counts.
TODO — Some token counting logic is implemented in the lower-level Dify SDK, so this PR does not yet modify that part of the code.
5. Type Hints and SDK Compatibility
The source code contained multiple incorrect Gemini type usages (partly due to SDK version changes, where some types are now deprecated).
Updated all Gemini type references to their correct values to improve IDE autocomplete and AI-assisted coding accuracy.
6. Comprehensive Tests for
contentsHistory StateThe existing test coverage for conversation
contentshistory state was insufficient to ensure correctness across different scenarios.Added a full set of test cases covering various conversation states, message types, and role assignments to ensure
contentsis correctly managed across the conversation lifecycle.This PR contains Changes to Non-Plugin
This PR contains Changes to Non-LLM Models Plugin
This PR contains Changes to LLM Models Plugin
Version Control (Any Changes to the Plugin Will Require Bumping the Version)
VersionField, Not in Meta Section)Dify Plugin SDK Version
dify_plugin>=0.3.0,<0.5.0is in requirements.txt (SDK docs)Environment Verification (If Any Code Changes)
Local Deployment Environment
SaaS Environment
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.