Skip to content

fix code cell for streaming#575

Merged
Lauren Hirata Singh (lnhsingh) merged 2 commits into
mainfrom
cc/fix_streaming
Sep 19, 2025
Merged

fix code cell for streaming#575
Lauren Hirata Singh (lnhsingh) merged 2 commits into
mainfrom
cc/fix_streaming

Conversation

@ccurme

Copy link
Copy Markdown
Contributor

Currently we split the streaming loop and combination into separate code blocks.

There are a few problems with the code block for combining chunks:

  1. It doesn't work (we sum an AIMessage, which results in a ChatPromptTemplate):
from langchain_core.messages import AIMessage, AIMessageChunk

chunks = [AIMessageChunk("foo"), AIMessageChunk(" bar")]

final_message = sum(chunks, AIMessage(""))  # ChatPromptTemplate
  1. More nitpicky, but I think aggregating chunks is important and also concise enough that we can display it in the first cell with just two additional lines.

Here we demonstrate aggregation in the first cell (and also remove an import).

Copilot AI review requested due to automatic review settings September 18, 2025 13:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a broken code example for streaming message aggregation in the LangChain documentation. The main issue was that the existing code for combining chunks used an incorrect pattern that resulted in a ChatPromptTemplate instead of an AIMessage.

  • Consolidates streaming and chunk aggregation into a single, working code block
  • Removes the broken sum(chunks, AIMessage("")) pattern that produces incorrect results
  • Demonstrates proper streaming aggregation inline with collection

@github-actions github-actions Bot added langchain For docs changes to LangChain oss labels Sep 18, 2025
@github-actions

Copy link
Copy Markdown
Contributor

Preview ID generated: preview-ccfixs-1758202838-928639b

Comment thread src/oss/langchain/messages.mdx
@github-actions

Copy link
Copy Markdown
Contributor

Preview ID generated: preview-ccfixs-1758220102-e6cc573

@lnhsingh
Lauren Hirata Singh (lnhsingh) merged commit a559c54 into main Sep 19, 2025
12 checks passed
@lnhsingh
Lauren Hirata Singh (lnhsingh) deleted the cc/fix_streaming branch September 19, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

langchain For docs changes to LangChain oss

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants