[PR #4834] [Engine] Anthropic errors on /v1/messages use Anthropic's error shape, so the Vercel AI SDK shows Strata's message #5204

Open
opened 2026-11-07 16:33:26 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • b0ede64 Anthropic 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/messages and /v1/messages/count_tokens had no top-level type: "error". @ai-sdk/anthropic 4.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:

  • Before: AI_APICallError status 400 message: "Bad Request"
  • After: 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-id header 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 a request_id. It also says: "Every API response includes a unique request-id header."

Who this helps: people who point @ai-sdk/anthropic at Strata themselves. The anthropic Python SDK already raised BadRequestError with 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.

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 adds not_found_error on its own /v1/systemone surface, 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 _post reads it, so /v1/v1/..., a trailing slash and ?beta=true are covered. Other keys in the error, such as error.code, stay.

error.type follows the status rows on the errors page:

Status error.type now Before
400, 401, 429 invalid_request_error, authentication_error, rate_limit_error same
403 permission_error forbidden
404 (only a GET on these paths) not_found_error no type
409 (not reached in normal use) conflict_error model_busy
413 request_too_large invalid_request_error
415 and other 4xx invalid_request_error 415 had no type
500 (not reached in normal use) api_error no site sends one on these paths
502, 503 keep their type; the page has no row same

A 503 is never called overloaded_error. The Claude Code 2.1.316 binary has the check e.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 type fixes 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 c62d5e3a plus b0ede643. 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 class ErrorEnvelope. One uses the real anthropic SDK (2.3.0); it is skipped if the package is missing.

  • The same tests with serve/server.py from 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-After header when request-id is 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 are jsonschema 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/messages with and without stream and tools, /v1/v1/messages, count_tokens success, /v1/chat/completions, /v1/responses, 400s on chat and responses, 404s on /v1/messages/batches and /v1/v1/chat/completions, GET /v1/models, GET /v1/nope. 2 differ, as intended: a 400 on /v1/messages and a 400 on /v1/messages/count_tokens.

  • Local script, not in the repo: @ai-sdk/anthropic 4.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.

  • anthropic SDK, base and branch: BadRequestError on both. e.request_id was None on the base and req_<12 hex> on the branch. On success _request_id stays None.

  • 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. anthropic SDK 2.3.0 and @ai-sdk/anthropic 4.0.94. Every run of a row gave the same result:

    Real condition Runs, base / branch Base Branch
    @ai-sdk/anthropic, overflow (150012 tokens), with and without stream 1 / 1 each status 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, GpuBusy 503, with and without stream 1 / 1 each status 503 message: "Service Unavailable" status 503 message: "the GPU is in use by another program: ...", Retry-After: 15, isRetryable true on both
    anthropic SDK, overflow 4 / 6, plus 1 / 3 with stream BadRequestError, e.request_id None, no top-level type, no header BadRequestError, top-level type: "error", and e.request_id, body request_id and the request-id header are equal
    anthropic SDK, 400 validation: stop_sequences, temperature, block type 1 / 3 each, temperature 1 / 4 as above, same message as above, same message
    anthropic SDK, GpuBusy 503 1 / 3 InternalServerError, server_error, e.request_id None InternalServerError, server_error, ids equal
    Raw GpuBusy 503, without and with stream 1 / 3 and 1 / 1 Retry-After: 15, no request-id Retry-After: 15 and request-id; error.type stays server_error
    400 on count_tokens, /v1/v1/messages, a trailing slash, ?beta=true 1 / 1 each, count_tokens block type 1 / 3 no top-level type new body and header
    403 foreign Origin, on both paths 1 / 1 each forbidden permission_error, new body and header
    404 GET, on both paths 1 / 1 each {"error": {"message": "not found"}} not_found_error, new body and header
    413 Content-Length of 10 TB, header only, on both paths 1 / 1 each invalid_request_error request_too_large, new body and header
    415 same Origin, text/plain, on both paths 1 / 1 each no type invalid_request_error, new body and header

    How the rare ones were triggered: GpuBusy by a second program holding 300 MiB of VRAM, with min_free_vram_mib set 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/completions and 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 no request-id.

  • Real engine, success, 1 / 1: /v1/messages with and without stream and count_tokens have the same header names on base and branch, with no request-id. The body keys match, and so do the first three and the last stream events. In the SDK, _request_id stays None. @ai-sdk/anthropic gave 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 is serve.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:

  • On the real engine: a 401 (the servers ran with --no-api-key), a 429 (not tried), and a GpuBusy or StructuredOutputError after a stream has started (I know no way to trigger one without a patch).
  • Other cards, other models and other engine versions.
  • Windows and macOS.
  • CUDA, HIP, SYCL and Metal builds: Python only, no engine file changed.
  • Claude Code against the patched server, so its behaviour on these bodies is not shown.
  • The two serve.test_structured tests that need jsonschema.
  • A real 409, 500 or 502 on these paths: the tests force them by making svc.load raise.

Limits

  • The request-id header 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.
  • Error events inside a stream keep their shape, with no request_id. There is one older problem here. A GpuBusy or StructuredOutputError raised after a stream starts writes a raw HTTP/1.0 503 or 502 block into the 200 stream. A local script showed this on both base and branch by raising them in anthropic_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 a request-id line.
  • /v1/messages/batches is left as it was ({"error": {"message": "not found"}}), outside these two paths.
  • anthropic 2.3.0's ErrorObject list does not include four types sent here: conflict_error and request_too_large, which the errors page lists, and structured_output_failed and server_error, which it does not.
  • A web page cannot read request-id across origins, because the header is not in Access-Control-Expose-Headers. The same id is in the body.
  • The monitor's stored response for these errors now shows the new 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.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4834 **Author:** [@gdevenyi](https://github.com/gdevenyi) **Created:** 10/31/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `api/anthropic-error-envelope` --- ### 📝 Commits (1) - [`b0ede64`](https://github.com/Niko1221/Strata/commit/b0ede6433df72a27bb6c6baade851595c980c491) Anthropic 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) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+16 -0) 📝 `serve/test_anthropic_base_url.py` (+151 -1) </details> ### 📄 Description ## Problem and result Errors on `/v1/messages` and `/v1/messages/count_tokens` had no top-level `type: "error"`. `@ai-sdk/anthropic` 4.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: - Before: `AI_APICallError status 400 message: "Bad Request"` - After: `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-id` header 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 a `request_id`. It also says: "Every API response includes a unique `request-id` header." Who this helps: people who point `@ai-sdk/anthropic` at Strata themselves. The `anthropic` Python SDK already raised `BadRequestError` with 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 adds `not_found_error` on its own `/v1/systemone` surface, 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 `_post` reads it, so `/v1/v1/...`, a trailing slash and `?beta=true` are covered. Other keys in the error, such as `error.code`, stay. `error.type` follows the status rows on the errors page: | Status | `error.type` now | Before | | --- | --- | --- | | 400, 401, 429 | `invalid_request_error`, `authentication_error`, `rate_limit_error` | same | | 403 | `permission_error` | `forbidden` | | 404 (only a `GET` on these paths) | `not_found_error` | no type | | 409 (not reached in normal use) | `conflict_error` | `model_busy` | | 413 | `request_too_large` | `invalid_request_error` | | 415 and other 4xx | `invalid_request_error` | 415 had no type | | 500 (not reached in normal use) | `api_error` | no site sends one on these paths | | 502, 503 | keep their type; the page has no row | same | A 503 is never called `overloaded_error`. The Claude Code 2.1.316 binary has the check `e.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 `type` fixes 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 c62d5e3a plus b0ede643. 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 class `ErrorEnvelope`. One uses the real `anthropic` SDK (2.3.0); it is skipped if the package is missing. - The same tests with `serve/server.py` from 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-After` header when `request-id` is 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 are `jsonschema 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/messages` with and without stream and tools, `/v1/v1/messages`, `count_tokens` success, `/v1/chat/completions`, `/v1/responses`, 400s on chat and responses, 404s on `/v1/messages/batches` and `/v1/v1/chat/completions`, `GET /v1/models`, `GET /v1/nope`. 2 differ, as intended: a 400 on `/v1/messages` and a 400 on `/v1/messages/count_tokens`. - Local script, not in the repo: `@ai-sdk/anthropic` 4.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. - `anthropic` SDK, base and branch: `BadRequestError` on both. `e.request_id` was `None` on the base and `req_<12 hex>` on the branch. On success `_request_id` stays `None`. - 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. `anthropic` SDK 2.3.0 and `@ai-sdk/anthropic` 4.0.94. Every run of a row gave the same result: | Real condition | Runs, base / branch | Base | Branch | | --- | --- | --- | --- | | `@ai-sdk/anthropic`, overflow (150012 tokens), with and without stream | 1 / 1 each | `status 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`, `GpuBusy` 503, with and without stream | 1 / 1 each | `status 503 message: "Service Unavailable"` | `status 503 message: "the GPU is in use by another program: ..."`, `Retry-After: 15`, `isRetryable` true on both | | `anthropic` SDK, overflow | 4 / 6, plus 1 / 3 with stream | `BadRequestError`, `e.request_id` `None`, no top-level `type`, no header | `BadRequestError`, top-level `type: "error"`, and `e.request_id`, body `request_id` and the `request-id` header are equal | | `anthropic` SDK, 400 validation: `stop_sequences`, `temperature`, block type | 1 / 3 each, `temperature` 1 / 4 | as above, same message | as above, same message | | `anthropic` SDK, `GpuBusy` 503 | 1 / 3 | `InternalServerError`, `server_error`, `e.request_id` `None` | `InternalServerError`, `server_error`, ids equal | | Raw `GpuBusy` 503, without and with stream | 1 / 3 and 1 / 1 | `Retry-After: 15`, no `request-id` | `Retry-After: 15` and `request-id`; `error.type` stays `server_error` | | 400 on `count_tokens`, `/v1/v1/messages`, a trailing slash, `?beta=true` | 1 / 1 each, `count_tokens` block type 1 / 3 | no top-level `type` | new body and header | | 403 foreign `Origin`, on both paths | 1 / 1 each | `forbidden` | `permission_error`, new body and header | | 404 `GET`, on both paths | 1 / 1 each | `{"error": {"message": "not found"}}` | `not_found_error`, new body and header | | 413 `Content-Length` of 10 TB, header only, on both paths | 1 / 1 each | `invalid_request_error` | `request_too_large`, new body and header | | 415 same `Origin`, `text/plain`, on both paths | 1 / 1 each | no type | `invalid_request_error`, new body and header | How the rare ones were triggered: `GpuBusy` by a second program holding 300 MiB of VRAM, with `min_free_vram_mib` set 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/completions` and 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 no `request-id`. - Real engine, success, 1 / 1: `/v1/messages` with and without stream and `count_tokens` have the same header names on base and branch, with no `request-id`. The body keys match, and so do the first three and the last stream events. In the SDK, `_request_id` stays `None`. `@ai-sdk/anthropic` gave 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 is `serve.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:** - On the real engine: a 401 (the servers ran with `--no-api-key`), a 429 (not tried), and a `GpuBusy` or `StructuredOutputError` after a stream has started (I know no way to trigger one without a patch). - Other cards, other models and other engine versions. - Windows and macOS. - CUDA, HIP, SYCL and Metal builds: Python only, no engine file changed. - Claude Code against the patched server, so its behaviour on these bodies is not shown. - The two `serve.test_structured` tests that need `jsonschema`. - A real 409, 500 or 502 on these paths: the tests force them by making `svc.load` raise. ## Limits - The `request-id` header 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. - Error events inside a stream keep their shape, with no `request_id`. There is one older problem here. A `GpuBusy` or `StructuredOutputError` raised after a stream starts writes a raw `HTTP/1.0 503` or `502` block into the 200 stream. A local script showed this on both base and branch by raising them in `anthropic_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 a `request-id` line. - `/v1/messages/batches` is left as it was (`{"error": {"message": "not found"}}`), outside these two paths. - `anthropic` 2.3.0's `ErrorObject` list does not include four types sent here: `conflict_error` and `request_too_large`, which the errors page lists, and `structured_output_failed` and `server_error`, which it does not. - A web page cannot read `request-id` across origins, because the header is not in `Access-Control-Expose-Headers`. The same id is in the body. - The monitor's stored response for these errors now shows the new body. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5204
No description provided.