Get or create a singleton AsyncOpenAI client with the given configuration. Args: api_key: OpenAI API key base_url: Optional base URL for API organization: Optional organization ID timeout: Request timeout default_headers: Optional default headers
(
api_key: str,
base_url: Optional[str] = None,
organization: Optional[str] = None,
timeout: Union[float, Timeout] = 120.0,
default_headers: Optional[Dict[str, str]] = None,
http_client: Optional[Any] = None,
http_client_config: Optional[Dict[str, Any]] = None,
)
| 153 | |
| 154 | |
| 155 | def get_async_openai_client( |
| 156 | api_key: str, |
| 157 | base_url: Optional[str] = None, |
| 158 | organization: Optional[str] = None, |
| 159 | timeout: Union[float, Timeout] = 120.0, |
| 160 | default_headers: Optional[Dict[str, str]] = None, |
| 161 | http_client: Optional[Any] = None, |
| 162 | http_client_config: Optional[Dict[str, Any]] = None, |
| 163 | ) -> AsyncOpenAI: |
| 164 | """ |
| 165 | Get or create a singleton AsyncOpenAI client with the given configuration. |
| 166 | |
| 167 | Args: |
| 168 | api_key: OpenAI API key |
| 169 | base_url: Optional base URL for API |
| 170 | organization: Optional organization ID |
| 171 | timeout: Request timeout |
| 172 | default_headers: Optional default headers |
| 173 | http_client: Optional httpx.AsyncClient instance |
| 174 | http_client_config: Optional config dict for creating httpx.AsyncClient |
| 175 | |
| 176 | Returns: |
| 177 | AsyncOpenAI client instance |
| 178 | """ |
| 179 | if isinstance(timeout, (int, float)): |
| 180 | timeout = Timeout(timeout) |
| 181 | |
| 182 | # If http_client is provided directly, don't cache (complex object) |
| 183 | if http_client is not None: |
| 184 | client = AsyncOpenAI( |
| 185 | api_key=api_key, |
| 186 | base_url=base_url, |
| 187 | organization=organization, |
| 188 | timeout=timeout, |
| 189 | default_headers=default_headers, |
| 190 | http_client=http_client, |
| 191 | ) |
| 192 | _all_clients.add(client) |
| 193 | return client |
| 194 | |
| 195 | # If http_client_config is provided, create async client from config and cache |
| 196 | created_http_client = None |
| 197 | if http_client_config is not None: |
| 198 | try: |
| 199 | from httpx import AsyncClient |
| 200 | |
| 201 | created_http_client = AsyncClient(**http_client_config) |
| 202 | except ImportError: |
| 203 | raise ValueError( |
| 204 | "httpx is required to use http_client_config. " |
| 205 | "Install it with: pip install httpx" |
| 206 | ) |
| 207 | |
| 208 | cache_key = _get_cache_key( |
| 209 | "async_openai", |
| 210 | api_key=api_key, |
| 211 | base_url=base_url, |
| 212 | organization=organization, |
searching dependent graphs…