Chat Streaming (SSE)

Real-time streaming chat responses via Server-Sent Events (SSE).

POST /api/v1/chat/stream

Same as /chat but returns a Server-Sent Events stream for real-time token-by-token output. The request body is identical to the Chat endpoint.

SSE Events

Event
Description
Data format

sources

Source citations (sent first)

{"sources": [...], "documentType": "pdf"}

token

Incremental text token

{"t": "word "}

thinking

Thinking mode progress

{"step": "searching", ...}

done

Full response complete

{"full_text": "...", "conversation_id": "..."}

error

Error occurred

{"error": {"code": "...", "message": "..."}}

Example: Python

import requests
import json

url = "https://api.knowbase.ai/api/v1/chat/stream"
headers = {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json"
}
data = {
    "question": "Summarize the main findings",
    "file_ids": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"]
}

response = requests.post(url, headers=headers, json=data, stream=True)

event_type = None
for line in response.iter_lines():
    line = line.decode("utf-8")
    if line.startswith("event: "):
        event_type = line[7:]
    elif line.startswith("data: "):
        payload = json.loads(line[6:])
        if event_type == "token":
            print(payload["t"], end="", flush=True)
        elif event_type == "sources":
            print(f"\nSources: {len(payload['sources'])} found")
        elif event_type == "done":
            print(f"\n\nDone. Conversation: {payload['conversation_id']}")

Example: JavaScript

Last updated