omi icon indicating copy to clipboard operation
omi copied to clipboard

Make LC3 transcribe dependency optional

Open tianmind-studio opened this issue 5 months ago • 2 comments

Summary

  • allow routers/transcribe.py to import when lc3py is 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.py
  • git diff --check origin/main..HEAD

Notes

  • Checked the changed files locally for bidi control characters; none were present in the added diff.

tianmind-studio avatar Jun 06 '26 09:06 tianmind-studio

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 lc3 is now wrapped in try/except ImportError, setting lc3 = None as the fallback so the module loads on any platform.
  • Runtime guard: The elif codec == 'lc3': branch now checks if 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

greptile-apps avatar Jun 06 '26 09:06 greptile-apps

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.

tianmind-studio avatar Jun 08 '26 14:06 tianmind-studio