Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,29 @@ function _convertLangChainContentToPart(content: MessageContentComplex, isMultim
}
} else if ('functionCall' in content) {
return undefined
} else if (content.type === 'thinking') {
// Gemini's "thinking" / "thought summary" parts. Created by the
// response parsers in this file (see the two locations that emit
// `{ type: 'thinking' as const, thinking: p.text, signature: ... }`).
// When the assistant message is echoed back to the API as
// conversation history, the part must be in Google's native
// shape — a regular text Part with `thought: true` (and the
// optional `thoughtSignature` Gemini 3 expects for tool-call
// continuity). Without this branch the converter falls through
// to the throwing else, surfacing as
// "Error in Agent node: Unknown content type thinking" on the
// SECOND iteration of any agent loop running on a thinking
// model (gemini-2.5-* with thinking, gemini-3-flash-preview, …).
//
// Schema reference: https://ai.google.dev/gemini-api/docs/thinking
// Signature reference: https://ai.google.dev/gemini-api/docs/thought-signatures
const text = (content as any).thinking ?? (content as any).text ?? ''
const signature = (content as any).signature ?? (content as any).thoughtSignature
return {
text: typeof text === 'string' ? text : String(text ?? ''),
thought: true,
...(signature ? { thoughtSignature: signature } : {})
} as Part
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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 text and signature fields are of the expected types and throw an error if they are invalid.

        const text = (content as any).thinking ?? (content as any).text
        if (text !== undefined && typeof text !== 'string') {
            throw new Error('Invalid text type received from Google Generative AI API')
        }
        const signature = (content as any).signature ?? (content as any).thoughtSignature
        if (signature !== undefined && typeof signature !== 'string') {
            throw new Error('Invalid signature type received from Google Generative AI API')
        }
        return {
            text: text ?? '',
            thought: true,
            ...(signature ? { thoughtSignature: signature } : {})
        } as Part
References
  1. When handling potentially invalid data from external sources (like an API response), prefer throwing an error for invalid input types rather than silently returning a default or empty value. This promotes fail-fast behavior.

} else {
if ('type' in content) {
throw new Error(`Unknown content type ${content.type}`)
Expand Down