Reference/SDK/Typescript/Classes

Class: BraintrustStream

A Braintrust stream. This is a wrapper around a ReadableStream of BraintrustStreamChunk, with some utility methods to make them easy to log and convert into various formats.

Constructors

constructor

new BraintrustStream(baseStream): BraintrustStream

Parameters

NameType
baseStreamReadableStream<Uint8Array>

Returns

BraintrustStream

new BraintrustStream(stream): BraintrustStream

Parameters

NameType
streamReadableStream<string>

Returns

BraintrustStream

new BraintrustStream(stream): BraintrustStream

Parameters

NameType
streamReadableStream<{ data: string ; type: "text_delta" } | { data: string ; type: "json_delta" } | { data: string ; type: "error" } | { data: { message: string ; stream: "stderr" | "stdout" } = sseConsoleEventDataSchema; type: "console" } | { data: { data: string ; event: "error" | "text_delta" | "json_delta" | "console" | "start" | "done" ; format: "code" | "global" | "llm" ; id: string ; name: string ; object_type: "prompt" | "tool" | "scorer" | "task" ; output_type: "completion" | "score" | "any" } = sseProgressEventDataSchema; type: "progress" } | { data: string ; type: "start" } | { data: string ; type: "done" }>

Returns

BraintrustStream

Methods

[asyncIterator]

[asyncIterator](): AsyncIterator<{ data: string ; type: "text_delta" } | { data: string ; type: "json_delta" } | { data: string ; type: "error" } | { data: { message: string ; stream: "stderr" | "stdout" } = sseConsoleEventDataSchema; type: "console" } | { data: { data: string ; event: "error" | "text_delta" | "json_delta" | "console" | "start" | "done" ; format: "code" | "global" | "llm" ; id: string ; name: string ; object_type: "prompt" | "tool" | "scorer" | "task" ; output_type: "completion" | "score" | "any" } = sseProgressEventDataSchema; type: "progress" } | { data: string ; type: "start" } | { data: string ; type: "done" }, any, undefined>

Returns an async iterator for the BraintrustStream. This allows for easy consumption of the stream using a for-await...of loop.

Returns

AsyncIterator<{ data: string ; type: "text_delta" } | { data: string ; type: "json_delta" } | { data: string ; type: "error" } | { data: { message: string ; stream: "stderr" | "stdout" } = sseConsoleEventDataSchema; type: "console" } | { data: { data: string ; event: "error" | "text_delta" | "json_delta" | "console" | "start" | "done" ; format: "code" | "global" | "llm" ; id: string ; name: string ; object_type: "prompt" | "tool" | "scorer" | "task" ; output_type: "completion" | "score" | "any" } = sseProgressEventDataSchema; type: "progress" } | { data: string ; type: "start" } | { data: string ; type: "done" }, any, undefined>

An async iterator that yields BraintrustStreamChunk objects.


copy

copy(): BraintrustStream

Copy the stream. This returns a new stream that shares the same underlying stream (via tee). Since streams are consumed in Javascript, use copy() if you need to use the stream multiple times.

Returns

BraintrustStream

A new stream that you can independently consume.


finalValue

finalValue(): Promise<unknown>

Get the final value of the stream. The final value is the concatenation of all the chunks in the stream, deserialized into a string or JSON object, depending on the value's type.

This function returns a promise that resolves when the stream is closed, and contains the final value. Multiple calls to finalValue() will return the same promise, so it is safe to call this multiple times.

This function consumes the stream, so if you need to use the stream multiple times, you should call copy() first.

Returns

Promise<unknown>

A promise that resolves with the final value of the stream or undefined if the stream is empty.


toReadableStream

toReadableStream(): ReadableStream<{ data: string ; type: "text_delta" } | { data: string ; type: "json_delta" } | { data: string ; type: "error" } | { data: { message: string ; stream: "stderr" | "stdout" } = sseConsoleEventDataSchema; type: "console" } | { data: { data: string ; event: "error" | "text_delta" | "json_delta" | "console" | "start" | "done" ; format: "code" | "global" | "llm" ; id: string ; name: string ; object_type: "prompt" | "tool" | "scorer" | "task" ; output_type: "completion" | "score" | "any" } = sseProgressEventDataSchema; type: "progress" } | { data: string ; type: "start" } | { data: string ; type: "done" }>

Get the underlying ReadableStream.

Returns

ReadableStream<{ data: string ; type: "text_delta" } | { data: string ; type: "json_delta" } | { data: string ; type: "error" } | { data: { message: string ; stream: "stderr" | "stdout" } = sseConsoleEventDataSchema; type: "console" } | { data: { data: string ; event: "error" | "text_delta" | "json_delta" | "console" | "start" | "done" ; format: "code" | "global" | "llm" ; id: string ; name: string ; object_type: "prompt" | "tool" | "scorer" | "task" ; output_type: "completion" | "score" | "any" } = sseProgressEventDataSchema; type: "progress" } | { data: string ; type: "start" } | { data: string ; type: "done" }>

The underlying ReadableStream.

On this page