fix(parse): run the request transform once in parse() - #3616
Open
rasmusfaber wants to merge 1 commit into
Open
Conversation
ting-hong-shieh
approved these changes
Aug 14, 2026
ting-hong-shieh
left a comment
There was a problem hiding this comment.
Validated at 67ed3cad against main/d9029e3a.
Focused checks:
.venv/bin/pytest -q -n 0 tests/lib/chat/test_completions.py tests/lib/responses/test_responses.py— 24 passed- Ruff lint and format checks on both changed files — passed
- Mypy on both changed files — passed
I also ran a same-input transform snapshot for Chat non-streaming and Responses with stream omitted, false, and true. The union and concrete transforms produced identical canonical output hashes in all four cases. Recursive transform calls dropped from 187 to 94 for Chat and from 211/215 to 105/108/106 for the three Responses cases.
main is still the PR base, so there is no base drift. GitHub currently reports all three workflows as action_required with no jobs created, so CI has not run yet. I did not call the live API; request serialization was validated locally.
No blocking issues found.
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.
Changes being requested
For concurrent usage with large payloads, systems can use a disproportionately long time just in the
_transform_recursive()method. A more optimal implementation (compile the per-type transform plan once, execute without runtime typing introspection) could speed up performance of transform up to 370x. You would probably want to do that yourself, however, and it might conflict with the migration to Castiron.There is a tiny, trivial 2x improvement to be had, however:
The hand-written
parse()helpers pass theCreateParamsunion to the request transform, while the generatedcreate()methods dispatch to the concreteStreaming/NonStreamingtype. For union annotations,_transform_recursivewalks the body once per union member, soparse()requests pay exactly 2x the transform cost ofcreate()requests (measured 1.99-2.01x).Pass the concrete params type instead, mirroring
create().The transformed body is unchanged, we only skip an unnecessary second walk in
_transform_recursive(). Output is verified by differential comparison on large bodies for both APIs.This is the same fix
anthropic-sdk-pythonshipped forMessages.stream()in v0.122.0 (12c744c8, "run the request transform once in messages.stream()").Additional context & links