Implement Retry Server Error Logic

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

Server errors indicate a server-side issue during the transcription process. These rarely happen, but can occasionally occur on our side. If a transcription fails due to a server error, we recommend that you resubmit the file for transcription to allow another server to process the audio. If the issue persists, please reach out to our support team: support@assemblyai.com

This workflow is designed to automatically retry these transcripts if a server error is encountered.

If your transcription fails due to a server error on our side, we will automatically retry the request up to three times. You can find this option in your Account Settings.

Quickstart

1import assemblyai as aai
2import time
3
4aai.settings.api_key = "YOUR_API_KEY"
5
6def handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5):
7 for attempt in range(retries + 1):
8 transcript = transcriber.transcribe(audio_url, config)
9 if transcript.error == "Server error, developers have been alerted":
10 if attempt < retries:
11 print(f"Encountered a server error. Retrying in {wait_time} second(s)...")
12 time.sleep(wait_time)
13 else:
14 print("Retry failed with a server error. Please contact AssemblyAI Support: support@assemblyai.com")
15 return None
16 elif transcript.status == aai.TranscriptStatus.error:
17 print(f"Transcription failed: {transcript.error}")
18 return None
19 else:
20 return transcript
21
22audio_url = "YOUR_AUDIO_URL"
23transcriber = aai.Transcriber()
24config = aai.TranscriptionConfig()
25
26transcript = handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5)
27if transcript:
28 print(transcript.text)

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 and time package and set your API key:

1import assemblyai as aai
2import time
3
4aai.settings.api_key = "YOUR_API_KEY"

Create a function that handles errors that may occur during the transcription process. The default number of retires is 1. The default wait time before retranscribing is 5 seconds.

1def handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5):
2 for attempt in range(retries + 1):
3 transcript = transcriber.transcribe(audio_url, config)
4 if transcript.error == "Server error, developers have been alerted":
5 if attempt < retries:
6 print(f"Encountered a server error. Retrying in {wait_time} second(s)...")
7 time.sleep(wait_time)
8 else:
9 print("Retry failed with a server error. Please contact AssemblyAI Support: support@assemblyai.com")
10 return None
11 elif transcript.status == aai.TranscriptStatus.error:
12 print(f"Transcription failed: {transcript.error}")
13 return None
14 else:
15 return transcript

Define the audio file that you want to transcribe.

1audio_url = "YOUR_AUDIO_URL"

Create a Transcriber object and specify features in TranscriptionConfig.

1transcriber = aai.Transcriber()
2config = aai.TranscriptionConfig()

Call the function to handle transcription with error handling. Specify number of retries and wait time. Return the transcribed text if transcription is successful.

1transcript = handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5)
2if transcript:
3 print(transcript.text)