The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(azure): route WebRTC client_secrets and calls to the GA v1 realtime API (#47316)
Habon Laszloa2773bdcad53
diff --git a/litellm/llms/azure/realtime/http_transformation.py b/litellm/llms/azure/realtime/http_transformation.pyindex 633c2b3b6d0..542a6a8c02f 100644--- a/litellm/llms/azure/realtime/http_transformation.py+++ b/litellm/llms/azure/realtime/http_transformation.py@@ -7,17 +7,21 @@ from litellm.llms.base_llm.realtime.http_transformation import BaseRealtimeHTTPCfrom litellm.secret_managers.main import get_secret_str+def _azure_realtime_v1_url(api_base: str, endpoint: str) -> str:+ """Azure serves WebRTC only on the GA v1 surface, which rejects any api-version query param."""+ base: Final = api_base.rstrip("/").removesuffix("/v1").removesuffix("/openai")+ return f"{base}/openai/v1/realtime/{endpoint}"++class AzureRealtimeHTTPConfig(BaseRealtimeHTTPConfig):- def get_api_base(self, api_base: str | None, **kwargs) -> str:+ def get_api_base(self, api_base: str | None, **kwargs: object) -> str:return api_base or litellm.api_base or get_secret_str("AZURE_API_BASE") or ""- def get_api_key(self, api_key: str | None, **kwargs) -> str:+ def get_api_key(self, api_key: str | None, **kwargs: object) -> str:return api_key or litellm.api_key or get_secret_str("AZURE_API_KEY") or ""def get_complete_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:- base: Final = self.get_api_base(api_base).rstrip("/")- version: Final = api_version or get_secret_str("AZURE_API_VERSION") or "2024-12-17"- return f"{base}/openai/realtime/client_secrets?api-version={version}"+ return _azure_realtime_v1_url(self.get_api_base(api_base), "client_secrets")def validate_environment(self,@@ -32,16 +36,14 @@ class AzureRealtimeHTTPConfig(BaseRealtimeHTTPConfig):}def get_realtime_calls_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:- base: Final = self.get_api_base(api_base).rstrip("/")- version: Final = api_version or get_secret_str("AZURE_API_VERSION") or "2024-12-17"- return f"{base}/openai/realtime/calls?api-version={version}"+ return _azure_realtime_v1_url(self.get_api_base(api_base), "calls")def get_transcription_session_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:base: Final = self.get_api_base(api_base).rstrip("/")version: Final = api_version or get_secret_str("AZURE_API_VERSION") or "2024-12-17"return f"{base}/openai/realtime/transcription_sessions?api-version={version}"- def get_realtime_calls_headers(self, ephemeral_key: str) -> dict:+ def get_realtime_calls_headers(self, ephemeral_key: str) -> dict[str, str]:return {- "api-key": ephemeral_key,+ "Authorization": f"Bearer {ephemeral_key}",}diff --git a/tests/unit/llms/azure/realtime/test_http_transformation.py b/tests/unit/llms/azure/realtime/test_http_transformation.pynew file mode 100644index 00000000000..714ce9558cf--- /dev/null+++ b/tests/unit/llms/azure/realtime/test_http_transformation.py@@ -0,0 +1,57 @@+from typing import Final++import httpx+import pytest++import litellm+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler++_DATED_PREVIEW_API_VERSION: Final = "2025-04-01-preview"+_API_BASES: Final = ("https://my.openai.azure.com", "https://my.openai.azure.com/", "https://my.openai.azure.com/openai")+++class _AzureThatRecordsTheRequest:+ def __init__(self) -> None:+ self.requests: Final[list[httpx.Request]] = []++ def __call__(self, request: httpx.Request) -> httpx.Response:+ self.requests.append(request)+ return httpx.Response(200, json={"value": "ek_upstream"})++ def client(self) -> AsyncHTTPHandler:+ return AsyncHTTPHandler(transport=httpx.MockTransport(self))++[email protected]("api_base", _API_BASES)+async def test_azure_client_secret_targets_the_ga_v1_surface_even_with_a_dated_api_version(api_base: str) -> None:+ azure: Final = _AzureThatRecordsTheRequest()+ await litellm.acreate_realtime_client_secret(+ model="azure/gpt-realtime-deployment",+ api_base=api_base,+ api_key="azure-key",+ api_version=_DATED_PREVIEW_API_VERSION,+ client=azure.client(),+ )+ (request,) = azure.requests+ assert str(request.url) == "https://my.openai.azure.com/openai/v1/realtime/client_secrets"+ assert request.headers.get("api-key") == "azure-key"++[email protected]("api_base", _API_BASES)+async def test_azure_sdp_exchange_targets_the_ga_v1_surface_with_the_ephemeral_key_as_bearer(api_base: str) -> None:+ azure: Final = _AzureThatRecordsTheRequest()+ await litellm.arealtime_calls(+ openai_ephemeral_key="ek_upstream",+ sdp_body=b"v=0",+ model="azure/gpt-realtime-deployment",+ api_base=api_base,+ api_key="azure-key",+ api_version=_DATED_PREVIEW_API_VERSION,+ client=azure.client(),+ )+ (request,) = azure.requests+ assert str(request.url) == "https://my.openai.azure.com/openai/v1/realtime/calls"+ assert request.headers.get("authorization") == "Bearer ek_upstream"+ assert "api-key" not in request.headers