Streaming replies (SSE)
Streaming delivers responses over Server-Sent Events (SSE) using Content-Type: text/event-stream. Your UI can display typing status steps and stream text as words arrive, rather than waiting for the entire reply.
Requesting a stream
To stream replies, set "stream": true in the body of POST /api/v1/threads/{id}/messages:
{
"text": "Can I do barbell squats during week 28 of pregnancy?",
"stream": true
}The server returns HTTP 200 OK with Content-Type: text/event-stream. Each event is delivered as a frame terminated by double newlines (\n\n).
Frame types
Every frame includes an event name and a JSON payload in data. The payload also repeats event so clients can parse data directly.
1. Status frames (event: status)
Status frames describe what the assistant is doing before text generation begins:
event: status
data: {"event":"status","step":"reading_your_notes","label":"Reading your notes"}Fields:
step: Machine-readable identifier.label: Ready-to-display label for your UI.
| Step value | Label | Description |
|---|---|---|
thinking | Thinking | Turn intake and reasoning. |
reading_your_notes | Reading your notes | Reviewing member memory and context. |
searching | Searching | Querying the content catalogue. |
double_checking | Double-checking | Evaluating clinical safety boundaries. |
looking_it_up | Looking it up | Retrieving exercise or symptom specifics. |
checking_your_stage | Checking where you're at | Checking gestational or postpartum week milestones. |
replying | Replying | Word generation started; delta frames follow immediately. |
2. Delta frames (event: delta)
Delta frames carry incremental Markdown text chunks:
event: delta
data: {"event":"delta","text":"At week 28,"}
event: delta
data: {"event":"delta","text":" modifying your squat"}
event: delta
data: {"event":"delta","text":" stance helps manage pelvic pressure."}Append each text snippet to your local buffer to stream words into the chat UI.
3. Done frames (event: done)
When generation completes and the response is saved, the server emits a done frame and closes the connection:
event: done
data: {"event":"done","thread":"thr_9c41f0b7a2d64e83b15c7f20","message":{"id":"msg_7d02edd7fc430c94162cba02","role":"coach","text":"A wider stance helps manage pelvic pressure.","created_at":"2026-11-08T23:08:00.123456Z"}}The payload echoes the thread ID and returns the persisted coach message object (id, role, text, created_at).
Disconnections mid-stream
If an HTTP connection drops during streaming:
- Member message is preserved: The incoming message is saved to the database before processing starts.
- Partial generation is discarded: If disconnected before the
doneframe, partial replies are not stored. This prevents truncated messages in conversation history. - Recovery: Your client can prompt the member to resend or fetch thread history. Member memory remains unchanged.
Production parsers
Here are complete client examples in cURL, TypeScript, and Python showing how to consume the stream, handle buffering across TCP chunks, and dispatch frames.
cURL
To inspect raw SSE frames directly in your terminal, use curl -N (no buffer):
curl -N -X POST "https://api.bellacoach.ai/api/v1/threads/thr_9c41f0b7a2d64e83b15c7f20/messages" \
-H "Authorization: Bearer $BELLA_API_KEY" \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{
"text": "What exercises are recommended for postpartum pelvic recovery?",
"stream": true
}'