Repository navigation
Stop rejecting valid non-ASCII Anthropic batches as too large - #74662
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 14, 2026 04:56
henry3260
requested review from
Lee-W,
ashb,
gopidesupavan and
kaxil
as code owners
October 14, 2026 04:56
kaxil
approved these changes
Oct 14, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
AnthropicBatchAdapter.validate_requestsrefuses a batch whose serialized payload exceedsAnthropic's 256 MB limit. It measured that payload with
len(json.dumps(item)), which countsthe characters of the ASCII-escaped form.
The adapter submits the requests inline (
messages.batches.create(requests=...)), so whattravels 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
\uXXXXescape;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 Englishbatch of the same content passes.
OpenAIBatchAdapteris not affected: it builds the uploaded JSONL file with the sameASCII-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 inUTF-8 bytes, through a
_utf8_sizehelper that follows the conventionutils/query_results.py::_sizealready documents. ASCII-only input is unaffected.providers/common/ai/tests/.../batch/test_anthropic.py:TestPayloadSizeEstimatepins theestimate to the bytes the inline
batches.createbody actually carries, parametrized overASCII / 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?