Skip to content

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

Draft
markstuart-oai wants to merge 1 commit into
mainfrom
perf/defer-aiohttp-transport
Draft

markstuart-oai wants to merge 1 commit into
mainfrom
perf/defer-aiohttp-transport

Conversation

@markstuart-oai

Copy link
Copy Markdown
Contributor

Changes being requested

Applications with the aiohttp extra installed have been paying to import its transport even when they use the default HTTP client. The default client has been able to work without that import. The SDK now loads the transport when the application constructs the optional client or requests the vendored transport directly. Applications that select aiohttp still pay that cost on first use.

On every request, the sync and async clients have also been walking the type annotations of plain strings, numbers, booleans and nulls inside typed dictionaries. Those values already have their JSON representation. The client now applies the field alias and passes exact built-in scalars through. Subclasses, dates, files, models, collections, omitted fields and unknown keys keep their existing paths. No user values or schemas have been entering a new cache.

Additional context & links

With aiohttp installed and the default HTTP client selected, process launch to a checked one-tool answer has been falling from 864 to 752 ms. The paired median saving across 16 pairs has been 109 ms (95% interval 91–118 ms). Fifteen pairs have been improving. Applications without the optional dependency have been seeing little change in startup.

With the Agents SDK fixed at its main revision, 12 paired blocks through the Responses adapter have been showing the following times.

Default client, core installation Main This change Paired change in time
Launch to answer 819.2 ms 796.6 ms -0.2%
First request after setup 132.6 ms 132.9 ms -1.1%
Warm median 5.067 ms 4.075 ms -19.8%
Warm p95 5.212 ms 4.183 ms -19.9%

Across 83 Codex-off comparisons, 82 medians have been meeting the 3% slowdown limit. One result remains inconclusive. No interval lies wholly above the limit. The comparison combines both Python changes against openai/openai-agents-python at 6afdd47b and openai/openai-python at 66266569. It cannot isolate the effect of either PR. Each row uses 32 primary pairs. In 4 prespecified or uncertain rows, the reported estimate uses 64 new pairs without pooling samples.

The 32-stream workload has been taking 355.4 ms per batch against 410.3 ms on main (paired −12.79%, 95% interval −13.52% to −12.29%). The large SQLite write has been remaining inconclusive (48.92 to 49.35 ms, paired +1.98%, interval −1.11% to +3.83%). The small write has been meeting the margin on confirmation (−4.56%, interval −7.61% to +0.09%). Building an agent with 12 tools and dictionary settings has been taking 51.7 to 52.5 µs (+1.07%, interval +0.80% to +1.92%). A separate 256-request burst with 32 ordinary requests at a time has been showing a real tail cost for the combined changes. The median has been falling from 11.86 to 7.40 ms, while p95 has been rising from 13.15 to 59.55 ms in all 32 pairs.

A separate 128-block test has been synchronizing four writers using separate databases on the same storage on another host. The large write has been showing +0.009% (95% interval −0.030% to +0.052%), close to the comparisons between identical builds. The small write has been showing +0.81% (−0.67% to +3.48%). All 11,776 committed-data checks have been passing. This control leaves the original 83 results intact.

A separate 12-block Responses adapter run has been keeping the Agents testing package unloaded. Launch to answer has been taking 852.8 ms on both mains, 676.4 ms with only the Agents change and 674.0 ms with both changes. Warm medians have been taking 5.089, 4.678 and 3.671 ms respectively. This checks that the application benefit survives removal of the test helper imports. Responses have been using an intercepted transport without live inference.

In a separate short-burst check with only this client change and the Agents SDK held fixed, p95 has been measuring 12.72 ms against 13.54 ms on main. Those two diagnostic pairs have not been reproducing the combined increase. They are too few to establish a client-only tail improvement.

The measurements have been using deterministic local model replies. The provider run has been using the Responses client with an intercepted HTTP transport. Each answer has been requiring two checked requests and one tool execution. These numbers describe SDK work and exclude model inference and external network latency.

The offline suite has been passing 13,005 tests under Pydantic v2 and 12,989 under v1 (175 and 191 skipped). Each version has also been passing seven export-contract tests and 15 installed-wheel transport tests. Ruff, type checking, the wheel without the optional extra, and 476 differential conversion cases have been passing.

The API-resource suite and the fine-tuning positional-argument integration module have not been running locally. The official Steady server has been missing a pinned dependency from its cache, and its registry has been unreachable. CI coverage for those tests remains important.

The new tests have been covering first use, missing and broken optional installations, a preloaded module without an import spec, public class identity, aliases, scalar subclasses and nested conversions. A broken optional installation can select a different private implementation class before construction. The public client still raises the missing-extra error when constructed. The vendored package export now loads the original transport class on demand.

All 83 comparisons with both Python changes and Codex off

Each row reports a median in milliseconds. The paired percentage uses within-pair ratios, so it can differ from the ratio of the displayed medians. When a row has 64 pairs, the table uses the independent confirmation. The two SQLite rows have been writing 2 items of 256 characters and 1,024 items of 4,096 characters. The 32 concurrent ordinary requests and the 32 concurrent streams have separate rows. The short-burst p95 falls outside these medians. The cold_normal and cold_stream rows time the run after imports and fixture setup. The process-launch measurement above includes both.

Suite Workload Main ms Both changes ms Paired change 95% interval Pairs
archived_holdouts cold_normal 64.426 61.758 -4.35% -5.03% to -2.91% 32
archived_holdouts cold_stream 259.78 242.58 -6.77% -9.06% to -5.28% 32
archived_holdouts ids_17x32769 68.066 65.027 -3.93% -4.61% to -1.69% 32
archived_holdouts ids_17x32769_stream 91.406 84.614 -7.79% -9.27% to -6.53% 32
archived_holdouts ids_3x8193 5.7447 4.8384 -16.02% -16.81% to -15.09% 32
archived_holdouts ids_3x8193_stream 8.0181 6.999 -12.79% -13.16% to -11.81% 32
archived_holdouts reuse_normal 38.783 38.254 -1.87% -4.18% to -0.56% 32
archived_holdouts reuse_stream 39.975 39.393 -1.22% -3.31% to +0.41% 32
archived_holdouts typed_3x257 5.5834 5.3571 -5.22% -6.21% to -3.28% 32
archived_holdouts typed_3x257_stream 7.0601 6.1027 -13.72% -14.85% to -12.41% 32
round1 add_tool 1.7158 1.3035 -24.27% -24.44% to -24.00% 32
round1 build_dict_12 0.051716 0.052452 +1.07% +0.80% to +1.92% 32
round1 build_omitted_12 0.069718 0.070001 +0.37% -0.17% to +1.38% 32
round1 build_typed_12 0.038884 0.038865 -0.00% -0.62% to +0.51% 32
round1 con1 0.49985 0.34304 -32.10% -33.52% to -30.39% 32
round1 32 concurrent ordinary requests 11.856 7.4002 -38.12% -38.65% to -37.35% 32
round1 con8 2.8859 2.0035 -30.82% -31.23% to -30.35% 32
round1 diag_32 12.018 7.3393 -38.99% -39.51% to -38.26% 32
round1 h128 1.2553 1.0949 -12.73% -12.99% to -12.23% 32
round1 handoffs_0 0.50605 0.34686 -31.84% -33.94% to -30.14% 32
round1 handoffs_12 0.7154 0.57527 -19.84% -20.08% to -18.65% 32
round1 history120 0.93131 0.78017 -16.19% -16.75% to -15.56% 32
round1 history2000 6.1862 6.022 -2.60% -3.24% to -1.84% 32
round1 history720 2.6756 2.5154 -6.18% -6.66% to -5.86% 32
round1 mixed_6 0.72115 0.6124 -15.07% -15.40% to -14.40% 32
round1 nested67_stream16 73.713 69.132 -6.35% -7.01% to -5.97% 32
round1 repeat_8 0.99012 0.88827 -10.21% -10.92% to -9.85% 32
round1 retained_16384 0.19738 0.19918 +0.07% -0.27% to +0.60% 32
round1 retained_2048 0.029487 0.029228 -0.75% -1.09% to -0.38% 32
round1 run_dict_6 0.77385 0.63052 -18.53% -19.17% to -18.44% 32
round1 run_omitted_6 0.82787 0.68395 -17.14% -17.61% to -16.95% 32
round1 run_typed_6 0.74513 0.59694 -19.88% -20.21% to -19.17% 32
round1 schemas_24 0.85975 0.71884 -16.29% -16.73% to -15.86% 32
round1 server_1 1.796 1.3869 -22.86% -23.32% to -22.49% 32
round1 server_1024 20.983 20.376 -2.38% -3.11% to -1.68% 32
round1 server_128 4.3569 3.9281 -9.31% -10.54% to -8.59% 32
round1 server_512 11.403 10.858 -4.79% -5.49% to -3.70% 32
round1 single_0 0.50048 0.33047 -34.33% -34.75% to -33.25% 32
round1 single_12 0.5986 0.44654 -25.19% -26.47% to -24.52% 32
round1 stream 0.78425 0.61071 -22.26% -22.61% to -21.65% 32
round1 sustained_32 11.823 7.2339 -38.63% -38.95% to -38.34% 32
round1 Large SQLite write 48.923 49.348 +1.98% -1.11% to +3.83% 64
round1 time_16384_2 0.40213 0.39668 -1.46% -1.94% to -0.65% 32
round1 time_2048_2 0.057975 0.057933 -0.14% -0.34% to +0.26% 32
round1 Small SQLite write 6.2549 5.9762 -4.56% -7.61% to +0.09% 64
round1 time_4096_8 0.4325 0.42575 -1.43% -2.28% to +0.01% 32
round1 tools1 1.6155 1.1966 -25.99% -26.42% to -25.71% 32
round1 tools16 19.239 15.083 -21.50% -21.77% to -21.01% 32
round1 tools4 4.5589 3.4511 -24.24% -24.43% to -24.02% 32
round1 warm 0.48 0.33118 -31.34% -32.32% to -29.77% 32
round2 batch-compare 1x1024 21.133 20.647 -2.17% -2.56% to -1.71% 32
round2 batch-compare 2x32 3.6859 3.0396 -17.48% -17.76% to -17.26% 32
round2 bulk-live 12x64 16.542 13.316 -19.43% -19.82% to -19.10% 32
round2 bulk-live 1x1024 20.854 20.392 -1.83% -2.59% to -1.30% 32
round2 bulk-live 1x2048 39.774 39.003 -1.91% -2.29% to -1.23% 32
round2 bulk-live 1x256 6.783 6.361 -6.44% -6.82% to -6.11% 32
round2 combo5 4x1024 44.359 43.052 -3.06% -3.91% to -2.15% 32
round2 combo5 8x128 36.398 34.181 -6.48% -6.73% to -5.76% 32
round2 32 concurrent streams 410.35 355.36 -12.79% -13.52% to -12.29% 64
round2 first-final 1x0 1594.4 1328 -16.48% -17.75% to -15.76% 32
round2 first-mcp 1x0 1594.7 1324.9 -16.72% -17.27% to -15.91% 32
round2 lists-final 1x1024 5.2867 4.3683 -17.68% -18.47% to -17.15% 32
round2 lists-final 8x8192 35.039 29.91 -15.22% -16.39% to -13.23% 32
round2 provider-final 1x2048 262.47 198.95 -24.45% -24.94% to -23.82% 32
round2 provider-final 1x4096 520.25 393.8 -24.27% -24.59% to -23.93% 32
round2 quote-final 1x1 1.7077 1.3208 -22.74% -23.25% to -22.43% 32
round2 quote-final 1x64 2.5888 2.2281 -14.11% -14.57% to -13.90% 32
round2 quote-final 8x16 11.485 9.4046 -18.41% -18.82% to -17.89% 32
round2 quote-final 8x256 37.156 34.993 -5.92% -6.15% to -5.31% 32
round2 quote-final 8x64 16.791 14.808 -11.95% -12.20% to -11.77% 32
round2 shape-local normal 12x96 35.032 31.428 -9.62% -10.30% to -9.28% 32
round2 shape-local stream 12x96 56.48 52.891 -6.46% -6.90% to -6.23% 32
round2 shape-local stream 4x12 11.586 10.262 -10.86% -12.24% to -10.26% 32
round2 shape-local stream 8x0 14.341 12.286 -14.20% -14.72% to -13.82% 32
round2 tail 4x1024 43.686 42.403 -2.74% -3.35% to -2.55% 64
round2 values-final ids 1x1 1.6622 1.284 -22.88% -23.62% to -22.57% 32
round2 values-final ids 8x8192 13.483 10.796 -19.98% -20.56% to -17.74% 32
round2 values-final last 4x512 6.0543 4.9802 -17.88% -17.97% to -17.51% 32
round2 values-final records 8x512 11.234 9.1382 -18.66% -19.13% to -18.23% 32
round2 values-final typed 16x1024 27.245 23.335 -14.01% -14.90% to -13.43% 32
round2 values-final typed 4x128 5.0662 3.9724 -21.54% -21.89% to -20.89% 32
round2 warm-control 1x0 1.7102 1.3124 -23.28% -23.66% to -22.89% 32
round2 warm-control 1x4096 34.927 34.557 -1.34% -2.11% to -0.75% 32

import httpx2
import pytest

import openai

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