fix(ai): preserve string error payloads in Google Interactions (#60823)

This commit is contained in:
Aiden Cline
2026-10-31 02:04:24 -05:00
committed by GitHub
Unverified
parent 956e41fd17
commit 4592000085
2 changed files with 29 additions and 4 deletions
@@ -1,4 +1,4 @@
import { Effect, Schema, SchemaGetter } from "effect"
import { Effect, Option, Schema, SchemaGetter } from "effect"
import { Route } from "../route/client.js"
import { Auth } from "../route/auth.js"
import { Endpoint } from "../route/endpoint.js"
@@ -204,7 +204,7 @@ const Event = Schema.Union([
status: Schema.String,
}),
Schema.Struct({ event_type: Schema.Literal("interaction.completed"), interaction: Interaction }),
Schema.Struct({ event_type: Schema.Literal("error"), error: lenient(ErrorDetail) }),
Schema.Struct({ event_type: Schema.Literal("error"), error: Schema.optionalKey(Schema.Unknown) }),
UnknownEvent,
])
type Event = typeof Event.Type
@@ -550,11 +550,12 @@ const step = Effect.fnUntraced(function* (state: ParserState, event: Event) {
return [state, []] satisfies StepResult
case "error": {
const rawBody = encodeJson(event)
const detail = Option.getOrUndefined(Schema.decodeUnknownOption(ErrorDetail)(event.error))
return yield* new AIError({
reason: failureReason(
state.route,
event.error?.code ?? event.error?.error_type,
event.error?.message?.trim() || providerErrorMessage(rawBody) || "Google Interactions stream error",
detail?.code ?? detail?.error_type,
detail?.message?.trim() || providerErrorMessage(rawBody) || "Google Interactions stream error",
event.error,
rawBody,
),
@@ -134,6 +134,30 @@ describe("Google Interactions", () => {
}),
)
it.effect("classifies plain-string stream error payloads by their message", () =>
Effect.gen(function* () {
const rateLimited = { event_type: "error", error: "Rate limit exceeded, too many requests" }
const rateLimitError = yield* generate(rateLimited).pipe(Effect.flip)
expect(rateLimitError.reason).toMatchObject({
_tag: "RateLimit",
message: "Rate limit exceeded, too many requests",
body: JSON.stringify(rateLimited),
})
const overflow = { event_type: "error", error: "prompt is too long" }
const overflowError = yield* generate(overflow).pipe(Effect.flip)
expect(overflowError.reason).toMatchObject({
_tag: "InvalidRequest",
message: "prompt is too long",
classification: "context-overflow",
body: JSON.stringify(overflow),
})
const bare = yield* generate({ event_type: "error" }).pipe(Effect.flip)
expect(bare.reason).toMatchObject({ _tag: "UnknownProvider", message: "Google Interactions stream error" })
}),
)
it.effect("classifies blocked Interactions generations as non-retryable content policy errors", () =>
Effect.gen(function* () {
const codes = [