Interface LanguageModelChatResponse

Represents a language model response.

See

LanguageModelAccess.chatRequest

Stubbed

interface LanguageModelChatResponse {
    stream: AsyncIterable<unknown>;
    text: AsyncIterable<string>;
}

Properties

Properties

stream: AsyncIterable<unknown>

An async iterable that is a stream of text and tool-call parts forming the overall response. A LanguageModelTextPart is part of the assistant's response to be shown to the user. A LanguageModelToolCallPart is a request from the language model to call a tool. The latter will only be returned if tools were passed in the request via LanguageModelChatRequestOptions.tools. The unknown-type is used as a placeholder for future parts, like image data parts.

Note that this stream will error when during data receiving an error occurs. Consumers of the stream should handle the errors accordingly.

To cancel the stream, the consumer can cancel the token that was used to make the request or break from the for-loop.

Example

try {
// consume stream
for await (const chunk of response.stream) {
if (chunk instanceof LanguageModelTextPart) {
console.log("TEXT", chunk);
} else if (chunk instanceof LanguageModelToolCallPart) {
console.log("TOOL CALL", chunk);
}
}

} catch(e) {
// stream ended with an error
console.error(e);
}
text: AsyncIterable<string>

This is equivalent to filtering everything except for text parts from a LanguageModelChatResponse.stream.