|
22 | 22 | - LITELLM_ANTHROPIC_BETA_HEADERS_URL: Custom URL for remote config (optional) |
23 | 23 | """ |
24 | 24 |
|
| 25 | +import asyncio |
25 | 26 | import json |
26 | 27 | import os |
| 28 | +import threading |
| 29 | +from collections.abc import Mapping |
| 30 | +from concurrent.futures import ThreadPoolExecutor |
| 31 | +from functools import cache |
27 | 32 | from importlib.resources import files |
28 | | -from typing import Final |
| 33 | +from typing import Final, TypeAlias |
29 | 34 |
|
30 | 35 | import httpx |
| 36 | +from pydantic import TypeAdapter |
31 | 37 |
|
32 | 38 | from litellm._logging import verbose_logger |
33 | 39 |
|
34 | 40 | # Cache for the loaded configuration |
35 | 41 | _BETA_HEADERS_CONFIG: dict | None = None |
| 42 | +_BETA_HEADERS_CONFIG_LOCK: Final = threading.Lock() |
| 43 | +_BETA_HEADERS_CONFIG_LOADER: Final = ThreadPoolExecutor(max_workers=1, thread_name_prefix="litellm-beta-headers-config") |
| 44 | + |
| 45 | +BetaHeadersConfig: TypeAlias = Mapping[str, str | Mapping[str, str | None]] |
| 46 | +_BETA_HEADERS_CONFIG_ADAPTER: Final = TypeAdapter(BetaHeadersConfig) |
36 | 47 |
|
37 | 48 |
|
38 | 49 | class GetAnthropicBetaHeadersConfig: |
@@ -174,13 +185,27 @@ def _load_beta_headers_config() -> dict: |
174 | 185 | if _BETA_HEADERS_CONFIG is not None: |
175 | 186 | return _BETA_HEADERS_CONFIG |
176 | 187 |
|
177 | | - # Get the URL from environment or use default |
178 | | - from litellm import anthropic_beta_headers_url |
| 188 | + with _BETA_HEADERS_CONFIG_LOCK: |
| 189 | + if _BETA_HEADERS_CONFIG is not None: |
| 190 | + return _BETA_HEADERS_CONFIG |
| 191 | + |
| 192 | + # Get the URL from environment or use default |
| 193 | + from litellm import anthropic_beta_headers_url |
| 194 | + |
| 195 | + _BETA_HEADERS_CONFIG = get_beta_headers_config(url=anthropic_beta_headers_url) |
| 196 | + verbose_logger.debug("Loaded and cached beta headers config") |
| 197 | + |
| 198 | + return _BETA_HEADERS_CONFIG |
| 199 | + |
| 200 | + |
| 201 | +async def aload_beta_headers_config() -> None: |
| 202 | + if _BETA_HEADERS_CONFIG is None: |
| 203 | + await asyncio.get_running_loop().run_in_executor(_BETA_HEADERS_CONFIG_LOADER, _load_beta_headers_config) |
179 | 204 |
|
180 | | - _BETA_HEADERS_CONFIG = get_beta_headers_config(url=anthropic_beta_headers_url) |
181 | | - verbose_logger.debug("Loaded and cached beta headers config") |
182 | 205 |
|
183 | | - return _BETA_HEADERS_CONFIG |
| 206 | +@cache |
| 207 | +def _bundled_beta_headers_config() -> BetaHeadersConfig: |
| 208 | + return _BETA_HEADERS_CONFIG_ADAPTER.validate_python(GetAnthropicBetaHeadersConfig.load_local_beta_headers_config()) |
184 | 209 |
|
185 | 210 |
|
186 | 211 | def reload_beta_headers_config() -> dict: |
@@ -288,6 +313,14 @@ def is_beta_header_supported( |
288 | 313 | return beta_header in provider_mapping and provider_mapping[beta_header] is not None |
289 | 314 |
|
290 | 315 |
|
| 316 | +def is_beta_header_supported_by_bundled_config(beta_header: str, provider: str) -> bool: |
| 317 | + config: Final = _bundled_beta_headers_config() |
| 318 | + aliases: Final = config.get("provider_aliases", {}) |
| 319 | + canonical_provider: Final = (aliases.get(provider) if isinstance(aliases, Mapping) else None) or provider |
| 320 | + provider_mapping: Final = config.get(canonical_provider, {}) |
| 321 | + return isinstance(provider_mapping, Mapping) and provider_mapping.get(beta_header) is not None |
| 322 | + |
| 323 | + |
291 | 324 | def get_provider_beta_header( |
292 | 325 | anthropic_beta_header: str, |
293 | 326 | provider: str, |
|
0 commit comments