-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
fix(FlowiseChatGoogleGenerativeAI): handle 'thinking' content type round-trip #6449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
azamiftikhar1000
wants to merge
2
commits into
FlowiseAI:main
Choose a base branch
from
azamiftikhar1000:fix/gemini-thinking-content-roundtrip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
.../components/nodes/chatmodels/ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { AIMessage } from '@langchain/core/messages' | ||
| import { convertMessageContentToParts } from './FlowiseChatGoogleGenerativeAI' | ||
|
|
||
| describe('convertMessageContentToParts — Gemini "thinking" content blocks', () => { | ||
| // Background: When a Gemini model with thinking enabled | ||
| // (gemini-2.5-* with thinkingConfig, gemini-3-flash-preview, etc.) | ||
| // emits a thought summary, Flowise's response parser stores it as a | ||
| // LangChain content block of shape: | ||
| // | ||
| // { type: 'thinking', thinking: 'reasoning text…', signature: '…' } | ||
| // | ||
| // On the NEXT iteration of an agent loop, the assistant message is | ||
| // echoed back to the API as conversation history, and each content | ||
| // block runs through `_convertLangChainContentToPart`. Without a | ||
| // branch for type='thinking', the converter throws | ||
| // "Unknown content type thinking" and the agent node errors out. | ||
| // | ||
| // Google's native shape is a text Part with a boolean `thought: true` | ||
| // flag and an optional `thoughtSignature` for Gemini-3 tool-call | ||
| // signature continuity. See: | ||
| // https://ai.google.dev/gemini-api/docs/thinking | ||
| // https://ai.google.dev/gemini-api/docs/thought-signatures | ||
|
|
||
| it('round-trips a thinking content block back into a Gemini text Part with thought=true', () => { | ||
| const msg = new AIMessage({ | ||
| content: [ | ||
| { type: 'thinking', thinking: 'First I should validate the inputs.' } as any, | ||
| { type: 'text', text: 'OK, validated.' } | ||
| ] | ||
| }) | ||
|
|
||
| const parts = convertMessageContentToParts(msg, false, []) | ||
|
|
||
| const thoughtPart = parts.find((p: any) => p.thought === true) as any | ||
| expect(thoughtPart).toBeDefined() | ||
| expect(thoughtPart.text).toBe('First I should validate the inputs.') | ||
| expect(thoughtPart.thought).toBe(true) | ||
| // No signature in the input → no thoughtSignature in the output | ||
| expect(thoughtPart.thoughtSignature).toBeUndefined() | ||
|
|
||
| const textPart = parts.find((p: any) => p.text === 'OK, validated.' && !p.thought) as any | ||
| expect(textPart).toBeDefined() | ||
| }) | ||
|
|
||
| it('preserves the thoughtSignature for Gemini-3 multi-turn tool-call continuity', () => { | ||
| // signature is stored under `signature` on the LangChain block (see | ||
| // the response parsers' output in FlowiseChatGoogleGenerativeAI.ts); | ||
| // it must be emitted as `thoughtSignature` on the outgoing Part. | ||
| const msg = new AIMessage({ | ||
| content: [ | ||
| { | ||
| type: 'thinking', | ||
| thinking: 'Need to call the search tool.', | ||
| signature: 'abc123-thought-sig' | ||
| } as any | ||
| ] | ||
| }) | ||
|
|
||
| const parts = convertMessageContentToParts(msg, false, []) | ||
| const p = parts[0] as any | ||
| expect(p.thought).toBe(true) | ||
| expect(p.thoughtSignature).toBe('abc123-thought-sig') | ||
| }) | ||
|
|
||
| it('also accepts thoughtSignature on the LangChain block (alternate key name)', () => { | ||
| const msg = new AIMessage({ | ||
| content: [ | ||
| { | ||
| type: 'thinking', | ||
| thinking: 'alt', | ||
| thoughtSignature: 'sig-from-alt-key' | ||
| } as any | ||
| ] | ||
| }) | ||
|
|
||
| const parts = convertMessageContentToParts(msg, false, []) | ||
| const p = parts[0] as any | ||
| expect(p.thought).toBe(true) | ||
| expect(p.thoughtSignature).toBe('sig-from-alt-key') | ||
| }) | ||
|
|
||
| it('coerces non-string thinking payload to a string instead of throwing', () => { | ||
| const msg = new AIMessage({ | ||
| content: [{ type: 'thinking', thinking: null } as any] | ||
| }) | ||
| const parts = convertMessageContentToParts(msg, false, []) | ||
| const p = parts[0] as any | ||
| expect(p.thought).toBe(true) | ||
| expect(typeof p.text).toBe('string') | ||
| }) | ||
|
|
||
| it('still throws "Unknown content type" for truly unrecognized types', () => { | ||
| const msg = new AIMessage({ | ||
| content: [{ type: 'definitely-not-a-real-type', value: 1 } as any] | ||
| }) | ||
| expect(() => convertMessageContentToParts(msg, false, [])).toThrow(/Unknown content type/) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When handling potentially invalid data from external API responses, we should prefer throwing an error for invalid input types rather than silently returning a default/empty value or silently coercing them. This promotes fail-fast behavior. Please validate that the
textandsignaturefields are of the expected types and throw an error if they are invalid.References