Skip to content

The agent that grows with you

fix(stt): keep the provider's 5xx when no transcode is possible; trim tests

teknium14b4e8ed4bdb3
diff --git a/tests/tools/test_managed_media_gateways.py b/tests/tools/test_managed_media_gateways.py
index 46a4b5b1b2a..e57bd408b73 100644
--- a/tests/tools/test_managed_media_gateways.py
+++ b/tests/tools/test_managed_media_gateways.py
@@ -177,6 +177,7 @@ def _install_fake_openai_module(captured, transcription_response=None):
APIConnectionError=Exception,
APITimeoutError=Exception,
BadRequestError=type("BadRequestError", (Exception,), {}),
+ APIStatusError=type("APIStatusError", (Exception,), {}),
)
sys.modules["openai"] = fake_module
diff --git a/tests/tools/test_transcription_tools.py b/tests/tools/test_transcription_tools.py
index 300155568c5..745e6b021a7 100644
--- a/tests/tools/test_transcription_tools.py
+++ b/tests/tools/test_transcription_tools.py
@@ -1559,29 +1559,19 @@ class TestTranscribeOpenaiFiveXxRetry:
assert result["transcript"] == "retried transcript"
assert mock_client.audio.transcriptions.create.call_count == 2
- def test_container_400_still_transcodes(self, sample_wav, tmp_path):
- """The original 400-with-container-hint path keeps working."""
- converted = tmp_path / "retry.m4a"
- converted.write_bytes(b"fake audio")
+ def test_server_error_without_transcode_keeps_provider_error(self, sample_wav):
+ """No ffmpeg: the 5xx surfaces as the provider's own error, not a transcode message,
+ and the file is not re-sent."""
mock_client = MagicMock()
- mock_client.audio.transcriptions.create.side_effect = [
- self._status_error(
- 400, "Invalid file format: unsupported audio container"
- ),
- "retried transcript",
- ]
+ mock_client.audio.transcriptions.create.side_effect = self._status_error(503, "503 system_error")
with patch("tools.transcription_tools._HAS_OPENAI", True), \
patch("openai.OpenAI", return_value=mock_client), \
- patch(
- "tools.transcription_cloud._transcode_audio_for_stt",
- return_value=(str(converted), None),
- ):
+ patch("tools.transcription_cloud._transcode_audio_for_stt",
+ return_value=(None, "ffmpeg not found")):
from tools.transcription_tools import _transcribe_openai
- result = _transcribe_openai(
- sample_wav, "whisper-1", api_key="sk-test"
- )
+ result = _transcribe_openai(sample_wav, "whisper-1", api_key="sk-test")
- assert result["success"] is True
- assert result["transcript"] == "retried transcript"
- assert mock_client.audio.transcriptions.create.call_count == 2
+ assert result["success"] is False
+ assert "503" in result["error"] and "ffmpeg" not in result["error"]
+ assert mock_client.audio.transcriptions.create.call_count == 1
diff --git a/tools/transcription_cloud.py b/tools/transcription_cloud.py
index 4c87d504218..ff8d5d36797 100644
--- a/tools/transcription_cloud.py
+++ b/tools/transcription_cloud.py
@@ -149,19 +149,17 @@ def _transcribe_openai(
try:
transcription = _create_transcription(file_path)
except APIStatusError as exc:
- message = str(exc).lower()
- # 400s with a container hint mean the audio container was rejected. Some
- # OpenAI-compatible endpoints (e.g. the gapgpt case in #81644) instead reject
- # unsupported containers with a 5xx, which never reached this retry path. 5xx is
- # otherwise ambiguous, but the transcode is cheap and the retry is a single
- # attempt, so escalate to it whenever the provider hints at a bad container OR
- # returned a 5xx.
+ # 400 + container hint is the documented rejection; some OpenAI-compatible endpoints
+ # reject a container with a bare 5xx instead (#81644). A 5xx is ambiguous, so it earns
+ # the same single transcode retry and, when no transcode is possible, its own error.
is_server_error = (exc.status_code or 0) >= 500
- if not is_server_error and not any(k in message for k in ("unsupported", "corrupted", "invalid file")):
+ if not is_server_error and not any(k in str(exc).lower() for k in ("unsupported", "corrupted", "invalid file")):
raise
# Newer models reject containers whisper-1 accepted (Ogg/Opus voice notes): transcode, retry once.
converted_path, transcode_error = _transcode_audio_for_stt(file_path, work_dir)
if transcode_error:
+ if is_server_error:
+ raise
return _error_result(transcode_error)
logger.info("Retrying %s STT after transcoding %s to m4a (API rejected the original container)",
provider_label, Path(file_path).name)