Route to Default Language if Language Confidence is Low

This guide will show you how to use AssemblyAI’s API to resubmit a request using a default language if the Automatic Language Detection’s language_confidence is below a certain threshold.

Get Started

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

Step-by-Step Instructions

Install the SDK:

Import the assemblyai package and set the API key.

1import { AssemblyAI } from 'assemblyai';
2
3const client = new AssemblyAI({
4apiKey: 'YOUR_API_KEY'
5});

Define a default_language, which should be set to the language code that will be used to rerun the transcript if language detection runs with low language_confidence.

1const default_language = 'LANGUAGE_CODE';

Define an audio_url that is set to a link to the audio file. Define and set the parameters audio: audioUrl and language_detection: true. We also need to define our language_confidence_threshold. For the purposes of this example, we’ll set it to 0.8, representing 80% confidence.

If a transcript ends up with a language_confidence below this value, the transcript will error out and will return the transcript using the default_language.

1const audioUrl = 'https://example.org/audio.mp3';
2
3const params = {
4audio: audioUrl,
5language_detection: true,
6language_confidence_threshold: 0.8,
7// Add any other params
8};

You can handle the error safely by checking the error message and rerunning the transcript with the language_code set to the default_language.

The error handling flow works as follows:

  1. If there is no error, the transcript ID and text are printed
  2. If there is an error:
    • Check if it’s related to language_confidence being below threshold
    • If so:
      • Print message about rerunning with default language
      • Create new transcript with default_language as the language_code
      • Print new transcript ID and text
    • If not:
      • Print the error message

When rerunning with the default language, the configuration is updated to:

  • Turn off language_detection
  • Remove language_confidence_threshold
  • Set language_code to the default_language

You will not be charged for the first transcript if there is an error. You will only be charged for the transcript that processes successfully.

1const run = async (params) => {
2
3 const transcript = await client.transcripts.transcribe(params);
4
5 if (transcript.status === 'error') {
6 if (transcript.error.includes("below the requested confidence threshold value")) {
7 console.log(
8 `${transcript.error}. Running transcript again with language set to '${default_language}'.`
9 );
10 params = {...params, language_detection: false, language_confidence_threshold: null, language_code: default_language};
11 run(params);
12 return;
13 }
14
15 console.log(transcript.error);
16 return;
17 }
18
19 console.log(`Transcript ID: ${transcript.id}`);
20 console.log(transcript.text);
21
22};
23
24run(params);