Skip to content

perf: eliminate redundant Translate API calls — detection double-billing, segment re-translation, no persistent cache #4651

Description

@beastoin

Problem

The listen WebSocket endpoint (transcribe.py:1508) calls Google Cloud Translate API with three compounding inefficiencies that together inflate translation costs ~6–8x above necessary levels:

  1. Detection double-billing (~43% of spend is redundant): Every segment triggers an explicit detect_language() API call (translation.py:191), then translate_text() auto-detects the source language again for free (no source_language_code is set at line 298). Both are billed at the same per-character rate. The detection call is pure waste.

  2. Growing segments re-translated 5–10x: The 0.6s processing loop (transcribe.py:1414) re-sends the full segment text on every tick as Deepgram adds words. Early words in a segment are billed 5–10 times as the segment grows.

  3. No persistent cache: Translation cache is an in-memory per-session OrderedDict (translation.py:250), max 1000 items, lost on disconnect. Detection cache is global in-memory, lost on pod restart. Zero cross-user or cross-session reuse. Redis exists in the codebase for other features but is not used for translation.

  4. Per-sentence API calls with comma splitting: split_into_sentences() (line 244) splits on .?!, — commas create 3–5 tiny fragments per segment, each sent as a separate translate_text() call. The API supports contents=[] lists (line 299) but code only sends 1 item per call.

Code Flow

stream_transcript_process() [transcribe.py:1414]
  └─ every 0.6s, for each updated segment:
      └─ translate(updated_segments) [line 1508]
          ├─ detect_language(text) [translation_cache.py:26]
          │   └─ _client.detect_language() ← PAID, redundant
          └─ translate_text_by_sentence(text) [translation.py:269]
              ├─ split_into_sentences() ← splits on commas
              └─ for each sentence:
                  └─ _client.translate_text(contents=[text]) ← PAID, 1 item at a time

Solution (4 changes, Codex-reviewed)

Change 1: Eliminate explicit detect_language API

  • Remove _client.detect_language() calls entirely
  • Use response.translations[0].detected_language_code from translate response (free)
  • Replace translated_text == segment_text check (line 1155) with detected_language_code == target_language — Translate normalizes punctuation/spacing even for same-language text
  • Impact: ~43% reduction (eliminates entire detection cost line)

Change 2: Per-segment debounce queue

  • Translate immediately on first segment appearance (zero UX delay)
  • Debounce updates with 0.8–1.2s trailing window before re-translating
  • If STT marks segment as "final," translate immediately (skip debounce)
  • Track segment_version / last_text_hash to prevent stale out-of-order responses
  • Pattern: per-segment tracking map, not asyncio queue:
    pending[segment_id] = {last_text, last_hash, last_update_at, task}
  • Impact: 60–80% reduction in translation characters

Change 3: Sentence-level Redis cache

  • Key: translate:v1:{md5(sentence)}:{dest_lang} → translated text
  • TTL: 7–30 days + Redis LRU eviction
  • Sentence granularity provides best cache hits (intra-session growing segments + cross-user common phrases)
  • Store detected_language_code alongside cached translation for ambiguous short text
  • Impact: additional 10–15% reduction on remaining characters

Change 4: Fix sentence splitting + batch API calls

  • Remove comma-based splitting — creates tiny ambiguous fragments with worse translation quality
  • Split on .?! and newlines only, length-based fallback (max 200–300 chars)
  • Batch all cache-miss sentences into one contents=[miss1, miss2, ...] call
  • Impact: overhead/latency reduction, better translation quality

Correctness Guardrails

  1. Use detected_language_code == target_language instead of text equality — Translate normalizes punctuation/spacing/quotes
  2. Track segment version to prevent out-of-order responses overwriting newer translations
  3. Version cache keys (translate:v1:...) for future compatibility
  4. Mixed-language segments: auto-detect picks dominant language (pre-existing limitation, unchanged)

Files to Modify

File Changes
backend/utils/translation.py Remove detect API, add Redis cache, fix splitting, add batching
backend/utils/translation_cache.py Simplify or remove (detection no longer needed)
backend/routers/transcribe.py Add debounce queue in translate(), use detected_language_code
backend/database/redis_db.py Add translation cache helpers (follows existing cache_signed_url pattern)

Estimated Combined Impact

Change Reduction
Eliminate detection ~43%
Dedup queue ~35–40% of remainder
Redis cache ~10–15% of remainder
Total ~84–89%

Analysis by @Chen (RepoOps), Codex-reviewed. All code references verified against current main.

Activity

  1. added
    enhancement
    backend
    understand
    p1
    maintainer
    on Feb 7, 2026
  2. beastoin commented on Feb 10, 2026

    @beastoin
    CollaboratorAuthor

    Deep Code Analysis — 4-Change Optimization (Codex-reviewed)

    Traced the full call chain from the listen WebSocket endpoint through translation. Found 5 root causes, proposing 4 changes with estimated 84–89% cost reduction.

    Root Causes Verified in Code

    # Root Cause Location Impact
    1 Double-paying for detection — explicit detect_language() API + translate auto-detects for free translation.py:191 + translation.py:298 (no source_language_code set) ~43% of total spend is redundant detection
    2 Re-translating growing segments 5–10x — 0.6s loop re-sends full segment text as Deepgram adds words transcribe.py:1414,1508 Same chars billed 5–10x per segment
    3 No persistent cache — in-memory only, per-session, max 1000 items translation.py:250 (per-instance OrderedDict) Zero cross-user/cross-session reuse
    4 Per-sentence API calls — comma-based splitting creates 3–5x call multiplication translation.py:244,269 Tiny fragments, worse translation quality
    5 ≤5 word threshold sends short text to paid detection translation.py:220 Short utterances always hit paid API

    Key Finding: Detection API is Pure Waste

    translate_text() at line 298 sends no source_language_code. Per Google's pricing:

    "If you don't specify a source language, Cloud Translation detects the source language for you with no additional charge."

    The response includes detected_language_code (currently ignored at line 305). Detection and translation are both $20/M chars — so every foreign-language segment is billed 2x (detect + translate) when translate alone suffices.

    Proposed Solution (4 Changes)

    Change 1: Eliminate explicit detect_language API

    • Remove _client.detect_language() calls entirely
    • Use response.translations[0].detected_language_code from translate response (free)
    • Replace brittle translated_text == segment_text check (line 1155) with detected_language_code == target_language — Codex flagged that Translate normalizes punctuation/spacing even for same-language text
    • Estimated savings: ~43% of total spend

    Change 2: Per-segment debounce queue (dedup growing segments)

    • Translate immediately on first segment appearance (zero UX delay)
    • Debounce updates: 0.8–1.2s trailing window before re-translating
    • If STT marks segment as "final," translate immediately (skip debounce)
    • Pattern (Codex-recommended): per-segment tracking map, not asyncio queue:
      pending[segment_id] = {last_text, last_hash, last_update_at, task}
      # On update: cancel/reschedule debounce task
      # On fire: translate only if last_hash unchanged since task creation
    • Track segment_version to prevent stale out-of-order responses from overwriting newer translations
    • Estimated savings: 60–80% of translation chars

    Change 3: Sentence-level Redis cache

    • Key: translate:v1:{md5(sentence)}:{dest_lang} → translated text
    • TTL: 7–30 days + Redis LRU eviction (volatile-ttl)
    • Sentence granularity provides best cache hits:
      • Intra-session: early sentences of growing segments hit cache on subsequent ticks
      • Cross-user: common phrases cached globally
    • Include detected_language_code in cache value to avoid ambiguity on short text (Codex: "pan", "no", "gift" can auto-detect differently)
    • Prefix keys with version (v1:) for future compatibility
    • Estimated savings: additional 10–15% on remaining chars

    Change 4: Fix sentence splitting + batch API calls

    • Remove comma-based splitting — commas create tiny ambiguous fragments with worse translation quality and cache churn
    • Split on .?! and newlines only, with length-based fallback (max 200–300 chars per chunk)
    • Batch all cache-miss sentences into one contents=[miss1, miss2, ...] call — API already supports lists at line 299 but code only sends 1
    • Respect API per-request limits (chunk if needed)
    • Impact: overhead/latency reduction, better translation quality

    Correctness Guardrails (from Codex review)

    1. Use detected_language_code == target_language instead of translated_text == segment_text — Translate normalizes punctuation/spacing/quotes even for same-language text
    2. Track segment version to prevent out-of-order responses from overwriting newer translations
    3. Version cache keys (translate:v1:...) for future compatibility if splitting strategy or model changes
    4. Mixed-language segments: auto-detect picks dominant language — pre-existing limitation, unchanged by this optimization

    Files to Modify

    File Changes
    backend/utils/translation.py Remove detect_language API calls, add Redis cache, fix sentence splitting, add batching
    backend/utils/translation_cache.py Simplify or remove (detection no longer needed)
    backend/routers/transcribe.py Add debounce queue in translate() function, use detected_language_code
    backend/database/redis_db.py Add translation cache helpers (follows existing cache_signed_url pattern)

    Combined Impact

    Change Reduction
    Eliminate detection ~43%
    Dedup queue ~35–40% of remainder
    Redis cache ~10–15% of remainder
    Total ~84–89%

    Analysis by @Chen (RepoOps) with Codex review. Traced from transcribe.py:1508 → translation_cache.py:26 → translation.py:191,298. All code references verified.

  3. changed the title [-]perf: cache Cloud Translate API calls to reduce redundant translation requests[/-] [+]perf: eliminate redundant Translate API calls — detection double-billing, segment re-translation, no persistent cache[/+] on Feb 10, 2026
  4. beastoin commented on Feb 10, 2026

    @beastoin
    CollaboratorAuthor

    Sub-Issues (implement in order)

    # Issue Change Priority Impact
    1 #4712 Eliminate redundant detect_language API P1 ~43% reduction
    2 #4713 Debounce translation for growing segments P1 60–80% of remaining chars
    3 #4714 Add Redis persistent translation cache P1 10–15% of remaining chars
    4 #4715 Fix sentence splitting + batch API calls P2 Overhead/quality improvement

    Order matters: #4712 first (largest impact, changes translate response shape), then #4713 (uses new response), then #4714 (adds cache layer), then #4715 (uses cache in batching).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendenhancementmaintainerp1performanceunderstand

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions