Skip to content

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]

fix(azure): forward realtime_protocol from config and relax api_version check for GA path (#22369)

Giulio Leone76459b1323b6
diff --git a/litellm/llms/azure/realtime/handler.py b/litellm/llms/azure/realtime/handler.py
index 8f4291ec271..0ad6fb57354 100644
--- a/litellm/llms/azure/realtime/handler.py
+++ b/litellm/llms/azure/realtime/handler.py
@@ -33,7 +33,7 @@ class AzureOpenAIRealtime(AzureChatCompletion):
self,
api_base: str,
model: str,
- api_version: str,
+ api_version: Optional[str],
realtime_protocol: Optional[str] = None,
) -> str:
"""
@@ -56,8 +56,9 @@ class AzureOpenAIRealtime(AzureChatCompletion):
"""
api_base = api_base.replace("https://", "wss://")
- # Determine path based on realtime_protocol
- if realtime_protocol in ("GA", "v1"):
+ # Determine path based on realtime_protocol (case-insensitive)
+ _is_ga = realtime_protocol is not None and realtime_protocol.upper() in ("GA", "V1")
+ if _is_ga:
path = "/openai/v1/realtime"
return f"{api_base}{path}?model={model}"
else:
@@ -85,7 +86,7 @@ class AzureOpenAIRealtime(AzureChatCompletion):
if api_base is None:
raise ValueError("api_base is required for Azure OpenAI calls")
- if api_version is None:
+ if api_version is None and (realtime_protocol is None or realtime_protocol.upper() not in ("GA", "V1")):
raise ValueError("api_version is required for Azure OpenAI calls")
url = self._construct_url(
diff --git a/litellm/realtime_api/main.py b/litellm/realtime_api/main.py
index 3e64f61abdb..83ab63ef146 100644
--- a/litellm/realtime_api/main.py
+++ b/litellm/realtime_api/main.py
@@ -1,5 +1,6 @@
"""Abstraction function for OpenAI's realtime API"""
+import os
from typing import Any, Optional, cast
import litellm
@@ -132,6 +133,8 @@ async def _arealtime( # noqa: PLR0915
realtime_protocol = (
kwargs.get("realtime_protocol")
+ or litellm_params.get("realtime_protocol")
+ or os.environ.get("LITELLM_AZURE_REALTIME_PROTOCOL")
or "beta"
)
await azure_realtime.async_realtime(
diff --git a/tests/test_litellm/llms/azure/realtime/test_azure_realtime_handler.py b/tests/test_litellm/llms/azure/realtime/test_azure_realtime_handler.py
index 2a110c8f9a7..e9c5c9cfc1b 100644
--- a/tests/test_litellm/llms/azure/realtime/test_azure_realtime_handler.py
+++ b/tests/test_litellm/llms/azure/realtime/test_azure_realtime_handler.py
@@ -158,6 +158,27 @@ async def test_construct_url_v1_protocol():
assert url.count("/realtime") == 1
[email protected]("protocol", ["ga", "Ga", "gA", "V1", "v1", "GA"])
+async def test_construct_url_case_insensitive_protocol(protocol):
+ """
+ Test that realtime_protocol matching is case-insensitive.
+ """
+ from litellm.llms.azure.realtime.handler import AzureOpenAIRealtime
+
+ handler = AzureOpenAIRealtime()
+ url = handler._construct_url(
+ api_base="https://my-endpoint.openai.azure.com",
+ model="gpt-realtime-deployment",
+ api_version=None,
+ realtime_protocol=protocol,
+ )
+
+ assert "/openai/v1/realtime?" in url
+ assert "model=gpt-realtime-deployment" in url
+ assert "api-version" not in url
+
+
@pytest.mark.asyncio
async def test_async_realtime_uses_ga_protocol_end_to_end():
"""
@@ -212,6 +233,113 @@ async def test_async_realtime_uses_ga_protocol_end_to_end():
assert "deployment" not in called_url
+async def test_async_realtime_ga_without_api_version():
+ """
+ Test that GA/v1 protocol works without api_version (which is not needed for the GA path).
+ Fixes #22127: api_version check was unconditional, blocking GA path.
+ """
+ from litellm.llms.azure.realtime.handler import AzureOpenAIRealtime
+
+ handler = AzureOpenAIRealtime()
+ api_base = "https://my-endpoint.openai.azure.com"
+ api_key = "test-key"
+ model = "gpt-realtime-deployment"
+
+ dummy_websocket = AsyncMock()
+ dummy_logging_obj = MagicMock()
+ mock_backend_ws = AsyncMock()
+
+ class DummyAsyncContextManager:
+ def __init__(self, value):
+ self.value = value
+ async def __aenter__(self):
+ return self.value
+ async def __aexit__(self, exc_type, exc, tb):
+ return None
+
+ with patch("websockets.connect", return_value=DummyAsyncContextManager(mock_backend_ws)) as mock_ws_connect, \
+ patch("litellm.llms.azure.realtime.handler.RealTimeStreaming") as mock_realtime_streaming:
+
+ mock_streaming_instance = MagicMock()
+ mock_realtime_streaming.return_value = mock_streaming_instance
+ mock_streaming_instance.bidirectional_forward = AsyncMock()
+
+ # GA protocol with api_version=None should NOT raise ValueError
+ await handler.async_realtime(
+ model=model,
+ websocket=dummy_websocket,
+ logging_obj=dummy_logging_obj,
+ api_base=api_base,
+ api_key=api_key,
+ api_version=None,
+ realtime_protocol="GA",
+ )
+
+ called_url = mock_ws_connect.call_args[0][0]
+ assert "/openai/v1/realtime?" in called_url
+ assert "model=gpt-realtime-deployment" in called_url
+ assert "api-version" not in called_url
+
+
+async def test_async_realtime_beta_without_api_version_raises():
+ """
+ Test that beta protocol still requires api_version.
+ """
+ from litellm.llms.azure.realtime.handler import AzureOpenAIRealtime
+
+ handler = AzureOpenAIRealtime()
+ dummy_websocket = AsyncMock()
+ dummy_logging_obj = MagicMock()
+
+ with pytest.raises(ValueError, match="api_version is required"):
+ await handler.async_realtime(
+ model="gpt-4o-realtime-preview",
+ websocket=dummy_websocket,
+ logging_obj=dummy_logging_obj,
+ api_base="https://my-endpoint.openai.azure.com",
+ api_key="test-key",
+ api_version=None,
+ realtime_protocol="beta",
+ )
+
+
+async def test_realtime_protocol_env_var_fallback():
+ """
+ Test that LITELLM_AZURE_REALTIME_PROTOCOL env var is used as fallback.
+ Fixes #22127: no way to set realtime_protocol from config.
+ """
+ from litellm.realtime_api.main import _arealtime
+ from litellm.types.router import GenericLiteLLMParams
+
+ with patch.dict(os.environ, {"LITELLM_AZURE_REALTIME_PROTOCOL": "v1"}):
+ # Create a GenericLiteLLMParams without realtime_protocol
+ litellm_params = GenericLiteLLMParams()
+ # The env var should be picked up as fallback
+ realtime_protocol = (
+ {}.get("realtime_protocol")
+ or litellm_params.get("realtime_protocol")
+ or os.environ.get("LITELLM_AZURE_REALTIME_PROTOCOL")
+ or "beta"
+ )
+ assert realtime_protocol == "v1"
+
+
+async def test_realtime_protocol_from_litellm_params():
+ """
+ Test that realtime_protocol is read from litellm_params (config.yaml extra field).
+ Fixes #22127: realtime_protocol in litellm_params was not used.
+ """
+ from litellm.types.router import GenericLiteLLMParams
+
+ # Simulate config.yaml with realtime_protocol as an extra field
+ litellm_params = GenericLiteLLMParams(realtime_protocol="GA")
+ assert litellm_params.get("realtime_protocol") == "GA"
+
+
@pytest.mark.asyncio
async def test_async_realtime_default_maintains_backwards_compatibility():
"""