# End of turn

For voice bots: end each segment as soon as the caller sounds finished, and know what ended it, to answer sooner.

A voice bot answers when the caller has finished. With a pause alone (`vad`), it must wait out the whole pause after every sentence, or cut people off when they stop to think. With `commit=turn`, the service also listens to how the speech ends, its intonation and rhythm, and ends the segment as soon as the speaker sounds finished.

> **Romanian real-time models.** End of turn works with Romanian real-time models, such as `vatis_ro_v9_realtime`. Its model is tuned on Romanian phone calls. It costs nothing extra: a session is billed by its audio seconds, as any other.

## When to use it

| commit | A segment ends | Suits |
|---|---|---|
| `vad` (default) | after a pause of `vad_silence_s` (1.5 s). The final comes about 1.5 s after the last word. | captions, meetings, a bot that can wait |
| `turn` | as soon as the speaker sounds finished, else after a pause of `vad_silence_s`. When the speaker sounds finished, the final comes about 0.5 s after the last word at the median. | voice bots that answer callers |
| `manual` | when your bot sends `{"type": "commit"}`. The final comes a fraction of a second after it. | bots that detect the end of a turn themselves, push-to-talk |

A `commit` works with `turn` too: send it to end a segment at once, for example when the caller presses a key.

## How it works

- At every pause, the service checks whether the speaker sounds finished: after 0.4 s of silence after the last word, then at 0.6 s and 1.0 s.
- When they sound finished, the segment ends, and its final has `"end_reason": "turn"`.
- When they do not, the segment ends after `vad_silence_s` of silence, as with `vad`. Here it is the longest pause: 1.5 s by default, 0.2 to 3.0. Its final has `"end_reason": "pause"`.
- The text, words and times of a final are made as with `vad`: only where a segment ends differs.
- With two channels (`multichannel=true`), each channel has its own turns.

## 1. Ask for end of turn

Add `commit=turn` to the request parameters of the session, and `turn_sensitivity` if you want another than 0.5 ([Turn sensitivity](https://platform.vatis.tech/docs/realtime/end-of-turn#turn-sensitivity)). The rest of the program is the one of [The whole program](https://platform.vatis.tech/docs/realtime#the-whole-program).

```javascript
// Request parameters: end of turn, and its sensitivity (0 to 1)
const params = {
  model_id: "vatis_ro_v9_realtime",
  commit: "turn",
  turn_sensitivity: "0.5",
};
```

```go
// Request parameters: end of turn, and its sensitivity (0 to 1)
var params = url.Values{
	"model_id":         {"vatis_ro_v9_realtime"},
	"commit":           {"turn"},
	"turn_sensitivity": {"0.5"},
}
```

```python
# Request parameters: end of turn, and its sensitivity (0 to 1)
params = {
    "model_id": "vatis_ro_v9_realtime",
    "commit": "turn",
    "turn_sensitivity": "0.5",
}
```

```java
// Request parameters: end of turn, and its sensitivity (0 to 1)
static final String[][] params = {
    {"model_id", "vatis_ro_v9_realtime"},
    {"commit", "turn"},
    {"turn_sensitivity", "0.5"},
};
```

```csharp
// Request parameters: end of turn, and its sensitivity (0 to 1)
var parameters = new Dictionary<string, string>
{
    ["model_id"] = "vatis_ro_v9_realtime",
    ["commit"] = "turn",
    ["turn_sensitivity"] = "0.5",
};
```

**Expected result:** `session.started` says so in its `config`:

```json
{"type": "session.started", "session_id": "sess_8d41c2a7e09b4f6a9c03d5e1b2a7f480",
 "model_id": "vatis_ro_v9_realtime",
 "config": {"commit": "turn", "turn_sensitivity": 0.5, "vad_silence_s": 1.5, ...}}
```

## 2. Answer when the turn ends

Keep the text of each final, and answer when a final ends the turn. Here `answer` stands for your bot: it gets what the caller said since its last answer, and replies.

```javascript
let heard = []; // what the caller said since the bot last answered

// in ws.onmessage
if (message.type === "final") {
  if (message.text) heard.push(message.text);
  // turn: they sounded finished; pause: the longest pause passed
  const reason = message.end_reason;
  if (heard.length && (reason === "turn" || reason === "pause")) {
    answer(heard.join(" ")); // your bot's reply
    heard = [];
  }
}
```

```go
// in the imports
"strings"

// in Message, what ended a final
EndReason string `json:"end_reason"`

var heard []string // what the caller said since the bot last answered

// in the switch over the messages
case "final":
	if message.Text != "" {
		heard = append(heard, message.Text)
	}
	// turn: they sounded finished; pause: the longest pause passed
	reason := message.EndReason
	if len(heard) > 0 && (reason == "turn" || reason == "pause") {
		answer(strings.Join(heard, " ")) // your bot's reply
		heard = nil
	}
```

```python
heard = []  # what the caller said since the bot last answered

# in the loop over the messages
if message["type"] == "final":
    if message["text"]:
        heard.append(message["text"])
    # turn: they sounded finished; pause: the longest pause passed
    if heard and message["end_reason"] in ("turn", "pause"):
        answer(" ".join(heard))  # your bot's reply
        heard.clear()
```

```java
// in Session: what the caller said since the bot last answered
final StringBuilder heard = new StringBuilder();

// in print's switch
case "final":
    String text = field(raw, "text");
    if (!text.isEmpty()) {
        heard.append(heard.length() > 0 ? " " : "").append(text);
    }
    // turn: they sounded finished; pause: the longest pause passed
    String endReason = field(raw, "end_reason");
    boolean ended = "turn".equals(endReason) || "pause".equals(endReason);
    if (ended && heard.length() > 0) {
        answer(heard.toString()); // your bot's reply
        heard.setLength(0);
    }
    break;
```

```csharp
var heard = new List<string>(); // the caller's words since the last answer

// in the switch over the messages
case "final":
    var text = message.GetProperty("text").GetString()!;
    if (text != "")
    {
        heard.Add(text);
    }
    // turn: they sounded finished; pause: the longest pause passed
    var endReason = message.GetProperty("end_reason").GetString();
    var ended = endReason is "turn" or "pause";
    if (ended && heard.Count > 0)
    {
        Answer(string.Join(" ", heard)); // your bot's reply
        heard.Clear();
    }
    break;
```

If the caller goes on while your bot replies (a `partial` with text comes), stop the reply and listen: their next final carries on what they said.

## What ended the segment

Every final of a `commit=turn` session has `end_reason`. Other sessions never have it.

| end_reason | What happened | What a bot can do |
|---|---|---|
| `turn` | The speaker sounded finished. | Answer now. |
| `pause` | `vad_silence_s` of silence passed, and the speaker did not sound finished. They may go on: their next words open a new segment. | The caller has been silent for the longest pause: answer, or keep the text and give them a moment more. |
| `commit` | The answer to your `commit`. | Go on as your bot decided when it sent the commit. |
| `end` | The session's last finals: after your `end`, or before a restart of the server ends the session. | Keep the text: nothing more comes in this session. After a restart, connect again ([Reconnect after a restart](https://platform.vatis.tech/docs/realtime/reconnect-after-a-restart)). |

A finished question, then a pause in the middle of a sentence:

```json
{"type": "partial", "segment_id": 4, "text": "aș vrea să mut programarea",
 "start": 20.12, "end": 21.76, "speaker": null}
{"type": "final", "segment_id": 4, "text": "Aș vrea să mut programarea de joi.",
 "start": 20.12, "end": 22.30, "speaker": null, "end_reason": "turn"}
{"type": "final", "segment_id": 5, "text": "Da, și",
 "start": 25.02, "end": 25.61, "speaker": null, "end_reason": "pause"}
```

A final with an empty `text` (a commit with nothing said since the last final) has a reason too. Skip it.

## Turn sensitivity

`turn_sensitivity` sets how finished the speaker must sound: 0.0 to 1.0, 0.5 by default, only with `commit=turn`.

- **Higher** answers sooner, and cuts more pauses inside a sentence: a caller who stops to think may get an answer before they finish.
- **Lower** waits until the speaker sounds more clearly finished: fewer cuts, and more finals wait for the longest pause.
- `vad_silence_s` is the most a final waits when the speaker does not sound finished. A shorter one answers those sooner, and cuts more of them.

Start with the default. If your bot cuts callers off, lower it; if it answers too late, raise it.

## A bot that labels speakers

With `diarize=true`, a final waits until speaker separation has passed its last word, about a second behind the audio, which takes back the time end of turn saves. Send `speaker_updates=true` too: the final comes as soon as its text is done, and its speakers follow in a `final.speakers` message ([The text first, its speakers after](https://platform.vatis.tech/docs/realtime/speakers-and-channels#the-text-first-its-speakers-after)). A bot on a two-channel call needs neither: each channel is its speaker.

## Refusals

A session that asks for end of turn the wrong way is refused before `session.started`, with `invalid_parameter` and close code 1008:

- `turn_sensitivity` without `commit=turn`, or outside 0 to 1: `param` `turn_sensitivity`.
- `commit=turn` with a model that is not Romanian: `param` `commit`.

```json
{"type": "error", "code": "invalid_parameter",
 "message": "turn_sensitivity: needs commit=turn", "param": "turn_sensitivity"}
```

Every code: [Errors in a session](https://platform.vatis.tech/docs/realtime/errors-in-a-session).
