@@ -77,7 +77,22 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
7777 // send thoughtSignature parts back to Gemini.
7878 const includeThoughtSignatures = Boolean ( thinkingConfig ?. thinkingLevel )
7979
80- const contents = messages . map ( ( message ) =>
80+ // The message list can include provider-specific meta entries such as
81+ // `{ type: "reasoning", ... }` that are intended only for providers like
82+ // openai-native. Gemini should never see those; they are not valid
83+ // Anthropic.MessageParam values and will cause failures (e.g. missing
84+ // `content` for the converter). Filter them out here.
85+ type ReasoningMetaLike = { type ?: string }
86+
87+ const geminiMessages = messages . filter ( ( message ) : message is Anthropic . Messages . MessageParam => {
88+ const meta = message as ReasoningMetaLike
89+ if ( meta . type === "reasoning" ) {
90+ return false
91+ }
92+ return true
93+ } )
94+
95+ const contents = geminiMessages . map ( ( message ) =>
8196 convertAnthropicMessageToGemini ( message , { includeThoughtSignatures } ) ,
8297 )
8398
@@ -325,11 +340,13 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
325340 try {
326341 const { id : model } = this . getModel ( )
327342
328- const response = await this . client . models . countTokens ( {
343+ const countTokensRequest = {
329344 model,
330345 // Token counting does not need encrypted continuation; always drop thoughtSignature.
331346 contents : convertAnthropicContentToGemini ( content , { includeThoughtSignatures : false } ) ,
332- } )
347+ }
348+
349+ const response = await this . client . models . countTokens ( countTokensRequest )
333350
334351 if ( response . totalTokens === undefined ) {
335352 console . warn ( "Gemini token counting returned undefined, using fallback" )
0 commit comments