Server-Sent Events

Pro Feature

This feature is only available on paid plans. Please schedule a demo or email us to get started.

Fern’s SDKs support Server-Sent Events (SSE) out of the box. This feature is especially relevant for chat completions, where you want to stream LLM outputs in real-time.

When an endpoint is configured to use Server-Sent Events, the TypeScript SDK method will return an AsyncIterable of the underlying data type. This allows you to use it in a for await loop.

Below is an example method signature for a stream endpoint:

1import core from "../core";
2
3export interface ChatClient {
4
5 /**
6 * Stream chat completions
7 * @param props
8 * @returns An async iterable of chat completions
9 */
10 stream(
11 request: ChatStreamRequest,
12 requestOptions: core.RequestOptions = {}
13 ): AsyncIterable<ChatCompletion>;
14}

And here is an example of how a user would use the stream method:

1const response = await client.chat.stream({
2 query: "What is the weather in New York?"
3});
4for await (const completion of response) {
5 console.log(completion);
6}