Stream individual message chunks using AIMessageChunk objects that are emitted by streaming LLMs, and aggregate them by accumulating the content fields and merging tool_calls chunks using the AIMessageChunk's built-in + operator or custom aggregation logic.
When streaming responses from a chat model using .stream() or astream_events, the model yields AIMessageChunk objects. These chunks represent partial content. To reconstruct the complete AIMessage, you can accumulate them by adding chunks together using the + operator, which is overloaded to merge content and tool call deltas appropriately. This aggregation is automatically handled when you collect all chunks into a list and sum them, or you can manually accumulate. For tool calls, chunk merging requires special handling because tool call arguments may arrive in multiple fragments; AIMessageChunk provides a tool_calls field that aggregates as chunks are added.
When using LangGraph, you can also receive messages via astream_events with event types on_chat_model_stream. Each event contains a chunk of type AIMessageChunk. You can accumulate these in the same way.