Skip to content

perf: eliminate redundant detect_language API calls (sub-issue 1/4 of #4651) #4712

Closed

Description

@beastoin

Parent Issue

Sub-issue of #4651 — Translate API cost optimization (target: 84–89% reduction)

Objective

Cost saving: The explicit detect_language() API accounts for ~43% of total Translate spend. Eliminating it is the single largest cost lever.

Problem

translate_text() at translation.py:298 sets no source_language_code. Google auto-detects the source language for free inside every translate call and returns detected_language_code in the response. But the code ALSO calls _client.detect_language() explicitly at translation.py:191, paying $20/M chars for detection that translate already does for free.

Every foreign-language segment is billed 2x: once for detect, once for translate. Same per-character rate — pure waste.

Changes Required

1. Remove explicit detection path

  • backend/utils/translation.py: Remove _detect_with_google_cloud() function (lines 189-196) and all _client.detect_language() calls
  • backend/utils/translation_cache.py: Remove or simplify TranscriptSegmentLanguageCache — detection is no longer needed as a pre-filter

2. Use detected_language_code from translate response

  • In translate_text() (translation.py:273-316): capture response.translations[0].detected_language_code
  • Return both translated text AND detected language code

3. Fix same-language check in transcribe.py

  • backend/routers/transcribe.py:1155: Replace brittle translated_text == segment_text with detected_language_code == target_language
  • Google Translate normalizes punctuation/spacing/quotes even for same-language text — equality check produces false negatives

4. Keep free langdetect for any non-translation language needs

  • If any other code path needs language detection without translation, use the free langdetect library (already imported)

Files to Modify

File Change
backend/utils/translation.py Remove _detect_with_google_cloud, update translate_text() return
backend/utils/translation_cache.py Simplify or remove
backend/routers/transcribe.py Use detected_language_code instead of text equality

Test Plan

  • Verify translation still works for non-target-language segments
  • Verify target-language segments are correctly identified via detected_language_code
  • Verify no detect_language API calls in Cloud Console after deploy
  • Run existing translation tests

Acceptance Criteria

  • Zero detect_language API calls to Google Cloud
  • Translation behavior unchanged for users
  • detected_language_code used for same-language detection

Sub-issue 1/4 of #4651. Do this FIRST — largest single impact (~43% reduction).

Metadata

Metadata

Assignees

No one assigned

    Labels

    intelligencemaintainerp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions