mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4834] [Engine] Anthropic errors on /v1/messages use Anthropic's error shape, so the Vercel AI SDK shows Strata's message #5204
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5204
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4834
Author: @gdevenyi
Created: 10/31/2026
Status: 🔄 Open
Base:
main← Head:api/anthropic-error-envelope📝 Commits (1)
b0ede64Anthropic errors: /v1/messages and /v1/messages/count_tokens answer an error in Anthropic's shape (top-level type, error.type by status from the errors page, request_id and a request-id header), so @ai-sdk/anthropic shows Strata's message instead of "Bad Request"; 502 and 503 keep their type, success and the other APIs do not change📊 Changes
2 files changed (+167 additions, -1 deletions)
View changed files
📝
serve/server.py(+16 -0)📝
serve/test_anthropic_base_url.py(+151 -1)📄 Description
Problem and result
Errors on
/v1/messagesand/v1/messages/count_tokenshad no top-leveltype: "error".@ai-sdk/anthropic4.0.94 requires that field. Without it the client falls back to the HTTP status text, so a person sees "Bad Request" and not Strata's explanation.The same too-long request through
@ai-sdk/anthropic, against the mock engine:AI_APICallError status 400 message: "Bad Request"AI_APICallError status 400 message: "request exceeds the context window: prompt (140244 tokens) leaves no room to answer in the context (131072); requests are never truncated"The body is now:
{"type": "error", "error": {"type": "invalid_request_error", "message": "..."}, "request_id": "req_..."}The response also has a
request-idheader with the same value.The spec is platform.claude.com/docs/en/api/errors, read on 2026-10-31. Its example shows the top-level
"type": "error"and arequest_id. It also says: "Every API response includes a uniquerequest-idheader."Who this helps: people who point
@ai-sdk/anthropicat Strata themselves. TheanthropicPython SDK already raisedBadRequestErrorwith the full message. The OpenCode preset uses@ai-sdk/openai-compatible, so no shipped preset reaches the failing path. This is a small real bug.Related discussion
None. An earlier check for this work, on 2026-10-31, searched for "vercel", "AI SDK" and "ai-sdk" and found no issue with this symptom. It also scanned the diffs of the 52 open pull requests that touch
serve/server.py. Only #3643 matched. It addsnot_found_erroron its own/v1/systemonesurface, so it is not a duplicate. I did not repeat those searches for this final revision.Approach
One change, at the top of
_json, for a status of 400 or more on these two paths. The path is read the same way_postreads it, so/v1/v1/..., a trailing slash and?beta=trueare covered. Other keys in the error, such aserror.code, stay.error.typefollows the status rows on the errors page:error.typenowinvalid_request_error,authentication_error,rate_limit_errorpermission_errorforbiddenGETon these paths)not_found_errorconflict_errormodel_busyrequest_too_largeinvalid_request_errorinvalid_request_errorapi_errorA 503 is never called
overloaded_error. The Claude Code 2.1.316 binary has the checke.status===529||(e.message?.includes('"type":"overloaded_error"')??!1), so that name would likely send a 503 down the overload path. I read this in the binary. I did not run that case.The id is
req_plus the request monitor's id when the monitor keeps a record. Otherwise it is a new random id of the same form.Only the top-level
typefixes the Vercel problem.request_id, the header and the type mapping are conformance only: no client I tested needs them. They can be dropped without losing the fix, if you prefer the smaller change.Compatibility: this is a bug fix to error bodies on these two paths, and clients that read those bodies will see the change. Success responses and the other APIs do not change. There is no opt-in flag, because a flag would leave the bug in place by default.
Validation
Revision: base
c62d5e3aplusb0ede643. Linux, Python 3.12.3. The tests and local scripts use the mock engine and no GPU, except the real-engine section under Passed.Passed:
python -m unittest serve.test_anthropic_base_url:Ran 14 tests,OK. Nine are new, in classErrorEnvelope. One uses the realanthropicSDK (2.3.0); it is skipped if the package is missing.The same tests with
serve/server.pyfrom the base:Ran 14 tests,FAILED (failures=16), counting subtests.Each of these edits to the fix makes a test fail: removing the 404, 409 or 500 row, and dropping the
Retry-Afterheader whenrequest-idis added.python -m unittest serve.test_request_hardening serve.test_security serve.test_parallel serve.test_structured serve.test_request_accounting serve.test_monitor:Ran 110 tests,OK (skipped=2). Both skips arejsonschema is not installed.Local script, not in the repo: the same 19 requests to a base server and a branch server, with ids and timestamps normalized. 17 are identical:
/v1/messageswith and without stream and tools,/v1/v1/messages,count_tokenssuccess,/v1/chat/completions,/v1/responses, 400s on chat and responses, 404s on/v1/messages/batchesand/v1/v1/chat/completions,GET /v1/models,GET /v1/nope. 2 differ, as intended: a 400 on/v1/messagesand a 400 on/v1/messages/count_tokens.Local script, not in the repo:
@ai-sdk/anthropic4.0.94 on Node 24.9.0, base and branch. The messages are quoted above. The success call worked on both, with the same usage.anthropicSDK, base and branch:BadRequestErroron both.e.request_idwasNoneon the base andreq_<12 hex>on the branch. On success_request_idstaysNone.Real engine: RTX 5080 16 GB, engine 0.1.55 from release v0.2.9, Qwen3.8-Flash-Next IQ3_S, context 65536,
--no-api-key, 127.0.0.1. Local scripts, not in the repo.anthropicSDK 2.3.0 and@ai-sdk/anthropic4.0.94. Every run of a row gave the same result:@ai-sdk/anthropic, overflow (150012 tokens), with and without streamstatus 400 message: "Bad Request"status 400 message: "request exceeds the context window: prompt (150012 tokens) leaves no room to answer in the context (65536); requests are never truncated"@ai-sdk/anthropic,GpuBusy503, with and without streamstatus 503 message: "Service Unavailable"status 503 message: "the GPU is in use by another program: ...",Retry-After: 15,isRetryabletrue on bothanthropicSDK, overflowBadRequestError,e.request_idNone, no top-leveltype, no headerBadRequestError, top-leveltype: "error", ande.request_id, bodyrequest_idand therequest-idheader are equalanthropicSDK, 400 validation:stop_sequences,temperature, block typetemperature1 / 4anthropicSDK,GpuBusy503InternalServerError,server_error,e.request_idNoneInternalServerError,server_error, ids equalGpuBusy503, without and with streamRetry-After: 15, norequest-idRetry-After: 15andrequest-id;error.typestaysserver_errorcount_tokens,/v1/v1/messages, a trailing slash,?beta=truecount_tokensblock type 1 / 3typeOrigin, on both pathsforbiddenpermission_error, new body and headerGET, on both paths{"error": {"message": "not found"}}not_found_error, new body and headerContent-Lengthof 10 TB, header only, on both pathsinvalid_request_errorrequest_too_large, new body and headerOrigin,text/plain, on both pathsinvalid_request_error, new body and headerHow the rare ones were triggered:
GpuBusyby a second program holding 300 MiB of VRAM, withmin_free_vram_mibset to 15800 in the test config, so the unloaded model could not reload. After that program stopped, the next request reloaded the model and answered 200 on both, in 15 to 18 s.Real engine, other paths:
/v1/messages/batches, the 400 on/v1/chat/completionsand the 400 on/v1/responses(1 / 3 runs), and the 403 on/v1/chat/completions(1 / 1), have the same body on base and branch, with norequest-id.Real engine, success, 1 / 1:
/v1/messageswith and without stream andcount_tokenshave the same header names on base and branch, with norequest-id. The body keys match, and so do the first three and the last stream events. In the SDK,_request_idstaysNone.@ai-sdk/anthropicgave the same text, finish reason and usage on both. These real runs compare names and keys only. The byte comparison is the mock-engine script above.Failed:
python -m unittest serve.test_server serve.test_responses serve.test_anthropic_base_url serve.test_chat_tool_results serve.test_context_overflow serve.test_images:Ran 446 tests,FAILED (failures=1, skipped=1). The one failure isserve.test_server.GpuChoice.test_hip_ordinal(AssertionError: '0,1' != '1,0'). It fails the same way on the base, so this change does not cause it.Not run:
--no-api-key), a 429 (not tried), and aGpuBusyorStructuredOutputErrorafter a stream has started (I know no way to trigger one without a patch).serve.test_structuredtests that needjsonschema.svc.loadraise.Limits
request-idheader is on these error responses only. The page says every API response has one, so success responses still do not meet that sentence. This keeps success responses byte-identical.request_id. There is one older problem here. AGpuBusyorStructuredOutputErrorraised after a stream starts writes a rawHTTP/1.0 503or502block into the 200 stream. A local script showed this on both base and branch by raising them inanthropic_events; I did not get it from a real engine. This change does not fix it. On the branch the block carries the new body and arequest-idline./v1/messages/batchesis left as it was ({"error": {"message": "not found"}}), outside these two paths.anthropic2.3.0'sErrorObjectlist does not include four types sent here:conflict_errorandrequest_too_large, which the errors page lists, andstructured_output_failedandserver_error, which it does not.request-idacross origins, because the header is not inAccess-Control-Expose-Headers. The same id is in the body.🤖 Generated with Claude Code
https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.