perf(lib): stop pulling openai.types.beta into every import openai - #3727
Open
aviseth wants to merge 1 commit into
Open
perf(lib): stop pulling openai.types.beta into every import openai#3727aviseth wants to merge 1 commit into
aviseth wants to merge 1 commit into
Conversation
openai/__init__.py imports openai.lib.streaming, whose _assistants module imported openai.types.beta at module scope. That loaded 318 modules on every import openai, for annotations that from __future__ import annotations already defers and two runtime paths that can import locally. Move the annotation-only imports under TYPE_CHECKING and import RunStep and MessageContent inside the two functions that construct them at runtime. import openai drops from 1234 to 916 modules and from 359ms to 257ms median cold import (1.40x, python 3.13, 25 runs). No public API change: openai.AssistantEventHandler and openai.AsyncAssistantEventHandler stay eagerly exported. Refs openai#2819
1 task
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
openai/__init__.pyimportsopenai.lib.streaming, andopenai/lib/streaming/_assistants.pyimportedopenai.types.betaat module scope. That loaded 318 modules on everyimport openai— about 30% of the total import cost — so that anyone importing the package paid for the Assistants API whether or not they used it._assistants.pyalready hasfrom __future__ import annotations, so all but two of those names were needed only by type checkers. This moves them underTYPE_CHECKINGand imports the two runtime exceptions locally:RunStep, used byconstruct_type(type_=RunStep, ...)inaccumulate_run_step, andMessageContent, used twice inaccumulate_event.Measured from a clean checkout with the repo venv, 25 cold runs each on Python 3.13:
sys.modulesopenai.types.betamain1.40x faster, −102 ms, −318 modules.
There is no public API change.
openai.AssistantEventHandlerandopenai.AsyncAssistantEventHandlerremain eagerly exported fromopenai/__init__.py,from openai import *is unchanged, andimport openai.lib.streamingstill works. This deliberately avoids a module-level__getattr__on the package.The whole change is confined to
src/openai/lib/, which CONTRIBUTING.md says the generator never modifies and which carries noFile generated from our OpenAPI specheader. It therefore does not touch generated output, does not consume Castiron custom-code budget, and cannot conflict with a regen — which is the concern that closed #2950.tests/lib/test_streaming_lazy_types.pyasserts in a fresh interpreter thatimport openailoads noopenai.types.betamodule, and that both handler names are still eagerly exported. It fails onmainand passes here../scripts/lintis clean (ruff, pyright strict, mypy 1573 files) and./scripts/testpasses both lanes: 9127 passed / 144 skipped on Pydantic v2, 9113 passed / 158 skipped on Pydantic v1.Additional context & links
Fixes the bulk of #2819. Supersedes the approach in #2950, which deferred
openai.libitself — that is only 0.9% of import cost, which is why it measured ~4%. The cost was in what onelibmodule pulled in behind it, not inlib.The remaining
openai.types.*non-beta cost (~144 ms across 434 modules) is reachable fromopenai/__init__.pydirectly and would need a change to generated code, so it is out of scope here.