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

# Speaker Diarization

> Find who spoke when in an audio file, and understand the limitations of speaker diarization.

(new in 4.6)

Speaker diarization finds who spoke when in an audio file. When it is enabled, the response contains a `speakers` list that splits the transcript into speaker turns, each with an anonymous speaker label and its timestamps.

Diarization runs as part of [audio processing](https://docs.getlimina.ai/configuration-and-operations/working-with-files/processing-files/audio.md#how-audio-files-are-processed). It does not change which entities are detected or how the audio is redacted.

## Parameters

Below are the parameters that control the behaviour of speaker diarization. These parameters shall be specified under `audio_options.diarization`.

| Parameter | Explanation | Default |
| - | - | - |
| `enabled` | This parameter sets whether diarization is run. When set to `True`, the response has `speakers`. | False |
| `min_speakers` | This parameter sets the minimum number of speakers, if known. Accepts values from 1 to 10. | Not set |
| `max_speakers` | This parameter sets the maximum number of speakers, if known. Accepts values from 1 to 10. | Not set |

If you know the exact number of speakers, set `min_speakers` and `max_speakers` to the same value. This makes the results much more reliable, especially for short files (see [Limitations](#limitations)).

See the `audio_options` object in the [API reference](https://docs.getlimina.ai/latest/process-files-base64.md#body-audio-options) for the full list of audio parameters.

> [!NOTE]
>
> Diarization slows down audio processing. See [Speaker Diarization benchmarks](https://docs.getlimina.ai/configuration-and-operations/container-management/benchmarks.md#speaker-diarization) for the throughput with and without diarization.

## Sample Request

> [!NOTE]
>
> [Connect with one of our privacy experts](https://getlimina.ai/contact-us/?utm_source=docs\&utm_medium=website) to run this code.

```json Request Body wrap lines theme={"theme":"poimandres"}
{
  "file": {
    "data": "<file_content_base64>",
    "content_type": "audio/wav"
  },
  "entity_detection": {
    "return_entity": true
  },
  "audio_options": {
    "diarization": {
      "enabled": true,
      "min_speakers": 2,
      "max_speakers": 2
    }
  }
}
```

```shell curl wrap lines theme={"theme":"poimandres"}
echo '{
          "file": {"data": "'$(base64 -w 0 sample.wav)'",
          "content_type": "audio/wav"},
          "entity_detection": {"return_entity": true},
          "audio_options": {"diarization": {"enabled": true, "min_speakers": 2, "max_speakers": 2}}
      }' \
| curl --request POST --url 'https://api.getlimina.ai/community/v4/process/files/base64' \
       -H 'Content-Type: application/json' \
       -H 'x-api-key: <YOUR KEY HERE>' \
       -d @- \
       | jq '.speakers'
```

```python python wrap lines theme={"theme":"poimandres"}
import requests
import base64

file_url = "https://paidocumentation.blob.core.windows.net/$web/sample.wav"
filename_out = "/path/to/output/sample.redacted.wav"
file_content = requests.get(file_url).content
file_content_base64 = base64.b64encode(file_content).decode()

headers = {"Content-Type": "application/json", "x-api-key": "<INSERT API KEY>"}

url = "https://api.getlimina.ai/community/v4/process/files/base64"

payload = {
  "file" : {
    "data": file_content_base64,
    "content_type": "audio/wav"
  },
  "entity_detection": {
    "return_entity": True
  },
  "audio_options": {
    "diarization": {
      "enabled": True,
      "min_speakers": 2,
      "max_speakers": 2
    }
  }
}

response = requests.post(url, json=payload, headers=headers).json()

for turn in response["speakers"]:
    text = response["processed_text"][turn["stt_idx"]:turn["end_idx"]]
    print(f'{turn["speaker"]} [{turn["stt_time"]:.2f}s - {turn["end_time"]:.2f}s]: {text}')

with open(filename_out, "wb") as f:
    f.write(base64.b64decode(response["processed_file"]))
```

## Sample Response

```json Response wrap lines theme={"theme":"poimandres"}
{
  "processed_file": "Base64 Encoded File Content of the Redacted File",
  "processed_text": "string",
  "entities": "List[Entity]",
  "entities_present": true,
  "languages_detected": {"en": 0.9},
  "speakers": [
    {
      "speaker": "SPEAKER_00",
      "stt_time": 2.8,
      "end_time": 8.18,
      "stt_idx": 1,
      "end_idx": 140
    },
    {
      "speaker": "SPEAKER_01",
      "stt_time": 8.98,
      "end_time": 11.34,
      "stt_idx": 141,
      "end_idx": 159
    }
  ]
}
```

Each item in `speakers` is one speaker turn, in time order. `stt_time` and `end_time` are in seconds, from the start of the turn's first word to the end of its last word. `stt_idx` and `end_idx` point to the turn's text in `processed_text`, and are not returned when `return_processed_text` is `False`. See the [API reference](https://docs.getlimina.ai/latest/process-files-base64.md) for the full response schema.

Speaker labels are anonymous and only valid inside one file. `SPEAKER_00` in one file is not the same person as `SPEAKER_00` in another file. Diarization does not identify who a speaker is.

## Limitations

Diarization is a statistical process and it makes mistakes. Treat speaker labels as a best guess, and review them when accuracy matters. In our testing we observed the following limitations:

* **One speaker at a time.** Each moment of audio is assigned to exactly one speaker. When two or more people talk at the same time, only one of them is labeled, and the overlapping speech can end up in the wrong turn.
* **Most errors happen at speaker changes.** Around 90% of the errors we observed are close to a point where the speaker changes. The words at the start or end of a turn are the ones most likely to be assigned to the wrong speaker.
* **Short replies are often mislabeled.** Very short turns such as "yes", "mhm", or "okay" contain too little speech to recognize the voice, so they can easily be assigned to the wrong speaker.
* **Short files can get the wrong number of speakers.** If a speaker has less than about 30 seconds of speech in the file, there may not be enough audio to tell their voice apart from the others. Two speakers can then be merged into one, or one speaker split into two. Setting `min_speakers` and `max_speakers` helps when you know how many people are speaking.
* **Group and in-person discussions are harder.** Conversations with many participants, frequent interruptions, or people talking over each other have many speaker changes and a lot of overlapping speech.
* **Tested on English only.** We have only tested diarization on English audio. It can be used with other [supported languages](https://docs.getlimina.ai/languages.md), but its accuracy on them has not been measured.
* **At most 10 speakers.** `min_speakers` and `max_speakers` accept values up to 10. In general, the more speakers a recording has, the lower the accuracy, because there are more voices to tell apart and usually more speaker changes.

### Getting Better Results

* Set `min_speakers` and `max_speakers` when you know the number of speakers.
* Expect the best results on recordings where people take turns and do not talk over each other, such as interviews and calls.
* Use the speaker labels for longer turns with more confidence than for short replies or the words around a speaker change.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.