Skip to content

perf: lazily load openai.types exports - #3725

Open
patrickswedish wants to merge 2 commits into
openai:mainfrom
patrickswedish:fix/lazy-types-import-2819
Open

perf: lazily load openai.types exports#3725
patrickswedish wants to merge 2 commits into
openai:mainfrom
patrickswedish:fix/lazy-types-import-2819

Conversation

@patrickswedish

Copy link
Copy Markdown

Summary

Reduce unnecessary eager imports from openai.types by 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. Because openai exposes this package during normal initialization, a plain import openai pays the cost of loading many API-domain type modules that most callers do not use during startup.

Change

  • add a lazy type-export proxy following the repository's existing proxy pattern
  • resolve openai.types public exports on first access
  • retain static imports under TYPE_CHECKING
  • preserve __all__, dir(), direct imports, repeated-access identity, and nested imports
  • add deterministic regression coverage for eager type-module loading

Import footprint

On the same Windows x86_64 / CPython 3.10.16 environment:

Metric main patch
loaded openai.types.* modules 433 20
Python import steps 2687 2305
cold-disk observation 14,334.97 ms 1,399.64 ms
warm page-cache median 1,479.93 ms 1,560.60 ms

The 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 openai
  • from openai import OpenAI
  • import openai.types
  • direct imports from openai.types
  • nested openai.types.chat imports
  • repeated lazy access identity
  • dir(openai.types)
  • __all__

All passed.

Regression proof

The import-graph test:

  • fails on clean upstream because representative openai.types.* modules are eagerly imported
  • passes with this patch
  • fails again when eager behavior is deliberately restored
  • passes after restoring the patch

Validation

  • Mypy: 0 issues in 1523 files
  • Pyright strict: 0 errors / 0 warnings
  • Ruff check: passed
  • Ruff format check: passed
  • focused regression tests: passed
  • built-wheel smoke tests: 8/8 passed
  • custom-code isolation/ratchet: passed
  • git diff --check: passed

…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
@patrickswedish
patrickswedish requested a review from a team as a code owner August 24, 2026 14:12

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/openai/types/__init__.py Outdated
Comment thread src/openai/types/__init__.py Outdated
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.
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.

Slow import times (due to openai.types)

1 participant