OpenAIProvider is the concrete model provider for OpenAI APIs; it manages API credentials, websocket configuration, HTTP client pooling, and feature validation for Chat Completions and Responses models. OpenAIProvider caches websocket model instances per event loop to maintain persistent connections when the same provider instance handles multiple concurrent calls, and auto-prunes closed event loops to release resources.
OpenAIProvider, implemented in src/agents/models/openai_provider.py, is the concrete model provider for OpenAI APIs and resolves base_url from the OPENAI_BASE_URL environment variable and websocket_base_url from OPENAI_WEBSOCKET_BASE_URL when those options are not explicitly supplied at construction time.[1] DEFAULT_MODEL in src/agents/models/openai_provider.py is kept as "gpt-4o" only for backward compatibility; the recommended approach is to call get_default_model(), which reflects the current default.[1]
OpenAIProvider raises a UserError if openai_client is provided together with any of api_key, base_url, websocket_base_url, organization, or project.[1] OpenAIProvider accepts an agent_registration parameter (type OpenAIAgentRegistrationConfig | dict | None), resolves it via resolve_openai_agent_registration_config, and exposes the result through the agent_registration property.[1]
OpenAIProvider shares a single httpx2.AsyncClient across all requests via shared_http_client() to avoid per-request connection-pool teardown and the associated latency and resource cost.[1] Websocket model wrappers are cached per event loop using a WeakKeyDictionary keyed by asyncio.AbstractEventLoop, so that websocket transport can maintain a persistent connection when callers pass model names as strings through a shared provider instance.[1] OpenAIProvider._prune_closed_ws_loop_caches() drops websocket model cache entries for event loops that are already closed, and forcibly drops the underlying websocket connection synchronously for each cached OpenAIResponsesWSModel.[1]
OpenAIProvider accepts a strict_feature_validation flag (default False); when True, Chat Completions models raise a UserError if callers pass Responses-only features such as previous_response_id, conversation_id, prompt, or non-text-only tool outputs.[1] OpenAIProvider accepts a buffer_streamed_tool_calls flag (default False); when True, Chat Completions models buffer all streamed function tool-call deltas and emit them only after the provider stream finishes, for compatibility with providers whose streamed tool-call chunk semantics are unreliable.[1]
Sources