Documentation/Streaming (SSE)

Streaming (SSE)

Streaming (Server-Sent Events)

MeeDoo supports streaming responses via Server-Sent Events (SSE). When you set stream: true in your request, the API returns a stream of chunks as the model generates text, enabling real-time output.

SSE Format

The response uses Content-Type: text/event-stream. Each line starts with data: followed by a JSON object:

data: {"id": "md-resp-UUID", "created": 1791814893, "model": "meedoo:latest", "owned_by": "MeeDoo", "choices": [{"index": 0, "finish_reason": null, "delta": {"content": "Hello"}}], "object": "chat.completion.chunk"}

Chunk Structure

Each chunk is a JSON object with these fields:

FieldTypeDescription
idstringUnique response ID (format: md-resp-UUID)
createdintegerUnix timestamp
modelstringModel name (e.g. meedoo:latest)
owned_bystringAlways MeeDoo
objectstringAlways chat.completion.chunk
choices[0].delta.contentstringIncremental text content (may be empty on final chunk)
choices[0].finish_reasonstring|nullnull during streaming, "stop" on final chunk
usageobject|nullToken usage stats (only on final chunk)

Stream Termination

The stream ends with two special events:

data: {"id": "md-resp-...", "choices": [{"index": 0, "finish_reason": "stop", "delta": {}}], "object": "chat.completion.chunk", "usage": {"prompt_tokens": 19, "completion_tokens": 5, "total_tokens": 24, "input_tokens": 19, "output_tokens": 5, "tokens_per_second": 44.25}}

data: [DONE]

The final chunk before [DONE] contains finish_reason: "stop", an empty delta, and a usage object with token statistics.

Accumulating Content

Concatenate delta.content from each chunk to build the full response:

full_text = ""
for chunk in stream:
    content = chunk["choices"][0]["delta"].get("content", "")
    full_text += content

Using stream: true

Add "stream": true to your request body. The endpoint is POST /api/chat/completions.

POST /api/chat/completions

Streaming Chat Completions

Create a streaming chat completion using Server-Sent Events. Set stream: true in the request body.

Parameters

NameTypeReqDescriptionDefault
streambooleanYesSet to true for SSE streamingtrue

Response Examples

Streaming SSE Chunks
data: {"id": "md-resp-2e43f0d7-c8db-41f5-afa4-61ec902190be", "created": 1791814893, "model": "meedoo:latest", "owned_by": "MeeDoo", "choices": [{"index": 0, "finish_reason": null, "delta": {"content": "Hello"}}], "object": "chat.completion.chunk"}

data: {"id": "md-resp-2e43f0d7-c8db-41f5-afa4-61ec902190be", "created": 1791814893, "model": "meedoo:latest", "owned_by": "MeeDoo", "choices": [{"index": 0, "finish_reason": null, "delta": {"content": " to"}}], "object": "chat.completion.chunk"}

data: {"id": "md-resp-2e43f0d7-c8db-41f5-afa4-61ec902190be", "created": 1791814893, "model": "meedoo:latest", "owned_by": "MeeDoo", "choices": [{"index": 0, "finish_reason": null, "delta": {"content": " you!"}}], "object": "chat.completion.chunk"}

data: {"id": "md-resp-2e43f0d7-c8db-41f5-afa4-61ec902190be", "created": 1791814893, "model": "meedoo:latest", "owned_by": "MeeDoo", "choices": [{"index": 0, "finish_reason": "stop", "delta": {}}], "object": "chat.completion.chunk", "usage": {"prompt_tokens": 19, "completion_tokens": 5, "total_tokens": 24, "input_tokens": 19, "output_tokens": 5, "tokens_per_second": 44.25}}

data: [DONE]

Code Examples

Streaming Chat
curl -N -X POST https://api.meedoo.co/api/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"meedoo:latest","messages":[{"role":"user","content":"Tell me a story"}],"stream":true}'

# Output:
# data: {"id":"md-resp-...","choices":[{"index":0,"finish_reason":null,"delta":{"content":"Once"}}],"object":"chat.completion.chunk"}
# data: {"id":"md-resp-...","choices":[{"index":0,"finish_reason":null,"delta":{"content":" upon"}}],"object":"chat.completion.chunk"}
# ...
# data: {"id":"md-resp-...","choices":[{"index":0,"finish_reason":"stop","delta":{}}],"usage":{"total_tokens":42}}
# data: [DONE]