Streaming
Process incremental text and audio with the right transport, event lifecycle, and completion handling.
Choose a Streaming Protocol
Streaming lets your application use output before the full operation finishes. AudioLab uses different protocols for different tasks:
| Task | Protocol | Endpoint |
|---|---|---|
| Text generation | HTTP server-sent events (SSE) | Text endpoints with stream: true |
| One spoken reply | HTTP server-sent events (SSE) | /v1/audio/speech-to-speech with stream: true |
| Incremental transcription | WebSocket | /v1/audio/transcriptions/stream |
| Incremental speech generation | WebSocket | /v1/audio/speech/stream |
| Continuous audio conversations | WebSocket | /v1/audio/conversations/stream |
| Manual audio turns | WebSocket | /v1/audio/speech-to-speech/stream |
Start with a successful non-streaming request when the task supports one. Confirm the model’s streaming capability, key scope, and available credits before opening a session.
Stream a Transcript
Connect to wss://api.tryaudiolab.ai/v1/audio/transcriptions/stream from your backend, sending Authorization: Bearer YOUR_WORKSPACE_KEY on the HTTP upgrade. For another environment, use that Router host with wss://.
The native browser WebSocket constructor cannot add this authorization header. A browser client should connect to your own authenticated backend relay; keep the AudioLab key on that backend.
The following is a protocol sequence, not executable JavaScript:
→ {"type":"session.start","model":"auto","encoding":"pcm_s16le","sample_rate":16000,"channels":1,"interim_results":true}
← {"type":"session.started","request_id":"req_...","model":"...","provider":"..."}
→ binary PCM16 mono audio frames
← transcript.interim events
← transcript.final events
→ {"type":"session.finish"}
← remaining transcript.final events
← session.completed
← connection closesWait for session.started before sending audio. Match the actual encoding, sample rate, and channels to the session settings. A WAV file includes a container header; do not send the entire file as if it were raw PCM samples.
Interim results can change. Use them for live captions, but commit final events to your transcript. When input ends, send session.finish and wait for session.completed rather than closing immediately and losing the final words. See the streaming transcription reference for event fields and limits.
Stream a Language Response
Use the AudioLab API and a streaming-capable model. Set stream: true in a Chat Completions request and process SSE data: records. A network chunk may contain half an event or several events, so use an SSE-aware client or parser.
Accumulate text deltas in order. Accumulate tool arguments until the tool call is complete before validating or executing it. Chat Completions, Responses, and Messages have different event shapes; use the parser for the endpoint you called.
A UI stop action should abort the upstream request as well as local rendering. Do not leave a provider generating after the reader has cancelled the operation.
Handle Partial Output and Disconnects
Check HTTP errors before a stream opens. After headers are sent, failures appear as protocol error events or a closed connection; the HTTP status cannot change.
Keep partial output separate from a completed result. Record the request ID and the last completion or error event. A disconnect does not prove that no billable work happened, and reconnecting does not resume an arbitrary prior session.
Apply backpressure when your consumer cannot keep up. Bound audio queues, clear queued playback when the conversation protocol signals an interruption, and avoid replaying tool actions during retries. Follow Errors and Debugging before retrying uncertain requests.