fix(ai): retry Gemini MALFORMED_RESPONSE finishes (#56232)

This commit is contained in:
Aiden Cline
2026-10-16 15:05:01 -05:00
committed by GitHub
Unverified
parent bc5ebf00a4
commit 180fd9d622
2 changed files with 15 additions and 1 deletions
+13
View File
@@ -12,6 +12,7 @@ import {
} from "../route/transport/index.js"
import {
AIError,
InvalidProviderOutputError,
LLMEvent,
Usage,
type FinishReason,
@@ -664,6 +665,18 @@ const step = (state: ParserState, event: GeminiEvent | NextRequest) => {
usage: event.usageMetadata ?? state.usage,
finishReason: candidate?.finishReason ?? state.finishReason,
}
// Gemini sporadically ends a valid request with MALFORMED_RESPONSE; continuing or resending it recovers.
if (candidate?.finishReason === "MALFORMED_RESPONSE")
return Effect.fail(
new AIError({
reason: new InvalidProviderOutputError({
route: state.route,
message: `Gemini stopped with ${candidate.finishReason}`,
body: ProviderShared.encodeJson(event),
classification: "incomplete-stream",
}),
}),
)
if (candidate?.finishReason && mapFinishReason(candidate.finishReason, state.hasToolCalls) === "error")
return Effect.fail(
ProviderShared.eventError(
+2 -1
View File
@@ -1,7 +1,7 @@
import { describe, expect } from "bun:test"
import { Deferred, Effect, Fiber } from "effect"
import * as TestClock from "effect/testing/TestClock"
import { LLM, AIError, LLMEvent, LLMRequest, Message, ToolCallPart, ToolDefinition, Usage, Media } from "../../src/index.js"
import { LLM, AIError, LLMEvent, LLMRequest, Message, ToolCallPart, ToolDefinition, Usage, Media, isRetryable } from "../../src/index.js"
import { Auth, LLMClient } from "../../src/route.js"
import { compileRequest } from "../../src/route/client.js"
import * as Gemini from "../../src/protocols/gemini.js"
@@ -1709,6 +1709,7 @@ describe("Gemini route", () => {
reason: { _tag: "InvalidProviderOutput", body: JSON.stringify(event), http: { status: 200 } },
message: `Gemini stopped with ${raw}`,
})
expect(isRetryable(error)).toBe(raw === "MALFORMED_RESPONSE")
}
}),
)