Implement Retry Upload Error Logic

In this guide, we’ll show you how to set up automatic upload error retry logic in your transcription process.

Upload errors could be a result of a transient issue with our servers or they could be related to an issue with the file itself. Most likely the issue would be that the file is empty. Because the cause can be unclear at first, we recommend adding some retry logic to handle the rare occasions in which our upload service is experiencing performance issues. If the upload failure persists, you’ll want to check whether the file is empty. If you’re unclear on why the file is failing, please reach out to our support team at support@assemblyai.com.

This workflow is designed to automatically retry file uploads if an upload error is encountered.

Quickstart

1import assemblyai as aai
2import time
3from assemblyai.types import TranscriptError
4
5aai.settings.api_key = "YOUR_API_KEY"
6
7def transcribe_with_upload_retry(file_path, retries=3, delay=5):
8 transcriber = aai.Transcriber()
9
10 for attempt in range(retries):
11 try:
12 # Attempt to transcribe the file
13 config = aai.TranscriptionConfig(speaker_labels=True)
14 transcript = transcriber.transcribe(file_path, config)
15 return transcript
16
17 except TranscriptError as e:
18 # Handle specific error if upload fails
19 print(f"Attempt {attempt + 1} failed. {e}")
20
21 # Retry if a TranscriptError occurs,
22 if attempt + 1 < retries:
23 print(f"Retrying in {delay} seconds...")
24 time.sleep(delay)
25 else:
26 raise e # Raise the error after max retries
27
28 print("Max retries reached. Transcription failed.")
29 return None
30
31transcribe_with_upload_retry("YOUR_AUDIO_URL")

Get started

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard.

Step-by-step instructions

Install the SDK:

$pip install assemblyai

Import the assemblyai package and assemblyai’s TranscriptError. Additionally import the and time package and set your API key:

1import assemblyai as aai
2import time
3from assemblyai.types import TranscriptError
4
5aai.settings.api_key = "YOUR_API_KEY"

Create a function that retries upload failures. This example retries up to 3 times with a delay of 5 seconds each time.

1def transcribe_with_upload_retry(file_path, retries=3, delay=5):
2 transcriber = aai.Transcriber()
3
4 for attempt in range(retries):
5 try:
6 # Attempt to transcribe the file
7 config = aai.TranscriptionConfig(speaker_labels=True)
8 transcript = transcriber.transcribe(file_path, config)
9 return transcript
10
11 except TranscriptError as e:
12 # Handle specific error if upload fails
13 print(f"Attempt {attempt + 1} failed. {e}")
14
15 # Retry if a TranscriptError occurs,
16 if attempt + 1 < retries:
17 print(f"Retrying in {delay} seconds...")
18 time.sleep(delay)
19 else:
20 raise e # Raise the error after max retries
21
22 print("Max retries reached. Transcription failed.")
23 return None
24
25transcribe_with_upload_retry("YOUR_AUDIO_URL")