Skip to main content

API Streaming Protocol

Certain GoingMerry API endpoints (like /api/generate, /api/chat, /api/pull, /api/push, /api/create) return streamed responses.


1. Line-Delimited JSON

Rather than using Server-Sent Events (SSE) data: prefixes, GoingMerry streams responses using Line-Delimited JSON.

  • Response Header: Content-Type: application/x-ndjson; charset=utf-8
  • Format: Every token chunk is sent as a single line containing a fully-formed JSON object. Lines are separated by a newline character (\n).

2. Example Stream Output

A raw HTTP chunk stream looks like:

{"model":"gemma4","response":"Hello","done":false}\n
{"model":"gemma4","response":"!","done":false}\n
{"model":"gemma4","response":"","done":true,"total_duration":4298120}\n

3. Client Implementation (Python)

To read this stream in code:

import requests
import json

response = requests.post(
"http://localhost:11434/api/generate",
json={"model": "gemma4", "prompt": "Say hello"},
stream=True
)

for line in response.iter_lines():
if line:
chunk = json.loads(line)
print(chunk.get("response", ""), end="", flush=True)