fix(xai): reject non-success stt responses before parsing

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
kerry 2026-09-19 01:22:25 +00:00
parent a124e079c3
commit 99d91d7205
2 changed files with 23 additions and 0 deletions

View file

@ -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:

View file

@ -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)