Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add retry layers around embedding API calls and re-embed pipeline
Murtaza Nasir07886d69a5f0
diff --git a/src/api/admin.py b/src/api/admin.pyindex 7bce24e2..2042de22 100644--- a/src/api/admin.py+++ b/src/api/admin.py@@ -1320,24 +1320,76 @@ def admin_process_recordings_for_inquire():processed = 0failed = []- for recording in recordings_needing_processing:+ # Number of times to retry each recording after the first pass. The+ # underlying _api_embed already retries transient errors; this layer+ # catches whole-pipeline failures (DB lock contention, partial+ # provider responses, anything that produced a False return).+ retry_passes = max(0, int(data.get('retry_passes', 2)))++ def _attempt_recording(recording, attempt_label):+ """Run process_recording_chunks for one recording.++ Returns ``(success: bool, error: Optional[str])``. Any exception+ is caught and converted to an error string so a single bad row+ cannot abort the whole sweep.+ """try:- success = process_recording_chunks(recording.id)- if success:- processed += 1- current_app.logger.info(- f"Admin API: {'Re-embedded' if force else 'Processed chunks for'} "- f"recording: {recording.title} ({recording.id})"- )- else:- failed.append({'id': recording.id, 'title': recording.title, 'reason': 'Processing returned false'})- except Exception as e:- current_app.logger.error(f"Admin API: Failed to process recording {recording.id}: {e}")- failed.append({'id': recording.id, 'title': recording.title, 'reason': str(e)})+ ok = process_recording_chunks(recording.id)+ except Exception as exc:+ current_app.logger.error(+ f"Admin API: Failed to process recording {recording.id} "+ f"({attempt_label}): {exc}"+ )+ return False, str(exc)+ if ok:+ current_app.logger.info(+ f"Admin API: {'Re-embedded' if force else 'Processed chunks for'} "+ f"recording ({attempt_label}): {recording.title} ({recording.id})"+ )+ return True, None+ return False, 'Processing returned false'++ # First pass: try every recording once.+ retry_queue = []+ for recording in recordings_needing_processing:+ ok, err = _attempt_recording(recording, 'pass 1')+ if ok:+ processed += 1+ else:+ retry_queue.append((recording, err))if processed % batch_size == 0:db.session.commit()+ # Subsequent passes: retry any recording that did not succeed.+ # Backoff between passes lets a temporarily overloaded provider+ # recover before we hammer it again.+ for pass_num in range(2, retry_passes + 2):+ if not retry_queue:+ break+ import time as _t+ _t.sleep(min(15, 2 ** (pass_num - 1)))+ current_app.logger.info(+ f"Admin API: retry pass {pass_num} on {len(retry_queue)} recordings"+ )+ still_failing = []+ for recording, prev_err in retry_queue:+ ok, err = _attempt_recording(recording, f'pass {pass_num}')+ if ok:+ processed += 1+ else:+ still_failing.append((recording, err or prev_err))+ retry_queue = still_failing++ # Anything still in retry_queue after the last pass is recorded as failed.+ for recording, err in retry_queue:+ failed.append({+ 'id': recording.id,+ 'title': recording.title,+ 'reason': err or 'Processing returned false',+ 'attempts': retry_passes + 1,+ })+db.session.commit()# On a successful force re-embed, refresh the stored identifier so thediff --git a/src/services/embeddings.py b/src/services/embeddings.pyindex 421c9c35..3e3de4f6 100644--- a/src/services/embeddings.py+++ b/src/services/embeddings.py@@ -3,6 +3,8 @@ Embedding generation and semantic search services."""import os+import time+import randomimport numpy as npfrom flask import current_appfrom sqlalchemy.orm import joinedload@@ -115,50 +117,103 @@ def get_embedding_api_client():return _embedding_api_client+_API_EMBED_MAX_ATTEMPTS = int(os.environ.get('EMBEDDING_API_MAX_RETRIES', '3'))+_API_EMBED_BASE_BACKOFF_SECONDS = float(os.environ.get('EMBEDDING_API_BACKOFF_SECONDS', '1.5'))++# Substrings of error messages that suggest the failure is transient and+# worth retrying. Auth and model-not-found errors do not match and fail fast.+_TRANSIENT_ERROR_HINTS = (+ 'timeout', 'timed out', 'connection', 'connect',+ 'rate limit', 'rate_limit', 'too many requests', '429',+ '500', '502', '503', '504',+ 'temporarily unavailable', 'service unavailable',+ 'overloaded', 'try again',+)+++def _is_transient_embedding_error(exc):+ """Decide whether an embedding-API error is worth retrying."""+ msg = str(exc).lower()+ return any(hint in msg for hint in _TRANSIENT_ERROR_HINTS)++def _api_embed(texts, user_id=None):"""Call the OpenAI-compatible embeddings endpoint and return numpy vectors.- When ``user_id`` is provided and the response includes a ``usage`` block,- record the call against the daily token-usage aggregate so admins can see- embedding cost alongside chat/summarization. Cost is captured when the- provider returns it (OpenRouter populates ``usage.cost``); for providers- that omit it, only token counts are recorded.+ Retries on transient errors (rate limits, timeouts, 5xx, connection+ blips) with exponential backoff. Auth or model-not-found errors fail+ fast since retrying will not help. On permanent failure, returns an+ empty list so callers see a clear sentinel; ``process_recording_chunks``+ treats a length mismatch between input and output as a failure and+ rolls back, so the recording's existing chunks are preserved instead+ of being silently deleted with nothing inserted in their place.++ Retry parameters are tunable via ``EMBEDDING_API_MAX_RETRIES`` (default+ 3) and ``EMBEDDING_API_BACKOFF_SECONDS`` (default 1.5).++ When ``user_id`` is provided and the response includes a ``usage``+ block, record the call against the daily token-usage aggregate."""client = get_embedding_api_client()if client is None or not texts:return []- try:- kwargs = {'input': texts, 'model': EMBEDDING_MODEL}- if EMBEDDING_DIMENSIONS is not None:- kwargs['dimensions'] = EMBEDDING_DIMENSIONS- response = client.embeddings.create(**kwargs)- if user_id is not None and getattr(response, 'usage', None) is not None:- try:- usage = response.usage- # Pydantic v2 stores non-schema fields under model_extra; fall- # back to attribute access for SDKs that surface them directly.- extras = getattr(usage, 'model_extra', None) or {}- prompt_tokens = getattr(usage, 'prompt_tokens', 0) or 0- total_tokens = getattr(usage, 'total_tokens', prompt_tokens) or prompt_tokens- cost = extras.get('cost') if 'cost' in extras else getattr(usage, 'cost', None)- from src.services.token_tracking import token_tracker- token_tracker.record_usage(- user_id=user_id,- operation_type='embedding',- prompt_tokens=int(prompt_tokens),- completion_tokens=0,- total_tokens=int(total_tokens),- model_name=EMBEDDING_MODEL,- cost=float(cost) if cost is not None else None,+ kwargs = {'input': texts, 'model': EMBEDDING_MODEL}+ if EMBEDDING_DIMENSIONS is not None:+ kwargs['dimensions'] = EMBEDDING_DIMENSIONS++ last_exc = None+ for attempt in range(1, _API_EMBED_MAX_ATTEMPTS + 1):+ try:+ response = client.embeddings.create(**kwargs)++ if user_id is not None and getattr(response, 'usage', None) is not None:+ try:+ usage = response.usage+ # Pydantic v2 stores non-schema fields under model_extra;+ # fall back to attribute access for SDKs that surface them+ # directly.+ extras = getattr(usage, 'model_extra', None) or {}+ prompt_tokens = getattr(usage, 'prompt_tokens', 0) or 0+ total_tokens = getattr(usage, 'total_tokens', prompt_tokens) or prompt_tokens+ cost = extras.get('cost') if 'cost' in extras else getattr(usage, 'cost', None)+ from src.services.token_tracking import token_tracker+ token_tracker.record_usage(+ user_id=user_id,+ operation_type='embedding',+ prompt_tokens=int(prompt_tokens),+ completion_tokens=0,+ total_tokens=int(total_tokens),+ model_name=EMBEDDING_MODEL,+ cost=float(cost) if cost is not None else None,+ )+ except Exception as track_err:+ current_app.logger.warning(f"Failed to record embedding usage: {track_err}")++ return [np.array(d.embedding, dtype=np.float32) for d in response.data]++ except Exception as e:+ last_exc = e+ transient = _is_transient_embedding_error(e)+ if not transient or attempt == _API_EMBED_MAX_ATTEMPTS:+ current_app.logger.error(+ f"Embedding API call failed (attempt {attempt}/{_API_EMBED_MAX_ATTEMPTS}, "+ f"transient={transient}): {e}")- except Exception as track_err:- current_app.logger.warning(f"Failed to record embedding usage: {track_err}")+ return []+ # Exponential backoff with light jitter so concurrent retries+ # do not all hit the provider at the same instant.+ backoff = _API_EMBED_BASE_BACKOFF_SECONDS * (2 ** (attempt - 1))+ backoff *= 0.85 + 0.3 * random.random()+ current_app.logger.warning(+ f"Embedding API transient error (attempt {attempt}/{_API_EMBED_MAX_ATTEMPTS}), "+ f"retrying in {backoff:.1f}s: {e}"+ )+ time.sleep(backoff)- return [np.array(d.embedding, dtype=np.float32) for d in response.data]- except Exception as e:- current_app.logger.error(f"Embedding API call failed: {e}")- return []+ # Loop exhausted without returning a result.+ current_app.logger.error(f"Embedding API call exhausted retries: {last_exc}")+ return []@@ -302,25 +357,49 @@ def get_accessible_recording_ids(user_id):def process_recording_chunks(recording_id):"""Process a recording by creating chunks and generating embeddings.- This should be called after a recording is transcribed.++ Returns True on full success, False on any failure including the case+ where embedding generation returned fewer vectors than there were+ chunks. On failure the transaction is rolled back so the recording's+ existing chunks are preserved; this prevents the "old chunks deleted,+ new chunks not inserted" silent-failure mode that occurs when the+ embedding API blips mid-run."""try:recording = db.session.get(Recording, recording_id)if not recording or not recording.transcription:return False- # Delete existing chunks for this recording+ # Delete existing chunks for this recording. The deletion is staged+ # in this transaction; if anything below fails we rollback and the+ # old chunks survive. Only the final commit makes the swap+ # permanent.TranscriptChunk.query.filter_by(recording_id=recording_id).delete()# Create chunkschunks = chunk_transcription(recording.transcription)if not chunks:+ db.session.commit()return True# Generate embeddings (recording owner gets billed for API-mode usage)embeddings = generate_embeddings(chunks, user_id=recording.user_id)-++ # Verify we got one embedding per chunk. _api_embed returns [] on+ # exhausted retries, and a partial provider response could return+ # fewer than expected. Either case is a failure: rolling back keeps+ # the recording's old chunks intact so the admin retry pass (or a+ # later Re-embed all) can try again.+ if len(embeddings) != len(chunks):+ db.session.rollback()+ current_app.logger.error(+ f"Embedding generation returned {len(embeddings)} vectors for "+ f"{len(chunks)} chunks on recording {recording_id}; rolling "+ f"back to preserve existing chunks."+ )+ return False+# Store chunks in databasefor i, (chunk_text, embedding) in enumerate(zip(chunks, embeddings)):chunk = TranscriptChunk(@@ -331,11 +410,11 @@ def process_recording_chunks(recording_id):embedding=serialize_embedding(embedding) if embedding is not None else None)db.session.add(chunk)-+db.session.commit()current_app.logger.info(f"Created {len(chunks)} chunks for recording {recording_id}")return True-+except Exception as e:current_app.logger.error(f"Error processing chunks for recording {recording_id}: {e}")db.session.rollback()