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(xai): reject non-success stt responses before parsing

kerry99d91d72056a
diff --git a/litellm/llms/xai/audio_transcription/transformation.py b/litellm/llms/xai/audio_transcription/transformation.py
index 03c06f24a2d..7b648fc8084 100644
--- a/litellm/llms/xai/audio_transcription/transformation.py
+++ b/litellm/llms/xai/audio_transcription/transformation.py
@@ -130,6 +130,13 @@ class XAIAudioTranscriptionConfig(BaseAudioTranscriptionConfig):
self,
raw_response: Response,
) -> TranscriptionResponse:
+ if raw_response.status_code >= 400:
+ raise self.get_error_class(
+ error_message=raw_response.text,
+ status_code=raw_response.status_code,
+ headers=raw_response.headers,
+ )
+
try:
payload: Final = _XAISttResponse.model_validate_json(raw_response.content)
except ValidationError as e:
diff --git a/tests/test_litellm/llms/xai/test_xai_audio_transcription_transformation.py b/tests/test_litellm/llms/xai/test_xai_audio_transcription_transformation.py
index 0f3445eb400..e2e3fc3d4dc 100644
--- a/tests/test_litellm/llms/xai/test_xai_audio_transcription_transformation.py
+++ b/tests/test_litellm/llms/xai/test_xai_audio_transcription_transformation.py
@@ -8,6 +8,7 @@ from litellm.llms.base_llm.audio_transcription.transformation import (
from litellm.llms.custom_httpx.http_handler import HTTPHandler
from litellm.llms.xai.audio_transcription.transformation import (
XAIAudioTranscriptionConfig,
+ XAIAudioTranscriptionError,
)
from litellm.types.utils import LlmProviders
from litellm.utils import ProviderConfigManager
@@ -130,6 +131,21 @@ def test_transform_response_maps_xai_shape():
assert response._hidden_params["audio_transcription_duration"] == 3.2
+def test_transform_response_raises_on_error_status():
+ raw = httpx.Response(
+ 400,
+ json={
+ "code": "Client specified an invalid argument",
+ "error": "Incorrect API key provided",
+ },
+ request=httpx.Request("POST", "https://api.x.ai/v1/stt"),
+ )
+ with pytest.raises(XAIAudioTranscriptionError) as exc:
+ CONFIG.transform_audio_transcription_response(raw_response=raw)
+ assert exc.value.status_code == 400
+ assert "Incorrect API key provided" in exc.value.message
+
+
def test_transcription_routes_to_xai_stt(monkeypatch):
monkeypatch.delenv("XAI_API_KEY", raising=False)
monkeypatch.setattr(litellm, "xai_key", None)