liveBook · Manning

Chain.stream

As explained in Build AI-Enhanced Web Apps

chain.stream is a LangChain operation that executes a runnable chain and emits its results incrementally.

chain.stream executes a LangChain runnable chain and exposes generated results incrementally. A chain can fetch data, apply a prompt template, call an LLM, and parse output before returning streamed values. section 6.1.2

In a Next.js conversation handler, await the stream, iterate with for await, accumulate generated text, and push each update into createStreamableUI. The server then renders the changing response in real time. section 6.1.2

Streaming a weather response

Build a chain with RunnableLambda.from(fetchWeatherData).pipe(promptTemplate).pipe(llm).pipe(new StringOutputParser()), then call chain.stream({ city }). Iterate over the returned stream, append each item to textContent, and update a ChatBubble until stream.done().

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains chain composition, invocation, asynchronous iteration, UI updates, and finalization without detailing stream internals.
Examples
Uses a weather-data chain that generates a city response and updates a ChatBubble token by token.
What it assumes
Assumes familiarity with LangChain runnables, asynchronous iteration, LLM configuration, and server-side Next.js handlers.
What you can do after
Build it from scratch by composing a runnable chain and rendering its asynchronous output incrementally.
How it connects
Connects runnable chains with RunnableLambda, prompt templates, language models, StringOutputParser, createStreamableUI, and Next.js.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.