Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ rest.post("/v1/infer", async (c) => {
}).catch(() => null)
if (!upstream || !upstream.ok) {
const detail = upstream ? await upstream.text().catch(() => "") : "upstream unreachable"
if (upstream && upstream.status >= 400 && upstream.status < 500) {
// upstream rejected the request itself (unknown model, task not
// served, oversized input) — pass the status + detail through
return errorResponse(upstream.status, "inference_rejected", detail.slice(0, 500))
}
return errorResponse(502, "inference_upstream", detail.slice(0, 500))
}
const result = (await upstream.json()) as Record<string, unknown>
Expand Down
34 changes: 34 additions & 0 deletions test/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ describe("REST models + inference", () => {
expect(missing.status).toBe(404)
})

it("POST /v1/infer passes upstream 4xx status through (not 502)", async () => {
const stubEnv = {
ASSETS: assets,
ML_ENDPOINT: "https://ml-stub.example",
ML_TOKEN: "t",
}
const originalFetch = globalThis.fetch
globalThis.fetch = (async (url: string | URL | Request) => {
if (String(url).includes("ml-stub.example")) {
return new Response(
JSON.stringify({ detail: "model x task translit is not served" }),
{ status: 400, headers: { "content-type": "application/json" } },
)
}
return originalFetch(url as RequestInfo)
}) as typeof fetch
try {
const res = await app.request(
"https://example.org/v1/infer",
{
method: "POST",
headers: JSON_HEADERS,
body: JSON.stringify({ model: "khm-latn-1.0", input: "x" }),
},
stubEnv,
)
expect(res.status).toBe(400)
const body = (await res.json()) as { error: { code: string } }
expect(body.error.code).not.toBe("inference_upstream")
} finally {
globalThis.fetch = originalFetch
}
})

it("POST /v1/infer validates without an upstream configured", async () => {
const unconfigured = await post("/v1/infer", { model: "heb-diac-1.0", input: "x" })
expect(unconfigured.status).toBe(503)
Expand Down
Loading