Skip to content

Stop rejecting valid non-ASCII Anthropic batches as too large - #74662

Merged
kaxil merged 5 commits into
apache:mainfrom
henry3260:fix-anthropic-batch-payload-bytes
Oct 14, 2026
Merged

kaxil merged 5 commits into
apache:mainfrom
henry3260:fix-anthropic-batch-payload-bytes

Conversation

@henry3260

@henry3260 henry3260 commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Why

AnthropicBatchAdapter.validate_requests refuses a batch whose serialized payload exceeds
Anthropic's 256 MB limit. It measured that payload with len(json.dumps(item)), which counts
the characters of the ASCII-escaped form.

The adapter submits the requests inline (messages.batches.create(requests=...)), so what
travels is httpx's encoding: ensure_ascii=False, then UTF-8 (httpx._content.encode_json).
A CJK character costs three bytes on the wire but was charged the six of its \uXXXX escape;
a non-BMP character costs four but was charged twelve.

So a non-ASCII batch that is well inside the limit is rejected locally and never reaches the
API, and the byte count in the error is up to three times the real payload — which makes the
.expand() split the message suggests sized against a number that does not exist. An English
batch of the same content passes.

OpenAIBatchAdapter is not affected: it builds the uploaded JSONL file with the same
ASCII-escaped defaults it measures, so its estimate matches its own file byte for byte.

What

  • providers/common/ai/.../batch/anthropic.py: measure the payload and the output directive in
    UTF-8 bytes, through a _utf8_size helper that follows the convention
    utils/query_results.py::_size already documents. ASCII-only input is unaffected.
  • providers/common/ai/tests/.../batch/test_anthropic.py: TestPayloadSizeEstimate pins the
    estimate to the bytes the inline batches.create body actually carries, parametrized over
    ASCII / CJK / non-BMP prompts, and checks the reported directive size for a non-ASCII schema.
    The first commit adds them as strict xfail against today's behaviour; the second drops the
    markers with the fix.

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

The adapter takes the batch inline, so the bytes that count are the ones
httpx encodes with ensure_ascii=False as UTF-8. Sizing the payload with
len(json.dumps(...)) counts \uXXXX escapes instead, inflating CJK input
2x and non-BMP input 3x, so batches the API would accept are rejected
locally and the reported byte count in the error is wrong. The openai
adapter already measures encoded bytes, so the two disagree today.

The expectations are marked strict xfail so the inconsistency is
recorded without changing behaviour; the fix has to drop the markers.
The adapter hands the requests to the SDK inline, so the bytes that count
against Anthropic's 256 MB payload limit are the ones httpx puts on the
wire: ensure_ascii=False, then UTF-8. Sizing the payload with json.dumps
defaults measured the \uXXXX escape instead, charging a CJK character six
bytes rather than three and a non-BMP character twelve rather than four.
A batch the API would have accepted was refused locally, and the byte
count in the error was up to three times the real one, so the suggested
remedy was sized against a number that did not exist.

The openai adapter measures the ASCII-escaped form on purpose, because it
builds the uploaded JSONL file that way itself; only the inline path was
wrong.
The size the adapter quotes back is the number a user sizes their
.expand() split against, so it should be the number that reaches
Anthropic. Dropping the ASCII escaping got most of the way there, but
the json.dumps defaults still spend two bytes of whitespace per
separator, leaving the estimate around 8% above the body the SDK
actually sends -- a gap independent of the characters involved, so the
non-ASCII tests could not see it either.

They could not see it for a second reason: the expectation repeated the
expression under test, which only ever confirms the code agrees with
itself. Deriving it from the SDK's encoder instead makes the comparison
independent, and leaves a tripwire if a future SDK serializes a request
differently.

The serializer is the SDK's own, not httpx's: it dumps the body and
hands httpx the finished bytes, so the docstrings now point at the
encoder that decides the size.
@henry3260
henry3260 marked this pull request as ready for review October 14, 2026 04:56
The limit this pre-check enforces is charged against the body Anthropic
receives, and the requests arrive inside one {"requests": [...]} array
rather than on their own. Summing the requests alone leaves out that
framing and the comma joining each pair -- around 100 KB at the
100,000-request maximum -- so a batch in that window passes here and is
refused by the API, which is the one outcome the pre-check exists to
prevent. openai.py is already charged its JSONL newline the same way.

Nothing caught this earlier because the default separators over-counted
by more than the framing omits, so the net estimate stayed above the real
body until the previous commit made each request exact.
One request cannot tell a running total from an overwrite: adding a
single size to zero reads the same as replacing it, so a loop that
dropped the accumulation would still pass while the limit it enforces
only ever saw the last request. The comma between requests is charged
from the second one on, which with one request means that term never ran
at all -- deleting it left the test green. Two prompts of unequal length
exercise both, the way the OpenAI size test already does.
@kaxil
kaxil merged commit bcbb825 into apache:main Oct 14, 2026
85 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants