> ## Documentation Index
> Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 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](mailto:support@assemblyai.com)

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

<Tip>
  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](https://www.assemblyai.com/dashboard/home).
</Tip>

## Quickstart

```python expandable theme={null}
import assemblyai as aai
import time

aai.settings.api_key = "YOUR_API_KEY"

SERVER_ERROR_MESSAGES = (
    "Internal server error. Please retry your request. If the error persists, please contact support@assemblyai.com for more information.",
    # Legacy message string, kept for backwards compatibility.
    "Server error, developers have been alerted.",
)

def is_server_error(transcript):
    return transcript.error is not None and any(
        transcript.error.startswith(msg.rstrip(".")) for msg in SERVER_ERROR_MESSAGES
    )

def handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5):
    for attempt in range(retries + 1):
        transcript = transcriber.transcribe(audio_url, config)
        if is_server_error(transcript):
            if attempt < retries:
                print(f"Encountered a server error. Retrying in {wait_time} second(s)...")
                time.sleep(wait_time)
            else:
                print("Retry failed with a server error. Please contact AssemblyAI Support: support@assemblyai.com")
                return None
        elif transcript.status == aai.TranscriptStatus.error:
            print(f"Transcription failed: {transcript.error}")
            return None
        else:
            return transcript

audio_url = "YOUR_AUDIO_URL"
transcriber = aai.Transcriber()
config = aai.TranscriptionConfig()

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

```

## Get started

Before we begin, make sure you have an AssemblyAI account and an API key. You can [sign up](https://assemblyai.com/dashboard/signup) for a free account and get your API key from your dashboard.

## Step-by-step instructions

Install the SDK:

```bash theme={null}
pip install assemblyai
```

Import the `assemblyai` and `time` package and set your API key:

```python theme={null}
import assemblyai as aai
import time

aai.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.

The current server-error string returned by the API is `Internal server error. Please retry your request. If the error persists, please contact support@assemblyai.com for more information.` The legacy `Server error, developers have been alerted` string is also matched so older retries keep working.

```python theme={null}
SERVER_ERROR_MESSAGES = (
    "Internal server error. Please retry your request. If the error persists, please contact support@assemblyai.com for more information.",
    # Legacy message string, kept for backwards compatibility.
    "Server error, developers have been alerted.",
)

def is_server_error(transcript):
    return transcript.error is not None and any(
        transcript.error.startswith(msg.rstrip(".")) for msg in SERVER_ERROR_MESSAGES
    )

def handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5):
    for attempt in range(retries + 1):
        transcript = transcriber.transcribe(audio_url, config)
        if is_server_error(transcript):
            if attempt < retries:
                print(f"Encountered a server error. Retrying in {wait_time} second(s)...")
                time.sleep(wait_time)
            else:
                print("Retry failed with a server error. Please contact AssemblyAI Support: support@assemblyai.com")
                return None
        elif transcript.status == aai.TranscriptStatus.error:
            print(f"Transcription failed: {transcript.error}")
            return None
        else:
            return transcript
```

Define the audio file that you want to transcribe.

```python theme={null}
audio_url = "YOUR_AUDIO_URL"
```

Create a `Transcriber` object and specify features in `TranscriptionConfig`.

```python theme={null}
transcriber = aai.Transcriber()
config = 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.

```python theme={null}
transcript = handle_error_transcription(audio_url, transcriber, config, retries=1, wait_time=5)
if transcript:
    print(transcript.text)
```
