Serialize upstream eval JSON directly to bytes - #1802
Merged
Conversation
Contributor
ApprovabilityVerdict: Approved Small, self-contained change that switches to pydantic_core's JSON serialization for better performance and adds graceful handling of inf/nan values (converting to null). The scope is limited to the eval client's HTTP layer with clear defensive intent. You can customize Macroscope's approvability policy. Learn more. |
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.
Overview
Serialize V1 eval upstream requests and responses directly as JSON bytes, avoiding the full intermediate strings created by HTTPX's stdlib JSON helpers.
Why
HTTPX's
json=bodypath serializes the complete body to a Pythonstrand then encodes it to UTF-8 bytes. Large prompts, tool output, and base64 content therefore create another payload-sized object and synchronously block the event loop before the request is sent.Likewise,
Response.json()materializes a decoded JSON source before constructing the returned object, adding another full-payload allocation and synchronous stall after the provider response arrives.Implementation
pydantic_core.to_json(..., inf_nan_mode="null")and pass those bytes through HTTPX'scontent=path.null, avoiding invalidNaN/Infinityconstants without a separate traversal or fallback serialization.resp.contentwithpydantic_core.from_json.Performance
Measured on CPython 3.13.12 with a 32 MiB JSON payload, two warmups, seven timed loops,
time.perf_counter()for synchronous/event-loop stall, andtracemallocfor peak traced allocation:The request and response wire sizes are unchanged; the savings come from removing one full-payload transient materialization in each direction.
Note
Low Risk
Localized transport change in the eval relay client with equivalent wire semantics aside from NaN/Infinity serialized as null; streaming and auth paths are untouched.
Overview
EvalClientnow encodes upstream POST bodies withpydantic_core.to_jsonand sends them through HTTPX’scontent=path instead ofjson=, and parses non-streaming provider JSON fromresp.contentviafrom_json(model completions andrelay_aux).Request encoding sets
content-type: application/jsonexplicitly and usesinf_nan_mode="null"so non-finite floats become standard JSONnullrather than invalid literals. SSE streaming, header/auth routing, dialect validation, and error handling are unchanged; only the encode/decode path is swapped for lower allocation and event-loop blocking on large payloads.Reviewed by Cursor Bugbot for commit e131149. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Serialize upstream eval JSON directly to bytes using
pydantic_corehttpxResponse.json()withpydantic_core.from_jsonon raw response bytes inget_responseandrelay_auxin eval.py.httpx'sjson=parameter withpydantic_core.to_json(viacontent=) for request serialization, ensuringNaN/Infinityfloat values are serialized asnull.content-type: application/jsonautomatically on outgoing requests if not already present.NaNorInfinitynow serialize asnullinstead of producing invalid JSON.Macroscope summarized e131149.