fix(azure/realtime): use byte-aware close reason for transcription socket errors

Greptile flagged that reason[:120] truncates by characters, so a multibyte Azure error can push the close frame past the 123-byte WebSocket limit and turn the readable 1008 close into a generic 1011. Route both close reasons through websocket_close_reason.
This commit is contained in:
DhruvaMyakeri 2026-08-28 14:38:21 +05:30
parent 590c9d134e
commit 9afa462966

View file

@ -19,6 +19,7 @@ from litellm.constants import (
from litellm.types.realtime import RealtimeQueryParams
from ....litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
from ....litellm_core_utils.realtime_errors import websocket_close_reason
from ....litellm_core_utils.realtime_streaming import (
RealTimeStreaming,
ScopedWebSocket,
@ -242,7 +243,12 @@ class AzureOpenAIRealtime(AzureChatCompletion):
else None
)
if isinstance(minted, _EphemeralMintError):
await websocket.close(code=1008, reason=_redact_string(minted.reason)[:120])
await websocket.close(
code=1008,
reason=websocket_close_reason(
_redact_string(minted.reason), fallback="Realtime transcription session error"
),
)
return
url: Final = self._construct_url(
@ -280,6 +286,9 @@ class AzureOpenAIRealtime(AzureChatCompletion):
await realtime_streaming.bidirectional_forward()
except websockets.exceptions.InvalidStatusCode as e:
await websocket.close(code=e.status_code, reason=_redact_string(str(e)))
await websocket.close(
code=e.status_code,
reason=websocket_close_reason(_redact_string(str(e)), fallback="Invalid status code"),
)
except Exception:
verbose_proxy_logger.exception("Error in AzureOpenAIRealtime.async_realtime")