perf: lazily load openai.types exports - #3725
Open
patrickswedish wants to merge 2 commits into
Open
Conversation
…openai#2819) Eager top-level imports in openai.types.__init__ caused every invocation of import openai to eagerly parse and construct hundreds of Pydantic models, regressing cold startup import time to 14+ seconds on slower I/O and constrained environments. This change introduces: 1. TypesProxy in openai._utils._types_proxy to lazily resolve the openai.types package on attribute access, matching the established ResourcesProxy pattern. 2. PEP 562 lazy attribute loading in openai.types.__init__ using __getattr__ and __dir__, with static type annotations guarded by yping.TYPE_CHECKING so IDEs and type checkers retain full autocomplete and type safety. 3. Regression unit tests in ests/test_types_proxy.py. Fixes openai#2819
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d9a21501d3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Address review feedback on wildcard import and module namespace introspection: 1. Include historical submodule and subpackage names in types.__all__ and static TYPE_CHECKING declarations so 'from openai.types import *' resolves both public classes and submodules lazily without breaking historical compatibility. 2. Update types.__dir__() to return a deterministic sorted union of globals(), __all__, and cached submodules, exposing standard dunders (__name__, __doc__, __package__, etc.) and dynamic attributes. 3. Add regression tests for wildcard import and dir(openai.types) namespace integrity in tests/test_types_proxy.py.
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.
Summary
Reduce unnecessary eager imports from
openai.typesby resolving generated type exports lazily while preserving the existing public import surface and static typing behavior.Fixes #2819.
Root cause
openai.types.__init__eagerly imports hundreds of generated type/model modules. Becauseopenaiexposes this package during normal initialization, a plainimport openaipays the cost of loading many API-domain type modules that most callers do not use during startup.Change
openai.typespublic exports on first accessTYPE_CHECKING__all__,dir(), direct imports, repeated-access identity, and nested importsImport footprint
On the same Windows x86_64 / CPython 3.10.16 environment:
openai.types.*modulesThe deterministic improvement is the import graph: the patch avoids loading 413
openai.types.*modules during normal startup.The cold-disk measurement showed a large improvement on this machine; warm page-cache timings were approximately neutral, so this PR does not rely on a wall-clock performance threshold in CI.
Compatibility
Validated from a built wheel in a fresh Python 3.10 virtualenv:
import openaifrom openai import OpenAIimport openai.typesopenai.typesopenai.types.chatimportsdir(openai.types)__all__All passed.
Regression proof
The import-graph test:
openai.types.*modules are eagerly importedValidation
git diff --check: passed