Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

ASR errors: cut the response body from the middle so the exception line survives

jjsmackay63b505463bfd
diff --git a/src/services/transcription/connectors/asr_endpoint.py b/src/services/transcription/connectors/asr_endpoint.py
index 22a314e7..68adca2c 100644
--- a/src/services/transcription/connectors/asr_endpoint.py
+++ b/src/services/transcription/connectors/asr_endpoint.py
@@ -20,6 +20,7 @@ from ..base import (
)
from ..exceptions import TranscriptionError, ConfigurationError, ProviderError
from src.config.app_config import ASR_ENABLE_CHUNKING, ASR_MAX_DURATION_SECONDS
+from src.utils.error_formatting import truncate_middle
logger = logging.getLogger(__name__)
@@ -227,9 +228,9 @@ class ASREndpointConnector(BaseTranscriptionConnector):
body = e.response.text
except Exception:
body = ''
- body_excerpt = body.strip()
- if len(body_excerpt) > 800:
- body_excerpt = body_excerpt[:800] + '...'
+ # Keep both ends: a worker traceback names the exception on its
+ # last line, and a cut from the start threw exactly that away.
+ body_excerpt = truncate_middle(body.strip(), 800)
logger.error(
f"ASR request for {request.filename!r} failed with status "
f"{e.response.status_code}: {body_excerpt}"
diff --git a/src/utils/error_formatting.py b/src/utils/error_formatting.py
index cadb5153..836001ab 100644
--- a/src/utils/error_formatting.py
+++ b/src/utils/error_formatting.py
@@ -308,6 +308,23 @@ def format_error_for_user(error_text: str) -> Dict:
}
+def truncate_middle(text: str, limit: int, tail_share: float = 0.6) -> str:
+ """Cut `text` to at most `limit` characters, keeping both ends.
+
+ A traceback says why it failed on its last line, so a cut that keeps
+ only the start loses the one line worth reading. Most of the budget
+ goes to the end (`tail_share`); the rest keeps the first lines, which
+ name the request that failed.
+ """
+ if len(text) <= limit:
+ return text
+ marker = ' … '
+ budget = max(limit - len(marker), 0)
+ tail = int(budget * tail_share)
+ head = budget - tail
+ return text[:head] + marker + text[len(text) - tail:]
+
+
def format_error_for_storage(error_text: str) -> str:
"""
Format an error message for storage in the database.
@@ -334,7 +351,9 @@ def format_error_for_storage(error_text: str) -> str:
# "Technical details" panel has something to show even for unknown errors
# where the user-facing message is the same as the raw text.
if formatted['technical']:
- error_data['d'] = formatted['technical'][:500] # Limit technical detail length
+ # Limit technical detail length, keeping the end: that is where a
+ # traceback names the exception.
+ error_data['d'] = truncate_middle(formatted['technical'], 500)
try:
json_str = json.dumps(error_data, ensure_ascii=False)
diff --git a/tests/test_asr_error_excerpt.py b/tests/test_asr_error_excerpt.py
new file mode 100644
index 00000000..6d8e6b9f
--- /dev/null
+++ b/tests/test_asr_error_excerpt.py
@@ -0,0 +1,67 @@
+"""An ASR error body is cut from the end, so the exception line survives.
+
+A 5xx from the ASR endpoint usually carries the worker's traceback. The
+body was cut to its first 800 characters, then the stored technical detail
+to 500, both from the start: what reached the recording was the top of the
+stack, and the one line that said why (the exception at the bottom) was gone.
+"""
+
+import os
+import sys
+from unittest.mock import patch
+
+import httpx
+import pytest
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.services.transcription.connectors.asr_endpoint import ASREndpointConnector
+from src.services.transcription.exceptions import ProviderError
+from src.utils.error_formatting import format_error_for_storage, truncate_middle
+
+FRAME = ' File "/workspace/app/pipeline.py", line 313, in transcribe\n result = whisper_model.transcribe(audio)\n'
+TRACEBACK = 'worker error:\nTraceback (most recent call last):\n' + FRAME * 30 + 'RuntimeError: CUDA out of memory'
+
+
+def test_truncate_middle_keeps_both_ends_and_respects_the_limit():
+ text = 'A' * 1000 + 'Z' * 1000
+ out = truncate_middle(text, 500)
+ assert len(out) <= 500
+ assert out.startswith('AAA') and out.endswith('ZZZ')
+ assert ' … ' in out
+ assert truncate_middle('short', 500) == 'short'
+
+
+def test_stored_technical_detail_ends_with_the_exception():
+ stored = format_error_for_storage(f'ASR request failed with status 500: {TRACEBACK}')
+ assert 'RuntimeError: CUDA out of memory' in stored
+ assert 'Traceback (most recent call last)' in stored
+
+
+class _Client:
+ def __init__(self, body):
+ self.body = body
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ return False
+
+ def post(self, url, **kwargs):
+ request = httpx.Request('POST', url)
+ return httpx.Response(500, request=request, text=self.body)
+
+
+def test_provider_error_carries_the_exception_from_a_long_body():
+ connector = ASREndpointConnector({'base_url': 'http://asr.test:9000', 'timeout': 5})
+ body = '{"detail":"' + TRACEBACK.replace('\n', '\\n') + '"}'
+ from src.services.transcription.base import TranscriptionRequest
+ import io
+ req = TranscriptionRequest(audio_file=io.BytesIO(b'x'), filename='a.mp3')
+ with patch('src.services.transcription.connectors.asr_endpoint.httpx.Client', return_value=_Client(body)), \
+ patch.object(ASREndpointConnector, 'timeout', 5):
+ with pytest.raises(ProviderError) as exc:
+ connector.transcribe(req)
+ assert 'status 500' in str(exc.value)
+ assert 'RuntimeError: CUDA out of memory' in str(exc.value)