Skip to content

Commit 1188db5

Browse files
committed
fix(llm): surface provider error message from long JSON error bodies
Provider error bodies over 500 characters were dropped from the error message entirely, leaving only "Provider request failed with HTTP 400". Extract the short reason from `error.message`, `error`, `message`, or `detail` so users can see why the provider rejected the request. Refs anomalyco#52042 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018CFR7BogLxVEwTy74UxWUE
1 parent 28e13d9 commit 1188db5

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎packages/llm/src/route/executor.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cause, Context, Effect, Layer, Random } from "effect"
1+
import { Cause, Context, Effect, Layer, Option, Random, Schema } from "effect"
22
import {
33
FetchHttpClient,
44
Headers,
@@ -203,9 +203,29 @@ const responseBody = (body: string | void, request: HttpClientRequest.HttpClient
203203

204204
const providerMessage = (status: number, body: { readonly body?: string }) => {
205205
if (body.body && body.body.length <= 500) return `Provider request failed with HTTP ${status}: ${body.body}`
206+
const detail = body.body ? providerErrorDetail(body.body) : undefined
207+
if (detail) return `Provider request failed with HTTP ${status}: ${detail}`
206208
return `Provider request failed with HTTP ${status}`
207209
}
208210

211+
const decodeJsonBody = Schema.decodeUnknownOption(Schema.UnknownFromJsonString)
212+
213+
// Long provider error bodies are not inlined, but most providers put a short
214+
// human-readable reason under `error.message`, `message`, or `detail`.
215+
const providerErrorDetail = (body: string) => {
216+
const json = Option.getOrUndefined(decodeJsonBody(body))
217+
if (typeof json !== "object" || json === null) return
218+
const error = "error" in json ? json.error : undefined
219+
const candidate = [
220+
typeof error === "object" && error !== null && "message" in error ? error.message : undefined,
221+
typeof error === "string" ? error : undefined,
222+
"message" in json ? json.message : undefined,
223+
"detail" in json ? json.detail : undefined,
224+
].find((value) => typeof value === "string" && value.trim().length > 0)
225+
if (typeof candidate !== "string") return
226+
return candidate.length <= 500 ? candidate : `${candidate.slice(0, 500)}…`
227+
}
228+
209229
const responseHttp = (input: {
210230
readonly request: HttpClientRequest.HttpClientRequest
211231
readonly response: HttpClientResponse.HttpClientResponse

‎packages/llm/test/executor.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ describe("RequestExecutor", () => {
113113
}).pipe(Effect.provide(responsesLayer([new Response("invalid parameter", { status: 400 })]))),
114114
)
115115

116+
it.effect("surfaces the provider error message from long JSON error bodies", () =>
117+
Effect.gen(function* () {
118+
const executor = yield* RequestExecutor.Service
119+
const error = yield* executor.execute(request).pipe(Effect.flip)
120+
121+
expectLLMError(error)
122+
expect(error.reason).toMatchObject({ _tag: "InvalidRequest" })
123+
expect(error.message).toContain(
124+
"Provider request failed with HTTP 400: Image input is not supported for this model",
125+
)
126+
}).pipe(
127+
Effect.provide(
128+
responsesLayer([
129+
new Response(
130+
JSON.stringify({
131+
error: {
132+
message: "Image input is not supported for this model",
133+
type: "invalid_request_error",
134+
trace: "x".repeat(600),
135+
},
136+
}),
137+
{ status: 400 },
138+
),
139+
]),
140+
),
141+
),
142+
)
143+
116144
it.effect("returns redacted diagnostics for retryable rate limits", () =>
117145
Effect.gen(function* () {
118146
const executor = yield* RequestExecutor.Service

0 commit comments

Comments
 (0)