Skip to content

For clean Markdown of any page, append .md to the page URL. For a complete documentation index, see For full documentation content, see For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at

Sentiment Analysis

Detect the sentiment of speech in your audio

For the complete documentation index, see llms.txt

Supported models: Universal-3 Pro (universal-3-pro), Universal-2 (universal-2)

Supported regions: US and EU

Supported languages: Global English (en), Australian English (en_au), British English (en_uk), US English (en_us)

Key API parameters:

  • sentiment_analysis (boolean) - Enable Sentiment Analysis
  • speaker_labels (boolean) - Optionally enable to add speaker labels to sentiment results

Response fields: Each element in sentiment_analysis_results contains:

  • text (string) - The sentence text
  • sentiment (string) - POSITIVE, NEUTRAL, or NEGATIVE
  • confidence (number) - Confidence score from 0 to 1
  • start / end (number) - Timestamps in milliseconds
  • speaker (string or null) - Speaker label if speaker diarization is enabled

cURL quickstart:

curl  \
  --header "Authorization: YOUR_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
    "audio_url": "YOUR_AUDIO_URL",
    "speech_models": ["universal-3-pro", "universal-2"],
    "sentiment_analysis": true
  }'

Poll GET /v2/transcript/{id} until status is completed. Results are in the sentiment_analysis_results array.

US & EU

The Sentiment Analysis model detects the sentiment of each spoken sentence in the transcript text. Use Sentiment Analysis to get a detailed analysis of the positive, negative, or neutral sentiment conveyed in the audio, along with a confidence score for each result.

Quickstart

Enable Sentiment Analysis by setting sentiment_analysis to True in the JSON payload.

python
import requests
import time

base_url = ""

headers = {
    "authorization": "<YOUR_API_KEY>"
}

with open("./local_file.mp3", "rb") as f:
    response = requests.post(base_url + "/v2/upload",
                            headers=headers,
                            data=f)

upload_url = response.json()["upload_url"]

data = {
    "audio_url": upload_url, # You can also use a URL to an audio or video file on the web
    "speech_models": ["universal-3-pro", "universal-2"],
    "language_detection": True,
    "sentiment_analysis": True
}

url = base_url + "/v2/transcript"
response = requests.post(url, json=data, headers=headers)

transcript_id = response.json()['id']
polling_endpoint = base_url + "/v2/transcript/" + transcript_id

print(f"Transcript ID: {transcript_id}")

while True:
    transcription_result = requests.get(polling_endpoint, headers=headers).json()

    if transcription_result['status'] == 'completed':
      for sentiment_result in transcription_result['sentiment_analysis_results']:
        print(sentiment_result['text'])
        print(sentiment_result['sentiment'])  # POSITIVE, NEUTRAL, or NEGATIVE
        print(sentiment_result['confidence'])
        print(f"Timestamp: {sentiment_result['start']} - {sentiment_result['end']}")
      break
    elif transcription_result['status'] == 'error':
        raise RuntimeError(f"Transcription failed: {transcription_result['error']}")
    else:
        time.sleep(3)

Enable Sentiment Analysis by setting sentiment_analysis to True in the transcription config.

python
import assemblyai as aai

aai.settings.api_key = "<YOUR_API_KEY>"

# audio_file = "./local_file.mp3"
audio_file = ""

config = aai.TranscriptionConfig(
    speech_models=["universal-3-pro", "universal-2"],
    language_detection=True,
    sentiment_analysis=True
)

transcript = aai.Transcriber().transcribe(audio_file, config)
print(f"Transcript ID: {transcript.id}")

for sentiment_result in transcript.sentiment_analysis:
    print(sentiment_result.text)
    print(sentiment_result.sentiment)  # POSITIVE, NEUTRAL, or NEGATIVE
    print(sentiment_result.confidence)
    print(f"Timestamp: {sentiment_result.start} - {sentiment_result.end}")

Enable Sentiment Analysis by setting sentiment_analysis to true in the JSON payload.

javascript
import fs from "fs-extra";

const baseUrl = "";

const headers = {
  authorization: "<YOUR_API_KEY>",
};

const path = "./my-audio.mp3";
const audioData = await fs.readFile(path);
let res = await fetch(`${baseUrl}/v2/upload`, {
  method: "POST",
  headers,
  body: audioData,
});
if (!res.ok) throw new Error(`Error: ${res.status}`);
const uploadResponse = await res.json();
const uploadUrl = uploadResponse.upload_url;

const data = {
  audio_url: uploadUrl, // You can also use a URL to an audio or video file on the web
  speech_models: ["universal-3-pro", "universal-2"],
  language_detection: true,
  sentiment_analysis: true,
};

const url = `${baseUrl}/v2/transcript`;
res = await fetch(url, {
  method: "POST",
  headers: { ...headers, "Content-Type": "application/json" },
  body: JSON.stringify(data),
});
if (!res.ok) throw new Error(`Error: ${res.status}`);
const response = await res.json();

const transcriptId = response.id;
console.log("Transcript ID: ", transcriptId);

const pollingEndpoint = `${baseUrl}/v2/transcript/${transcriptId}`;

while (true) {
  res = await fetch(pollingEndpoint, { headers });
  if (!res.ok) throw new Error(`Error: ${res.status}`);
  const transcriptionResult = await res.json();

  if (transcriptionResult.status === "completed") {
    for (const sentimentResult of transcriptionResult.sentiment_analysis_results) {
      console.log(sentimentResult.text);
      console.log(sentimentResult.sentiment); // POSITIVE, NEUTRAL, or NEGATIVE
      console.log(sentimentResult.confidence);
      console.log(
        `Timestamp: ${sentimentResult.start} - ${sentimentResult.end}`
      );
    }
    break;
  } else if (transcriptionResult.status === "error") {
    throw new Error(`Transcription failed: ${transcriptionResult.error}`);
  } else {
    await new Promise((resolve) => setTimeout(resolve, 3000));
  }
}

Enable Sentiment Analysis by setting sentiment_analysis to true in the transcription config.

javascript
import { AssemblyAI } from "assemblyai";

const client = new AssemblyAI({
  apiKey: "<YOUR_API_KEY>",
});

// const audioFile = './local_file.mp3'
const audioFile = "";

const params = {
  audio: audioFile,
  speech_models: ["universal-3-pro", "universal-2"],
  language_detection: true,
  sentiment_analysis: true,
};

const run = async () => {
  const transcript = await client.transcripts.transcribe(params);
  console.log("Transcript ID: ", transcript.id);

  for (const result of transcript.sentiment_analysis_results) {
    console.log(result.text);
    console.log(result.sentiment); // POSITIVE, NEUTRAL, or NEGATIVE
    console.log(result.confidence);
    console.log(`Timestamp: ${result.start} - ${result.end}`);
  }
};
run();

Example output

plain
Smoke from hundreds of wildfires in Canada is triggering air quality alerts throughout the US.
NEGATIVE
0.8181032538414001
Timestamp: 250 - 6350
...

Check out this cookbook LLM Gateway for Customer Call Sentiment Analysis for an example of how to use LLM Gateway to analyze the sentiment of a customer call.

Add speaker labels to sentiments

To add speaker labels to each sentiment analysis result, using Speaker Diarization, enable speaker_labels in the JSON payload.

Each sentiment result will then have a speaker field that contains the speaker label.

python
data = {
    "audio_url": upload_url,
    "sentiment_analysis": True,
    "speaker_labels": True
}
# ...
      for sentiment_result in transcription_result['sentiment_analysis_results']:
        print(sentiment_result['speaker'])
      break

To add speaker labels to each sentiment analysis result, using Speaker Diarization, enable speaker_labels in the transcription config.

Each sentiment result will then have a speaker field that contains the speaker label.

python
config = aai.TranscriptionConfig(
  sentiment_analysis=True,
  speaker_labels=True
)
# ...
for sentiment_result in transcript.sentiment_analysis:
  print(sentiment_result.speaker)

To add speaker labels to each sentiment analysis result, using Speaker Diarization, enable speaker_labels in the JSON payload.

Each sentiment result will then have a speaker field that contains the speaker label.

javascript
const data = {
  audio_url: uploadUrl,
  sentiment_analysis: true,
  speaker_labels: true
}
// ...
    for (const sentimentResult of transcriptionResult.sentiment_analysis_results) {
      console.log(sentimentResult.speaker);
    }
    break;

To add speaker labels to each sentiment analysis result, using Speaker Diarization, enable speaker_labels in the transcription config.

Each sentiment result will then have a speaker field that contains the speaker label.

javascript
const params = {
  audio: audioUrl,
  sentiment_analysis: true,
  speaker_labels: true,
};
// ...
for (const result of transcript.sentiment_analysis_results) {
  console.log(result.speaker);
}

API reference

Request

bash
curl  \
--header "Authorization: <YOUR_API_KEY>" \
--header "Content-Type: application/json" \
--data '{
  "audio_url": "YOUR_AUDIO_URL",
  "sentiment_analysis": true
}'
KeyTypeDescription
sentiment_analysisbooleanEnable Sentiment Analysis.

Response

KeyTypeDescription
sentiment_analysis_resultsarrayA temporal sequence of Sentiment Analysis results for the audio file, one element for each sentence in the file.
sentiment_analysis_results[i].textstringThe transcript of the i-th sentence.
sentiment_analysis_results[i].startnumberThe starting time, in milliseconds, of the i-th sentence.
sentiment_analysis_results[i].endnumberThe ending time, in milliseconds, of the i-th sentence.
sentiment_analysis_results[i].sentimentstringThe detected sentiment for the i-th sentence, one of POSITIVE, NEUTRAL, NEGATIVE.
sentiment_analysis_results[i].confidencenumberThe confidence score for the detected sentiment of the i-th sentence, from 0 to 1.
sentiment_analysis_results[i].speakerstring or nullThe speaker of the i-th sentence if Speaker Diarization is enabled, else null.

Frequently asked questions

The Sentiment Analysis model is based on the interpretation of the transcript and may not always accurately capture the intended sentiment of the speaker. It's recommended to take into account the context of the transcript and to validate the sentiment analysis results with human judgment when possible.

The Content Moderation model can be used to identify and filter out sensitive or offensive content from the transcript.

It's important to ensure that the audio being analyzed is relevant to your use case. Additionally, it's recommended to take into account the context of the transcript and to evaluate the confidence score for each sentiment label.

The Sentiment Analysis model is designed to be fast and efficient, but processing times may vary depending on the size of the audio file and the complexity of the language used. If you experience longer processing times than expected, don't hesitate to contact our support team.