Skip to main content

Bytes output parser

The BytesOutputParser takes language model output (either an entire response or as a stream) and converts it into binary data. This is particularly useful for streaming output to the frontend from a server.

This output parser can act as a transform stream and work with streamed response chunks from a model.

Usage

npm install @langchain/openai
import { ChatOpenAI } from "@langchain/openai";
import { BytesOutputParser } from "@langchain/core/output_parsers";
import { RunnableSequence } from "@langchain/core/runnables";

const chain = RunnableSequence.from([
new ChatOpenAI({ temperature: 0 }),
new BytesOutputParser(),
]);

const stream = await chain.stream("Hello there!");

const decoder = new TextDecoder();

for await (const chunk of stream) {
if (chunk) {
console.log(decoder.decode(chunk));
}
}

API Reference:


Help us out by providing feedback on this documentation page: