Repository navigation
fix(sync): treat empty DG transcription as success, not error (#6100) - #6106
Conversation
…#6100) Previously returned [] on both "no speech" and "failure after retries", making callers unable to distinguish success from failure. Now raises RuntimeError on retry exhaustion, consistent with deepgram_prerecorded_from_bytes. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Empty Deepgram words now treated as successful-but-empty segment (silence/noise audio) instead of appending to errors list. Real DG failures raise RuntimeError and are caught by the existing except block. Prevents false 500s when all segments contain only silence. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Updated structural tests, behavioral tests, and retry behavior tests to match new semantics: empty DG words = success, retry exhaustion = raise. 33/33 tests passing. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Greptile SummaryThis PR fixes a false 500 error in Key changes:
The result matrix is now correct: silence → 200, mixed → 207, all real DG failures → 500. Confidence Score: 5/5Safe to merge — the fix is logically sound, well-tested, and the only callers of All identified findings are P2 style suggestions (missing No files require special attention; Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[process_segment called] --> B[deepgram_prerecorded\nattempts=0]
B --> C{DG API call}
C -- success,\nwords found --> D[return words, lang]
C -- success,\nno words silence --> E["return [], lang\n(valid empty)"]
C -- exception --> F{attempts < 2?}
F -- yes --> G[retry\nattempts+1]
G --> C
F -- no --> H["raise RuntimeError\n'failed after N attempts'"]
D --> I[postprocess_words]
I --> J{transcript_segments empty?}
J -- yes --> K[log warning, return success]
J -- no --> L[create/update conversation]
L --> M[200 / 207 OK]
E --> N[log info, return early success]
N --> M
H --> O["except Exception in process_segment"]
O --> P[errors.append / Failed to process segment]
P --> Q[500 / 207 error]
Reviews (1): Last reviewed commit: "test(sync): update tests for empty-words..." | Re-trigger Greptile |
| if return_language: | ||
| return [], 'en' | ||
| return [] | ||
| raise RuntimeError(f'Deepgram transcription failed after {attempts + 1} attempts: {e}') |
There was a problem hiding this comment.
Consider explicit exception chaining with
from e
Raising RuntimeError inside an except block without from e causes Python to set __context__ implicitly, displaying a "During handling of the above exception, another exception occurred" message in tracebacks — which can be confusing. Using raise ... from e signals intent clearly, suppresses that extra noise, and is more idiomatic Python.
| raise RuntimeError(f'Deepgram transcription failed after {attempts + 1} attempts: {e}') | |
| raise RuntimeError(f'Deepgram transcription failed after {attempts + 1} attempts: {e}') from e |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| '# Convert Deepgram format' | ||
| ) | ||
| ] |
There was a problem hiding this comment.
Redundant assertion —
'return []' is a substring of the previous assertion
assert 'return []' in no_words_block will always pass whenever assert "return [], detected_lang or 'en'" in no_words_block passes, because 'return []' is literally a prefix of "return [], detected_lang or 'en'". The third assertion adds no additional coverage and can be removed to reduce noise.
| '# Convert Deepgram format' | |
| ) | |
| ] | |
| assert 'if not dg_words:' in no_words_block | |
| assert 'return [], detected_lang or \'en\'' in no_words_block |
…atrix (#6100) Addresses tester feedback: - TestDeepgramRetryBehavioral: executes real deepgram_prerecorded with mocked DG client — verifies RuntimeError on retry exhaustion, [] on valid empty. - test_all_silent_segments_return_200_not_500: exercises status code through real process_segment calls — proves the core #6100 bug is fixed. - test_runtime_error_from_dg_becomes_segment_error: verifies RuntimeError propagation from DG retry exhaustion through process_segment's except block. 37/37 tests passing. Co-Authored-By: Claude Opus 4.6 <[email protected]>
CP9 Live Test Evidence — Changed-Path Coverage ChecklistChanged Paths
L1 Evidence (Backend standalone)Ran 7/7 L1 integration tests exercising the actual runtime code paths (not static analysis): Plus 37/37 unit tests passing: L2 Evidence (Backend + App integrated)Backend → App response contract verified:
TestProcessSegmentReal (37 tests) runs the actual
L1 SynthesisAll changed paths (P1, P2) proven at runtime. P1 empty-words branch returns success with 0 errors. P2 retry exhaustion raises RuntimeError after 3 attempts, caught by process_segment's except block. Status code matrix verified: all-silent→200, mixed→207, all-failed→500. No paths untested. L2 SynthesisBackend response contract matches app-side by AI for @beastoin |
…sages Voice message functions in utils/chat.py also call deepgram_prerecorded but had no try/except for RuntimeError. Now that retry exhaustion raises instead of returning [], these callers need protection to preserve their existing graceful-failure behavior (return None/empty). Co-Authored-By: Claude Opus 4.6 <[email protected]>
Covers the 3 chat.py callers of deepgram_prerecorded: - transcribe_voice_message_segment returns (None, lang) on RuntimeError - process_voice_message_segment returns [] on RuntimeError - process_voice_message_segment_stream yields nothing on RuntimeError 40/40 tests passing. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Updated Test Results (post chat.py fix)Unit + Behavioral Tests: 40/40 passingReview cycles
by AI for @beastoin |
|
lgtm |
…ardware#6100) (BasedHardware#6106) ## Summary Fixes BasedHardware#6100 — backend-sync returns 500 instead of 207 when Deepgram returns empty transcription. **Root cause:** `deepgram_prerecorded` returned `([], lang)` for BOTH "no speech detected" (valid success) AND "failure after 3 retries" (real error), making them indistinguishable. `process_segment` treated all empty results as errors, causing false 500s for silence/noise audio. **Fix:** - `deepgram_prerecorded` now raises `RuntimeError` on retry exhaustion (consistent with `deepgram_prerecorded_from_bytes`) - `process_segment` treats empty DG words as success (silence/noise), not error - `utils/chat.py` voice message functions wrapped with try/except RuntimeError to preserve graceful failure - Status code matrix: all-silent→200, mixed→207, all-failed→500 ## Changes | File | Change | |------|--------| | `backend/utils/stt/pre_recorded.py` | Retry exhaustion raises `RuntimeError` instead of returning `[]` | | `backend/routers/sync.py` | Empty DG words treated as success, not error | | `backend/utils/chat.py` | Voice message functions handle `RuntimeError` from DG retry exhaustion | | `backend/tests/unit/test_sync_silent_failure.py` | Updated + new tests (40 total) | ## Callers of `deepgram_prerecorded` (exhaustive search via `git grep`) | Caller | File | Protected? | |--------|------|-----------| | `process_segment` | `routers/sync.py:655` | Yes — existing try/except (line 747) | | `transcribe_voice_message_segment` | `utils/chat.py:72-79` | Yes — **new** try/except added | | `process_voice_message_segment` | `utils/chat.py:119` | Yes — **new** try/except added | | `process_voice_message_segment_stream` | `utils/chat.py:208` | Yes — **new** try/except added | ## Test plan - [x] 40/40 unit + behavioral tests passing - [x] 7/7 L1 integration tests passing (runtime code paths with mocked DG) - [x] L2 contract verification: app `SyncLocalFilesResponse` handles 200/207/500 correctly - [x] Voice message callers verified — all 3 protected with try/except + tested - [x] CI lint check passed - [x] Reviewer: PR_APPROVED_LGTM (2 rounds — 2nd round after chat.py fix) - [x] Tester: TESTS_APPROVED (40/40) ## Deployment steps This is a **backend-only** change (`backend/` files). No app or desktop changes. ### After merge to main: 1. **Auto-deploy to dev** — `gcp_backend_auto_dev.yml` triggers automatically on push to `main` with `backend/**` changes ```bash # Verify dev deploy: gh run list --workflow=gcp_backend_auto_dev.yml --limit=3 ``` 2. **Verify on dev** — confirm sync endpoint returns 200 for silent audio (was 500), voice messages still work 3. **Manual deploy to prod** — after dev verification: ```bash gh workflow run gcp_backend.yml -f environment=prod -f branch=main ``` 4. **Verify on prod** — monitor sync endpoint logs for correct status codes, check voice message transcription still functional ### Rollback plan: - If issues found: redeploy previous commit to prod: ```bash gh workflow run gcp_backend.yml -f environment=prod -f branch=<previous-commit-sha> ``` - The change is backward-compatible — no app update required, no API contract changes ## Checkpoints | CP | Status | |----|--------| | CP0-CP9B | ✅ All complete | | CP9C | N/A (no remote infra deps) | ## Risks - `fal_whisperx` still silently returns `[]` on retry exhaustion (pre-existing, not in scope) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary
Fixes #6100 — backend-sync returns 500 instead of 207 when Deepgram returns empty transcription.
Root cause:
deepgram_prerecordedreturned([], lang)for BOTH "no speech detected" (valid success) AND "failure after 3 retries" (real error), making them indistinguishable.process_segmenttreated all empty results as errors, causing false 500s for silence/noise audio.Fix:
deepgram_prerecordednow raisesRuntimeErroron retry exhaustion (consistent withdeepgram_prerecorded_from_bytes)process_segmenttreats empty DG words as success (silence/noise), not errorutils/chat.pyvoice message functions wrapped with try/except RuntimeError to preserve graceful failureChanges
backend/utils/stt/pre_recorded.pyRuntimeErrorinstead of returning[]backend/routers/sync.pybackend/utils/chat.pyRuntimeErrorfrom DG retry exhaustionbackend/tests/unit/test_sync_silent_failure.pyCallers of
deepgram_prerecorded(exhaustive search viagit grep)process_segmentrouters/sync.py:655transcribe_voice_message_segmentutils/chat.py:72-79process_voice_message_segmentutils/chat.py:119process_voice_message_segment_streamutils/chat.py:208Test plan
SyncLocalFilesResponsehandles 200/207/500 correctlyDeployment steps
This is a backend-only change (
backend/files). No app or desktop changes.After merge to main:
gcp_backend_auto_dev.ymltriggers automatically on push tomainwithbackend/**changes# Verify dev deploy: gh run list --workflow=gcp_backend_auto_dev.yml --limit=3Rollback plan:
Checkpoints
Risks
fal_whisperxstill silently returns[]on retry exhaustion (pre-existing, not in scope)🤖 Generated with Claude Code