From 99d91d72056a43a95e6f377e9dda02df69d111d5 Mon Sep 17 00:00:00 2001 From: kerry Date: Sat, 19 Sep 2026 01:22:25 +0000 Subject: [PATCH] fix(xai): reject non-success stt responses before parsing Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../xai/audio_transcription/transformation.py | 7 +++++++ ...est_xai_audio_transcription_transformation.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) 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)