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(realtime): dial Azure's GA realtime upstream for GA clients
mateo-berri8deb46534631
diff --git a/litellm/realtime_api/main.py b/litellm/realtime_api/main.pyindex b824a5928c6..5310efba1c4 100644--- a/litellm/realtime_api/main.py+++ b/litellm/realtime_api/main.py@@ -14,6 +14,7 @@ from litellm.constants import (request_timeout,)from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider+from litellm.litellm_core_utils.realtime_streaming import client_sent_openai_beta_realtime_headerfrom litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfigfrom litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandlerfrom litellm.llms.xai.common_utils import XAIModelInfo@@ -413,14 +414,14 @@ async def _arealtime(api_version = api_version or litellm_params.api_version or "2024-10-01-preview"- realtime_protocol = (+ configured_realtime_protocol: Final = (kwargs.get("realtime_protocol")or litellm_params.get("realtime_protocol")or os.environ.get("LITELLM_AZURE_REALTIME_PROTOCOL"))- if realtime_protocol is None and (query_params or {}).get("intent") == "transcription":- realtime_protocol = "GA"- realtime_protocol = realtime_protocol or "beta"+ realtime_protocol: Final = _azure_realtime_protocol_for_client(+ configured_realtime_protocol, query_params=query_params, websocket=websocket+ )resolved_azure_ad_token: Final = (None if api_key else get_azure_ad_token(GenericLiteLLMParams(**kwargs, azure_ad_token=azure_ad_token)))@@ -576,6 +577,19 @@ def _is_transcription_only_realtime_model(model: str, custom_llm_provider: str)_TRANSCRIPTION_QUERY_PARAMS: Final[RealtimeQueryParams] = {"intent": "transcription"}+def _azure_realtime_protocol_for_client(+ configured_protocol: object,+ *,+ query_params: RealtimeQueryParams | None,+ websocket: "WebSocket",+) -> str:+ if isinstance(configured_protocol, str) and configured_protocol:+ return configured_protocol+ if (query_params or {}).get("intent") == "transcription":+ return "GA"+ return "beta" if client_sent_openai_beta_realtime_header(websocket) else "GA"++def _azure_realtime_health_protocol(model: str, realtime_protocol: str | None, model_params: Mapping[str, object]) -> tuple[str, RealtimeQueryParams | None]:diff --git a/tests/test_litellm/realtime_api/test_main.py b/tests/test_litellm/realtime_api/test_main.pyindex 761e87ac764..8a9abe819e5 100644--- a/tests/test_litellm/realtime_api/test_main.py+++ b/tests/test_litellm/realtime_api/test_main.py@@ -327,7 +327,60 @@ async def test_arealtime_azure_ai_on_a_foundry_host_connects_to_the_azure_openaiapi_key="fake-key",litellm_logging_obj=FakeLogging(),)- assert connect.url == (- "wss://my-project.services.ai.azure.com/openai/realtime"- "?api-version=2024-10-01-preview&deployment=gpt-realtime-mini"+ assert connect.url == "wss://my-project.services.ai.azure.com/openai/v1/realtime?model=gpt-realtime-mini"+++class _ClientWebSocketWithHeaders:+ def __init__(self, headers: tuple[tuple[bytes, bytes], ...]) -> None:+ self.scope: Final = {"headers": headers}+++_GA_CLIENT: Final = _ClientWebSocketWithHeaders(headers=())+_BETA_CLIENT: Final = _ClientWebSocketWithHeaders(headers=((b"openai-beta", b"realtime=v1"),))+++async def _azure_backend_url_dialed_for(websocket: _ClientWebSocketWithHeaders, **kwargs: object) -> str | None:+ connect: Final = _ConnectThatStopsAfterCapturingTheUrl()+ with patch("websockets.connect", connect):+ await realtime_main._arealtime.__wrapped__(+ model="azure/gpt-realtime",+ websocket=websocket,+ api_base="https://my-endpoint.openai.azure.com",+ api_key="fake-key",+ litellm_logging_obj=FakeLogging(),+ **kwargs,+ )+ return connect.url+++async def test_arealtime_azure_ga_client_without_beta_header_dials_the_ga_upstream(monkeypatch):+ monkeypatch.delenv("LITELLM_AZURE_REALTIME_PROTOCOL", raising=False)+ assert (+ await _azure_backend_url_dialed_for(_GA_CLIENT)+ == "wss://my-endpoint.openai.azure.com/openai/v1/realtime?model=gpt-realtime"+ )+++async def test_arealtime_azure_beta_header_client_keeps_the_beta_upstream(monkeypatch):+ monkeypatch.delenv("LITELLM_AZURE_REALTIME_PROTOCOL", raising=False)+ assert await _azure_backend_url_dialed_for(_BETA_CLIENT) == (+ "wss://my-endpoint.openai.azure.com/openai/realtime?api-version=2024-10-01-preview&deployment=gpt-realtime"+ )+++async def test_arealtime_azure_explicit_beta_protocol_wins_over_a_ga_client(monkeypatch):+ monkeypatch.delenv("LITELLM_AZURE_REALTIME_PROTOCOL", raising=False)+ assert await _azure_backend_url_dialed_for(_GA_CLIENT, realtime_protocol="beta") == (+ "wss://my-endpoint.openai.azure.com/openai/realtime?api-version=2024-10-01-preview&deployment=gpt-realtime"+ )+++async def test_arealtime_azure_env_beta_protocol_wins_over_a_ga_client(monkeypatch):+ monkeypatch.setenv("LITELLM_AZURE_REALTIME_PROTOCOL", "beta")+ assert await _azure_backend_url_dialed_for(_GA_CLIENT) == (+ "wss://my-endpoint.openai.azure.com/openai/realtime?api-version=2024-10-01-preview&deployment=gpt-realtime")