Skip to content

Defer unused aiohttp imports and speed up request body conversion - #4332

Draft
markstuart-oai wants to merge 4 commits into
mainfrom
perf/defer-aiohttp-transport
Draft

markstuart-oai wants to merge 4 commits into
mainfrom
perf/defer-aiohttp-transport

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Changes being requested

With one tool call and 1,024 mixed input items, this patch has been cutting the full run from 436 to 16.6 ms over mock HTTP. For one input message it has been cutting the warm median from 5.18 to 3.89 ms and the time from launch to the first answer from 1,037 to 941 ms with aiohttp installed and HTTPX2 selected.

The sync and async clients have been walking every alternative in a union of typed dictionaries for every message. Switching between text messages, tool calls and tool results has been repeating many of the same field checks and copies.

  • The converter has been visiting union members in order, sending ordinary dictionaries directly to the field converter and loading the Pydantic dump helper only for model values.
  • For lists of at least eight items, it has been reusing field checks within that list after converting a row entirely with cached type hints. It has been checking new keys for aliases and formats and checking for built-in string keys and values on each fresh copy.
  • The converter has been restarting those checks after nested or custom values. It has been limiting work on short groups of text rows and returning unfamiliar or deeply nested annotations to the existing conversion path.
  • The client has been delaying the optional transport import until construction of an aiohttp client or direct access to AiohttpTransport. Import errors from a broken optional installation have been surfacing on first use.

Additional context & links

In each run the SDK has been making two Responses requests, executing an async tool between replies and checking the returned history and usage. Mixed histories have been interleaving user and assistant messages with earlier function calls and results. The nested case has also been using lists of content parts every 64 items. Serialized request and response bodies and tool effects have been matching main.

Input history Main median (ms) Patch median (ms) Median change [95% interval] p95 change [95% interval]
1 text 5.178 3.887 -24.97% [-25.96, -24.51] -22.56% [-34.11, -17.13]
8 mixed 7.762 4.120 -46.72% [-48.11, -46.06] -48.64% [-51.81, -45.60]
256 mixed with nested content 114.420 8.969 -92.12% [-92.21, -91.88] -93.38% [-93.87, -92.21]
1,024 text 231.936 13.750 -94.08% [-94.16, -93.87] -95.09% [-95.65, -93.35]
1,024 mixed 436.451 16.568 -96.20% [-96.25, -96.10] -96.50% [-96.70, -96.11]

Against the previous draft at c9fe5ad8, the median has been changing by -86.92% [-87.11, -86.77] with 1,024 mixed items and +0.03% [-0.53, +0.57] with one message. For one message the p95 change against that draft has been spanning zero at +3.17% [-0.20, +10.62]. Its cold intervals have also been spanning zero for all five histories. The one-message cold change against main has been measuring -8.39% [-11.33, -6.40].

The benchmark has been rotating main, a second copy of main, the previous draft and this patch through 12 matched process blocks on one CPU. It has been using Agents main at cecd9eb with Codex off, Python 3.12.16, Pydantic 2.14.3, HTTPX2 2.14.0 and aiohttp 3.14.5. Cold has been including process launch, imports, setup and the first answer with warm bytecode and file caches. Warm estimates have been using 128 runs after 100 warmups on the two smallest cases and 24 runs after eight warmups on the others. Intervals have been resampling matched blocks. Both clocks have been excluding network and inference time.

Validation

  • The client has been passing 21,649 tests on Pydantic 2 and 21,633 on Pydantic 1, plus formatting, lint and Ty. The Pydantic 1 suite has been excluding functional tests.
  • Agents main has been passing 12,874 tests with 30 skips, plus formatting, lint, mypy and Pyright.
  • Additional requests have been covering changes to later aliases during date formatting, nested inputs, custom mappings, recursion limits, independent output copies and optional transport failures.

Agents SDK regression check

Both clients have been running against Agents SDK main at cecd9eb with Codex off. The 95% upper bounds have been staying below a 3% slowdown for 81/83 medians and 30/47 p95 estimates. Both uncertain medians have been coming from SQLite writes. Each workload has been keeping its original sample size and percentile rank. The p95 columns have been showing medians of process estimates. All 95% intervals in this description have been covering individual comparisons.

83 median comparisons against client main at f2b2399

Each row has been timing a full batch, request or SDK operation. The original protocol has been using 32 fresh pairs per case, then 64 new pairs for 4 selected rows. Intervals have been resampling whole pairs within the reported cohort. The cold clocks here have been starting after process setup.

Workload Pairs Main (ms) Patch (ms) Change [95% interval]
cold_normal 32 62.9801 62.7648 -0.43% [-2.17, +0.62]
cold_stream 32 258.1837 260.7240 +0.97% [-0.52, +2.95]
ids_17x32769 32 69.6007 68.6539 -1.60% [-3.12, -0.42]
ids_17x32769_stream 32 91.8788 90.1404 -1.08% [-3.60, -0.55]
ids_3x8193 32 5.6734 5.6749 -0.24% [-1.20, +0.36]
ids_3x8193_stream 32 7.9324 7.9529 +0.03% [-1.77, +1.01]
reuse_normal 32 39.1397 38.7885 -0.74% [-2.43, +0.23]
reuse_stream 32 39.9338 39.6206 -0.50% [-2.18, +0.22]
typed_3x257 32 6.2626 6.1689 -0.73% [-1.65, +0.00]
typed_3x257_stream 32 7.0214 6.9933 -0.92% [-1.39, +0.42]
add_tool 32 1.7009 1.7022 -0.03% [-0.32, +0.49]
build_dict_12 32 0.0517 0.0522 +0.88% [-0.04, +2.00]
build_omitted_12 32 0.0696 0.0701 +0.10% [-0.45, +1.50]
build_typed_12 32 0.0389 0.0390 -0.06% [-0.45, +0.41]
con1 32 0.4958 0.4985 -0.09% [-1.04, +1.84]
con32 32 11.9621 11.9533 +0.52% [-1.04, +1.68]
con8 32 2.8215 2.8274 +0.09% [-0.20, +0.50]
diag_32 32 11.9934 11.9098 -0.36% [-1.78, +0.25]
h128 32 1.2398 1.2492 +0.79% [+0.42, +1.31]
handoffs_0 32 0.4966 0.4948 -0.25% [-1.65, +0.80]
handoffs_12 32 0.7154 0.7133 -0.39% [-0.84, +0.39]
history120 32 0.9137 0.9125 +0.18% [-0.44, +0.62]
history2000 32 6.1513 6.1801 +0.55% [-0.06, +0.92]
history720 32 2.6692 2.6982 +0.82% [-0.03, +1.56]
mixed_6 32 0.7147 0.7180 +0.72% [-0.15, +1.07]
nested67_stream16 32 74.7974 74.2482 +0.06% [-1.74, +1.17]
repeat_8 32 0.9951 0.9870 -0.76% [-1.16, -0.19]
retained_16384 32 0.1991 0.1991 -0.05% [-0.42, +0.37]
retained_2048 32 0.0294 0.0294 +0.08% [-0.34, +0.59]
run_dict_6 32 0.7754 0.7760 +0.24% [-0.47, +0.55]
run_omitted_6 32 0.8260 0.8266 +0.12% [-0.37, +0.75]
run_typed_6 32 0.7424 0.7472 +0.57% [+0.13, +1.06]
schemas_24 32 0.8533 0.8527 -0.31% [-0.62, +0.21]
server_1 32 1.7777 1.7701 -0.37% [-0.87, +0.15]
server_1024 32 20.8993 20.8695 +0.14% [-0.89, +1.85]
server_128 32 4.2953 4.3525 +0.61% [-0.67, +2.51]
server_512 32 11.5311 11.5350 +0.10% [-1.81, +1.92]
single_0 32 0.4983 0.4928 -1.73% [-2.13, -0.27]
single_12 32 0.5957 0.5973 +0.49% [-0.11, +1.10]
stream 32 0.7756 0.7745 +0.01% [-0.43, +0.49]
sustained_32 32 11.8185 11.8097 -0.15% [-0.48, +0.39]
time_1024_4096 64 50.5901 50.3154 +0.29% [-1.02, +3.50]
time_16384_2 32 0.4084 0.4066 -0.48% [-0.90, -0.01]
time_2048_2 32 0.0579 0.0581 +0.17% [-0.11, +0.54]
time_2_256 64 5.6505 5.6273 +1.41% [-2.98, +4.99]
time_4096_8 32 0.4339 0.4335 -0.34% [-0.89, +0.58]
tools1 32 1.6093 1.6047 -0.20% [-0.52, +0.25]
tools16 32 19.1635 19.1698 +0.15% [-0.31, +0.48]
tools4 32 4.5718 4.5581 -0.01% [-0.50, +0.40]
warm 32 0.4873 0.4868 +0.18% [-0.82, +0.99]
batch-compare 1x1024 32 21.8606 21.5811 -1.21% [-2.33, +0.39]
batch-compare 2x32 32 3.6991 3.7120 +0.21% [-0.03, +0.75]
bulk-live 12x64 32 16.7161 16.6980 -0.11% [-0.71, +0.30]
bulk-live 1x1024 32 21.2043 21.0842 -0.04% [-1.12, +1.11]
bulk-live 1x2048 32 40.3903 40.5258 -0.04% [-0.87, +0.89]
bulk-live 1x256 32 6.8782 6.8804 -0.06% [-1.12, +0.76]
combo5 4x1024 32 44.5558 44.5394 +0.42% [-0.96, +1.21]
combo5 8x128 32 36.7618 36.7406 +0.11% [-2.21, +0.74]
concurrent5 2x128 64 418.7884 416.4708 -0.46% [-1.12, -0.07]
first-final 1x0 32 1572.4564 1570.5417 -0.04% [-0.58, +0.24]
first-mcp 1x0 32 1575.2964 1567.8495 +0.26% [-0.70, +0.86]
lists-final 1x1024 32 5.2907 4.6132 -13.03% [-13.77, -11.65]
lists-final 8x8192 32 35.4384 31.4177 -10.34% [-11.72, -9.35]
provider-final 1x2048 32 261.4802 49.9023 -80.95% [-81.14, -80.64]
provider-final 1x4096 32 519.4697 96.5048 -81.33% [-81.66, -81.06]
quote-final 1x1 32 1.7338 1.7294 -0.30% [-0.72, +0.06]
quote-final 1x64 32 2.6301 2.6170 -0.53% [-1.09, -0.08]
quote-final 8x16 32 11.6776 11.6335 +0.01% [-0.72, +0.38]
quote-final 8x256 32 37.3121 37.2415 -0.37% [-0.76, +0.07]
quote-final 8x64 32 17.1070 16.9863 -0.44% [-0.98, -0.02]
shape-local normal 12x96 32 35.0943 35.6321 +1.50% [+0.64, +2.17]
shape-local stream 12x96 32 56.9540 58.0758 +1.34% [+0.54, +1.99]
shape-local stream 4x12 32 11.6190 11.7280 +0.85% [-0.62, +1.79]
shape-local stream 8x0 32 14.5184 14.7134 +1.46% [-0.42, +2.22]
tail 4x1024 64 43.8209 43.8400 +0.16% [-0.12, +0.49]
values-final ids 1x1 32 1.6978 1.6976 +0.32% [-0.09, +1.04]
values-final ids 8x8192 32 13.5885 13.0087 -3.57% [-4.50, -2.92]
values-final last 4x512 32 6.2010 6.2078 +0.36% [-0.13, +0.86]
values-final records 8x512 32 11.4809 11.5462 +0.23% [-0.22, +0.92]
values-final typed 16x1024 32 27.7292 27.7440 -0.02% [-0.81, +0.84]
values-final typed 4x128 32 5.1534 5.1572 -0.03% [-0.41, +1.04]
warm-control 1x0 32 1.7348 1.7342 -0.10% [-0.68, +0.37]
warm-control 1x4096 32 35.6950 35.6271 -0.66% [-1.56, +0.45]
Process p95 estimates, including separate confirmations
Workload Pairs Main (ms) Patch (ms) Change [95% interval]
add_tool 32 1.8672 1.8170 -0.73% [-3.39, +0.93]
build_dict_12 32 0.0880 0.0889 +1.85% [-1.04, +4.89]
build_omitted_12 32 0.1103 0.1112 +2.24% [+0.59, +3.53]
build_typed_12 32 0.0682 0.0670 -0.89% [-6.05, +3.14]
con1 32 0.5538 0.5590 +1.09% [-0.10, +2.75]
con32 32 75.2405 78.1110 +0.90% [-2.05, +4.20]
con8 32 3.0515 3.0982 +0.56% [-0.42, +1.01]
diag_32 32 68.6957 67.9333 +2.46% [-3.61, +7.02]
h128 32 1.4876 1.5393 +2.87% [-1.01, +6.30]
handoffs_0 32 0.5680 0.5627 -0.88% [-3.51, +1.83]
handoffs_12 32 0.7612 0.7493 -0.40% [-2.09, +0.65]
history120 32 0.9621 0.9582 -0.47% [-3.77, +2.03]
history2000 32 6.4856 6.5614 +0.86% [-1.61, +2.65]
history720 32 2.8891 2.8493 -0.83% [-4.37, +1.01]
mixed_6 32 0.7556 0.7701 +1.60% [-0.06, +3.53]
repeat_8 32 1.0498 1.0521 -0.93% [-4.88, -0.34]
retained_16384 32 0.2445 0.2501 +1.12% [-2.65, +3.74]
retained_2048 32 0.0322 0.0325 -0.20% [-3.19, +2.78]
run_dict_6 32 0.8146 0.8120 +0.11% [-0.92, +2.96]
run_omitted_6 32 0.8777 0.8759 +0.76% [-1.69, +3.43]
run_typed_6 32 0.7812 0.7819 +1.16% [-0.31, +2.11]
schemas_24 32 0.8964 0.8966 +0.37% [-1.61, +1.45]
server_1 32 2.0470 1.9760 -0.91% [-4.76, +5.01]
single_0 32 0.5588 0.5578 -0.42% [-2.36, +0.76]
single_12 32 0.6356 0.6396 +0.36% [-0.63, +2.57]
stream 32 0.9191 0.9207 -0.25% [-1.36, +1.95]
sustained_32 32 60.7854 61.6744 -0.32% [-2.36, +5.36]
time_16384_2 32 0.4438 0.4425 -0.32% [-1.27, +0.60]
time_2048_2 32 0.0810 0.0812 +0.45% [-1.20, +1.23]
time_4096_8 32 0.4733 0.4739 +0.51% [-0.60, +1.38]
tools1 32 1.6931 1.6875 +0.36% [-1.13, +2.14]
tools16 32 19.8831 19.8620 -0.37% [-1.24, +2.63]
tools4 32 4.8028 4.7780 -0.47% [-3.41, +0.85]
warm 32 0.5440 0.5434 +0.12% [-2.61, +3.92]
batch-compare 2x32 32 3.9273 3.9525 -0.35% [-1.21, +6.79]
bulk-live 12x64 32 17.3631 17.4903 -0.36% [-4.31, +1.14]
bulk-live 1x256 32 7.3877 7.3786 +0.39% [-2.75, +1.84]
quote-final 1x1 32 1.8155 1.8017 -0.46% [-4.55, +1.28]
quote-final 1x64 32 2.7717 2.7314 -0.53% [-3.57, +0.99]
quote-final 8x16 32 12.2429 12.2776 +0.81% [-2.82, +3.09]
quote-final 8x256 32 40.4943 39.4622 +0.49% [-4.07, +2.33]
quote-final 8x64 32 17.7952 17.5108 -0.30% [-3.03, +0.64]
tail 4x1024 32 51.3628 52.3106 +2.47% [-3.92, +10.66]
values-final ids 1x1 32 1.7735 1.7758 -0.20% [-1.27, +1.34]
warm-control 1x0 32 1.8249 1.8239 -0.83% [-2.63, +0.81]
warm-control 1x4096 32 90.8375 90.3871 +0.10% [-2.51, +3.23]
tail 4x1024 64 48.1310 50.5533 +2.37% [-0.46, +7.31]

In separate runs, the upper bounds for both SQLite writes and all 16 distinct p95 cases with wide intervals have been staying below a 3% slowdown. The 17 original p95 estimates with wide intervals have been including separate 32- and 64-pair runs of the 4×1,024 stream. Across 256 fresh pairs per write, the small write has been changing by −0.02% [−1.35%, +1.59%] and the large write by −1.41% [−2.55%, −0.39%].

Across 128 fresh pairs per p95 case, the largest upper bound for the patch has been reaching +2.87% (h128). Two identical copies of main have been giving a wider interval on diag_32 [−0.64%, +3.32%]. Each run has been using the original timing and percentile rules.

Separate comparisons with a second copy of main
Workload Clock Pairs Patch vs main [95% interval] Main vs main [95% interval]
time_1024_4096 p50 256 -1.41% [-2.55, -0.39] -0.59% [-1.28, +0.59]
time_2_256 p50 256 -0.02% [-1.35, +1.59] -0.28% [-1.33, +0.29]
build_dict_12 p95 128 +0.32% [-0.59, +1.98] -0.96% [-1.80, +0.35]
build_omitted_12 p95 128 +0.61% [-0.37, +1.39] +0.66% [-0.06, +1.23]
build_typed_12 p95 128 +0.25% [-1.45, +2.44] +0.18% [-1.58, +1.40]
con32 p95 128 -1.46% [-3.89, +0.83] -1.45% [-2.72, +0.91]
diag_32 p95 128 -1.89% [-4.32, -0.71] +0.75% [-0.64, +3.32]
h128 p95 128 +1.03% [-0.89, +2.87] -0.21% [-1.41, +2.40]
mixed_6 p95 128 +0.18% [-0.92, +1.55] +0.38% [-0.05, +0.94]
retained_16384 p95 128 -0.64% [-1.51, -0.10] -0.58% [-1.18, -0.23]
run_omitted_6 p95 128 -0.59% [-1.50, +0.46] -0.20% [-0.63, +0.30]
server_1 p95 128 +0.59% [-0.67, +1.47] +0.28% [-0.16, +0.64]
sustained_32 p95 128 -0.98% [-3.79, +0.12] -1.18% [-3.56, +0.51]
warm p95 128 -0.28% [-1.84, +0.89] +0.29% [-1.76, +2.64]
batch-compare 2x32 p95 128 -0.01% [-0.85, +0.96] -0.20% [-0.88, +0.28]
quote-final 8x16 p95 128 +0.32% [-1.14, +2.12] +1.38% [+0.37, +2.70]
tail 4x1024 p95 128 -5.35% [-7.75, -2.05] -1.03% [-2.71, +0.50]
warm-control 1x4096 p95 128 -1.04% [-2.34, +0.68] -0.66% [-1.19, +0.79]

Comment thread tests/test_lazy_aiohttp.py Fixed
if not keys.issuperset(snapshot):
extra = snapshot.keys() - keys
if any(not _text_row_field(fields[key]) for fields in hints for key in extra if key in fields):
hints = None

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant