Learn how to handle transcription failures and resume partial work with TranscriptionError
Tafrigh provides robust error handling with partial progress preservation. When chunks fail, you can inspect the error, retry specific failures, and recover completed work.
When one or more chunks fail after all retry attempts, the transcribe function throws a TranscriptionError (see /home/daytona/workspace/source/src/errors.ts:18-43):
export class TranscriptionError extends Error { public readonly chunkFiles: AudioChunk[]; public readonly failures: FailedTranscription[]; public readonly outputDir?: string; public readonly transcripts: Segment[]; constructor(message: string, options: TranscriptionErrorOptions) { super(message); this.name = 'TranscriptionError'; this.chunkFiles = options.chunkFiles || []; this.failures = options.failures; this.outputDir = options.outputDir; this.transcripts = options.transcripts; } get failedChunks(): AudioChunk[] { return this.failures.map((failure) => failure.chunk); } get hasFailures(): boolean { return this.failures.length > 0; }}
type FailedTranscription = { chunk: AudioChunk; // The chunk file and time range error: unknown; // The original error that caused failure index: number; // Original position in chunk array};
Path to the temporary directory containing chunk files. This directory is not automatically deleted when a TranscriptionError is thrown, allowing you to retry failed chunks.
When a TranscriptionError is thrown, shouldCleanup is set to false to preserve the temporary directory. You are responsible for cleaning it up after handling the error.
The resumeFailedTranscriptions function retries only the failed chunks and merges results (see /home/daytona/workspace/source/src/transcriber.ts:206-224):
Cause: One or more keys in the rotation are invalid.Solution: Check which key caused the error and remove it:
if (error instanceof TranscriptionError) { error.failures.forEach(({ error: err }) => { if (err.message.includes('Invalid API key')) { console.error('Remove invalid key from rotation'); } });}
Audio format not supported
Cause: Wit.ai rejected the audio encoding.Solution: Verify preprocessing created valid MP3 files. Set preventCleanup: true and inspect error.outputDir.
Partial results needed
Scenario: You need the successful transcripts even if some chunks failed.Solution: Use error.transcripts directly:
} catch (error) { if (error instanceof TranscriptionError) { // Use partial results saveToDatabase(error.transcripts); // Schedule retry for failures later scheduleRetry(error.failures); }}
Always check error instanceof TranscriptionError before accessing properties like transcripts or failures. Other errors (e.g., file not found) won’t have these fields.
The outputDir property is undefined if the error occurs before chunk creation (e.g., during validation). Always check if (error.outputDir) before cleanup.
For production systems, implement a dead-letter queue for chunks that fail after multiple retries. Log the chunk metadata to error.failures for offline analysis.