# Messages

What the socket sends, and the control messages it takes, in Deepgram’s shapes.

## Messages you receive

- **`Metadata`**: On connect, with the session’s `request_id`.
- **`Results`**: The text of the current segment, in `channel.alternatives[0].transcript`, always whole words. An interim, `is_final: false`, replaces the one before it. A final, `is_final: true`, holds everything since the previous final, so joining the finals with one space gives the transcript.
- **`SpeechStarted`**: A segment has begun.
- **`UtteranceEnd`**: Follows the final that ends a segment, with `last_word_end`.

## When text becomes final

- **End of a segment**: After the pause set by `utterance_end_ms`: a final with `speech_final: true`, then `UtteranceEnd`.
- **`Finalize`**: At once: a final with `from_finalize: true`.
- **Long speech**: A segment still open after 30 seconds is finalized at its last word boundary, with `speech_final: false`.
- **`CloseStream`**: What is still open goes out as a final, then the socket closes.

## Words

Each entry of `words` is one word, with `word`, `punctuated_word`, `start` and `end` in seconds, and `confidence`, always 1.0. In Chinese and Japanese, which are written without spaces, each character is an entry and the transcript gains no space. When you name the language, `languages` repeats it.

## Messages you send

- **Audio**: Binary messages, in chunks of any size, at the speed of speech.
- **`{"type": "KeepAlive"}`**: Accepted. It does not keep a quiet session open: a session closes after 30 seconds without audio, so send silence instead.
- **`{"type": "Finalize"}`**: Finalizes the current segment now.
- **`{"type": "CloseStream"}`**: Sends the last final, then closes the socket.

## Errors and close codes

A parameter the socket cannot honour is refused with an error that names it. When a session ends on an error, an `Error` message with the reason comes first. The socket then closes with one of these codes:

| Close code | Meaning | What to do |
| --- | --- | --- |
| 1000 | Closed normally: after `CloseStream`, after 30 seconds without audio, or after 24 hours. | Nothing, or open a new session. |
| 1011 | A server error. | Open a new session. |
| 1012 | The server is restarting. | Open a new session. |
| 1013 | Audio arrived faster than real time. | Open a new session, and pace the audio. |
| 4400 | A protocol error, such as a malformed message or an invalid parameter. | Fix the client. |
| 4401 | The credential is invalid, expired or already used. | Use your API key, or a new client secret. |
| 4413 | The service is not ready. | Retry shortly. |
| 4429 | No capacity when you connected. | Retry after a short wait. |
