Core Concepts
Streaming answers
Show an answer as the engine writes it, so people see the first words in moments.
A full answer can take a few seconds to write. Streaming sends it piece by piece as it's written, so your UI can show the first words at once instead of waiting for the end.
Stream an answer
Pass stream to query and read the pieces as they arrive:
from docklee import Lynk
lynk = Lynk(engine_id="your-engine-id")
stream = await lynk.query("Summarize the refund policy.", stream=True)
async for token in stream:
print(token, end="", flush=True)In Python, streaming is async only. There's no _sync version. With cURL, -N turns off buffering so you see each event as it arrives.
Events
Over HTTP, POST /v1/engine/query/stream responds with server-sent events:
event: token
data: {"delta": "Customers can request "}
event: token
data: {"delta": "a refund within 30 days."}
event: final
data: {"answer": "Customers can request a refund within 30 days.", "citations": [{"chunk_id": "c2a8f5e1-7b39-4d6c-a014-8e3f2b9d61c7", "page_number": 4, "section_title": "Refunds and returns"}], "confidence": 0.93}
event: done
data: {"ok": true}| Event | Data | Meaning |
|---|---|---|
token | delta | The next piece of the answer. |
final | answer, citations, confidence | The complete, checked answer. Sent once, after the last token. |
done | ok | The stream is finished. |
error | detail | The answer couldn't be finished. No final follows. |
The SDK reads the events for you and yields each delta in order. It stops at done.
Some answers arrive whole in final, with no token events before it, such as a Precision Mode answer or a reply that the information isn't available. Over HTTP, always read final for the complete answer.
Mode while streaming
A streamed answer always uses the engine's Answer Mode from its settings in the dashboard. The mode you pass is used by query without stream. See Answer modes.
Citations and confidence
Citations and confidence are only known once the answer is complete. Read them from the final event over HTTP, or call query without stream when you need them in the SDK.
Next steps
Your UI can show answers as they're written. Choose how strictly they stay within your sources next.