Streaming
The askStream() method returns an async iterable that yields events as the AI generates a response.
Streaming Visualiser
Raw SSE Events
Click Play to start streaming...
Rendered Output
Output will appear here...
Event Types
| Event | Fields | Description |
|---|---|---|
start | threadId | Stream has started, thread ID assigned |
text | text | Incremental text chunk of the answer |
thinking | text | AI reasoning/thinking text (optional) |
finish | threadId, answer, citations, citedDocuments, followUpSuggestions | Final complete response with all metadata |
error | message | An error occurred during streaming |
Basic Usage
const stream = client.askStream({
buildingId: 'bld-789',
query: 'What fire safety documents are available?',
});
let fullAnswer = '';
for await (const event of stream) {
switch (event.event) {
case 'start':
console.log('Thread:', event.threadId);
break;
case 'text':
fullAnswer += event.text;
process.stdout.write(event.text);
break;
case 'finish':
console.log('\nCitations:', Object.keys(event.citations).length);
console.log('Follow-ups:', event.followUpSuggestions);
break;
case 'error':
console.error('Error:', event.message);
break;
}
}Aborting a Stream
Call abort() to cancel an in-flight stream:
const stream = client.askStream({ buildingId: 'bld-789', query: '...' });
// Abort after 5 seconds
setTimeout(() => stream.abort(), 5000);
try {
for await (const event of stream) {
// Process events...
}
} catch (err) {
if (err instanceof StreamAbortedError) {
console.log('Stream was cancelled');
}
}