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

import { ChatOpenAI } from "langchain/chat_models/openai";
import { BytesOutputParser } from "langchain/schema/output_parser";
import { RunnableSequence } from "langchain/schema/runnable";

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: