CoherenceDevelopers

Concepts

Streaming

Operations that produce text as they go can stream it, so your users see the answer forming.

Add ?stream=true to an operation that supports it. The response is NDJSON: one JSON object per line, as each part is ready, ending with the complete result. The reference marks which operations stream; Seller Advisor questions will be the first.

JavaScript
const res = await fetch('https://api.coherenceltd.com/v1/…?stream=true', { method: 'POST', headers, body });
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = '';
for (;;) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += value;
  const lines = buffer.split('\n');
  buffer = lines.pop();
  for (const line of lines) if (line) handle(JSON.parse(line));
}