Make LC3 transcribe dependency optional
Summary
- allow
routers/transcribe.pyto import whenlc3pyis not installed - close LC3 websocket sessions with a clear 1011 error when the LC3 codec is requested without the dependency
- add focused coverage for optional LC3 import behavior
Verification
python -m pytest tests\unit\test_transcribe_lc3_optional.py -q- 2 passed
python -m black routers\transcribe.py tests\unit\test_transcribe_lc3_optional.py --check- 2 files would be left unchanged
python -m py_compile routers\transcribe.py tests\unit\test_transcribe_lc3_optional.pygit diff --check origin/main..HEAD
Notes
- Checked the changed files locally for bidi control characters; none were present in the added diff.
Greptile Summary
This PR guards the lc3 (lc3py) top-level import with a try/except ImportError, preventing ImportError from crashing routers/transcribe.py on startup when lc3py is not installed. A focused early-exit guard was also added inside _stream_handler to close the WebSocket with code 1011 and a clear reason string when an LC3 stream is requested on a host without lc3py.
- Optional import:
import lc3is now wrapped intry/except ImportError, settinglc3 = Noneas the fallback so the module loads on any platform. - Runtime guard: The
elif codec == 'lc3':branch now checksif lc3 is None:and closes the WebSocket with code 1011 before returning, matching the existing early-exit pattern already used elsewhere in this function.
Confidence Score: 5/5
Safe to merge — the change is minimal, well-scoped, and follows existing patterns in the file.
The two-part change is straightforward: a standard optional-import guard at module level, and a runtime early-exit that mirrors the identical early-exit pattern already used at line 987. The early return at line 2383 happens before STT sockets, asyncio tasks, Pusher connections, or the active-connection metric are initialised, so nothing is leaked when lc3py is absent. The WebSocket is explicitly closed with the correct close code before returning, and the lc3_decoder usage inside receive_data at line 2533 is only reachable when the decoder was successfully initialised.
No files require special attention.
Important Files Changed
| Filename | Overview |
|---|---|
| backend/routers/transcribe.py | Top-level lc3 import made optional via try/except; runtime guard added to close WebSocket 1011 when LC3 is requested but unavailable. Early return occurs before any resources (STT socket, tasks, metrics) are initialized, so no cleanup is skipped. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Client connects via WebSocket] --> B{codec == 'lc3'?}
B -- No --> C[Initialize other decoder\nopus / aac / none]
B -- Yes --> D{lc3 is None?}
D -- Yes\n lc3py not installed --> E[log error\nclose WS code=1011\nreason='LC3 codec is not available'\nreturn]
D -- No\n lc3py installed --> F[lc3_decoder = lc3.Decoder\nlc3_frame_duration_us, sample_rate]
C --> G[Create asyncio tasks\nSTT socket, heartbeat, etc.]
F --> G
G --> H[supervise_tasks → session active]
H --> I[finally: cleanup\nclose WS, record usage]
E --> J[Connection closed — no resources to clean up]
Reviews (1): Last reviewed commit: "fix(backend): make LC3 import optional" | Re-trigger Greptile
Rebased and force-pushed onto current main. Focused verification passes: python -m pytest tests\unit\test_transcribe_lc3_optional.py -q -> 2 passed. I also checked the added diff for bidi control characters; none were introduced.