fix: gracefully skip unsupported content blocks in Gemini transformer (#9537)

Co-authored-by: Matt Rubens <mrubens@users.noreply.github.com>
This commit is contained in:
Daniel 2025-11-24 15:14:21 -05:00 committed by GitHub
parent a8b366f3dd
commit 5336246f4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 7 deletions

View file

@ -420,7 +420,7 @@ describe("convertAnthropicMessageToGemini", () => {
)
})
it("should throw an error for unsupported content block type", () => {
it("should skip unsupported content block types", () => {
const anthropicMessage: Anthropic.Messages.MessageParam = {
role: "user",
content: [
@ -428,11 +428,39 @@ describe("convertAnthropicMessageToGemini", () => {
type: "unknown_type", // Unsupported type
data: "some data",
} as any,
{ type: "text", text: "Valid content" },
],
}
expect(() => convertAnthropicMessageToGemini(anthropicMessage)).toThrow(
"Unsupported content block type: unknown_type",
)
const result = convertAnthropicMessageToGemini(anthropicMessage)
expect(result).toEqual([
{
role: "user",
parts: [{ text: "Valid content" }],
},
])
})
it("should skip reasoning content blocks", () => {
const anthropicMessage: Anthropic.Messages.MessageParam = {
role: "assistant",
content: [
{
type: "reasoning" as any,
text: "Let me think about this...",
},
{ type: "text", text: "Here's my answer" },
],
}
const result = convertAnthropicMessageToGemini(anthropicMessage)
expect(result).toEqual([
{
role: "model",
parts: [{ text: "Here's my answer" }],
},
])
})
})

View file

@ -6,7 +6,12 @@ type ThoughtSignatureContentBlock = {
thoughtSignature?: string
}
type ExtendedContentBlockParam = Anthropic.ContentBlockParam | ThoughtSignatureContentBlock
type ReasoningContentBlock = {
type: "reasoning"
text: string
}
type ExtendedContentBlockParam = Anthropic.ContentBlockParam | ThoughtSignatureContentBlock | ReasoningContentBlock
type ExtendedAnthropicContent = string | ExtendedContentBlockParam[]
function isThoughtSignatureContentBlock(block: ExtendedContentBlockParam): block is ThoughtSignatureContentBlock {
@ -124,8 +129,10 @@ export function convertAnthropicContentToGemini(
]
}
default:
// Currently unsupported: "thinking" | "redacted_thinking" | "document"
throw new Error(`Unsupported content block type: ${block.type}`)
// Skip unsupported content block types (e.g., "reasoning", "thinking", "redacted_thinking", "document")
// These are typically metadata from other providers that don't need to be sent to Gemini
console.warn(`Skipping unsupported content block type: ${block.type}`)
return []
}
})
}