Repository navigation
Description
Parent Issue
Sub-issue of #4651 — Translate API cost optimization (target: 84–89% reduction)
Objective
Cost saving: Current comma-based sentence splitting creates 3–5 tiny fragments per segment, each sent as a separate API call. Fixing the split and batching uncached sentences into one call reduces overhead and improves translation quality.
Problem
Bad splitting
split_into_sentences() at translation.py:244 uses:
sentences = re.findall(r'[^.?!,]+(?:[.?!,]\s*|\s*$)', text)This splits on commas — "Hello, how are you, nice to meet you" becomes 3 fragments. Tiny fragments:
- Have worse translation quality (no context)
- Create cache ambiguity for short text ("no", "pan", "gift" can auto-detect as different languages)
- Generate 3–5x API round-trips per segment
No batching
translate_text() at line 298 sends contents=[text] — a list with 1 item. The API supports up to 1024 items per request. Each sentence goes as a separate API call.
Changes Required
1. Remove comma from sentence splitting
# Before (line 244):
sentences = re.findall(r'[^.?!,]+(?:[.?!,]\s*|\s*$)', text)
# After — split on .?! and newlines only:
sentences = re.findall(r'[^.?!\n]+(?:[.?!]\s*|\s*$)', text)Add length-based fallback: if any sentence exceeds 300 chars, split at nearest clause boundary.
2. Batch cache-miss sentences into one API call
In translate_text_by_sentence():
def translate_text_by_sentence(self, dest_language, text):
sentences = split_into_sentences(text)
results = {}
misses = []
# Check cache for each sentence
for i, sentence in enumerate(sentences):
cached = get_cached_translation(md5(sentence), dest_language)
if cached:
results[i] = cached["text"]
else:
misses.append((i, sentence))
# Batch translate all cache misses in ONE call
if misses:
response = _client.translate_text(
contents=[s for _, s in misses],
parent=_parent,
target_language_code=dest_language,
)
for (i, sentence), translation in zip(misses, response.translations):
results[i] = translation.translated_text
cache_translation(md5(sentence), dest_language, translation.translated_text,
translation.detected_language_code)
return ' '.join(results[i] for i in sorted(results))3. Respect API limits
- Google Translate v3 supports up to 1024 content items or 30K chars per request
- Chunk if needed (unlikely for single segments but add safety check)
Files to Modify
| File | Change |
|---|---|
backend/utils/translation.py |
Fix split_into_sentences() regex, rewrite translate_text_by_sentence() with batching |
Test Plan
- Commas no longer split sentences ("Hello, how are you" stays as one unit)
- Periods, question marks, exclamation marks still split correctly
- Long text (>300 chars) gets length-based splitting
- Multiple cache misses batched into single API call
- Mixed cache hits/misses produce correct ordered output
- Translation quality improved for comma-separated clauses
Acceptance Criteria
- No comma-based splitting
- Cache misses batched into single API call
- Correct translation output regardless of cache state
Dependencies
- Requires sub-issue 3 (Redis cache) to be implemented first for the cache check/store logic
Sub-issue 4/4 of #4651. Lowest priority — overhead/latency/quality improvement. Implement last.
Activity
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…