| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
`import openai` eagerly imported the entire Assistants `beta` type tree and the streaming helpers, building hundreds of pydantic models that most callers never use. This dominates cold-start / first-import latency in serverless and hosted-agent environments. Make the top-level `openai` package expose `types`, `pydantic_function_tool`, `AssistantEventHandler`, `AsyncAssistantEventHandler` and the `ReconnectingEvent`/`ReconnectingOverrides` websocket types lazily via a module-level `__getattr__` (PEP 562) plus a `types` LazyProxy, and defer `openai.lib` helper imports. Public attribute access, `from openai import *`, and static type checking (via `TYPE_CHECKING` re-exports) are unchanged. Measured (openai 3.11.0, warm best-of-3): `import openai` drops from ~1,944 ms to ~1,101 ms (~43%), and eagerly-loaded `openai.*` submodules drop from 835 to 497 -- the Assistants `beta` subtree (309 modules) is no longer built at import. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 93f793d2-1b86-4677-908c-3722d4fef290
There was a problem hiding this comment.
Here are some automated review suggestions for this pull request.
Reviewed commit: bd68d7aad5
ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Sorry, something went wrong.
There was a problem hiding this comment.
This would definitely improve import performance across the board, and complements a fix I made in our package: microsoft/agent-framework#8219
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
import openai eagerly imports the entire Assistants beta type tree and the streaming helpers, building hundreds of pydantic models that most callers never use. This dominates cold-start / first-import latency in serverless and hosted-agent environments (e.g. containers that boot, import the SDK, then serve a single Responses/Chat request).
This PR makes the top-level openai package expose the rarely-needed surfaces lazily while keeping the public API and static typing identical:
Azure / Bedrock module-client support and every existing public object are untouched.
Measurements
openai 3.11.0, warm best-of-3, import openai on top of an already-imported pydantic + httpx2:
~43% faster first import; the Assistants beta subtree (309 modules) is no longer built at import time. It loads transparently on first access (openai.beta, openai.types.beta, etc.).
Tests
Adds tests/test_lazy_imports.py:
Existing tests/test_module_client.py and tests/lib/test_old_api.py continue to pass, and a full import smoke over every openai.types.* subtree + from openai import * verifies no exported name regressed.