YouTip LogoYouTip

Langchain Streaming

```html\\\\n

LangChain Streaming Output | Online Tutorial

\\\\n\\\\n

Streaming output makes AI responses appear character by character like typing, greatly improving user experience. LangChain's Agent has built-in complete streaming output support.

\\\\n\\\\n
\\\\n\\\\n

Why Streaming Output is Needed

\\\\n\\\\n

If using invoke(), users need to wait for the Agent to complete all steps (multiple model calls + tool execution) before seeing the result. For complex tasks, this could take ten seconds or even longer.

\\\\n\\\\n

Streaming output solves this problem: each token generated is immediately returned, allowing users to see progress in real time.

\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n
MethodUser ExperienceUse Case
invoke()Wait β†’ See complete result at onceScripts, API, Batch processing
stream()See every token in real-timeChat interfaces, Real-time display
\\\\n\\\\n
\\\\n\\\\n

stream_mode="messages" β€” Token-by-Token Streaming

\\\\n\\\\n

This is the finest-grained streaming mode, where each chunk corresponds to a token:

\\\\n\\\\n

Example

\\\\n\\\\n
from dotenv import load_dotenv\\\\n\\\\nload_dotenv()\\\\n\\\\nfrom langchain.agents import create_agent\\\\n\\\\nfrom langchain.chat_models import init_chat_model\\\\n\\\\nfrom langchain.messages import HumanMessage\\\\n\\\\nmodel = init_chat_model("deepseek:deepseek-v4-flash")\\\\n\\\\nagent = create_agent(\\\\n\\\\n    model=model,\\\\n    \\\\n    system_prompt="YouisTutorial TUTORIAL ofAssistant。",\\\\n\\\\n)\\\\n\\\\n# stream_mode="messages" return Token by Token\\\\n\\\\nprint("real-time streaming output:")\\\\n\\\\nfor msg_chunk, metadata in agent.stream(\\\\n\\\\n    {"messages": [HumanMessage(content="Introduce the TUTORIAL platform in one sentence")]},\\\\n    \\\\n    stream_mode="messages",\\\\n\\\\n):\\\\n\\\\n    # msg_chunk is AIMessageChunk\\\\n    \\\\n    # each chunk contains only a small piece of Content\\\\n\\\\n    if msg_chunk.content:\\\\n    \\\\n        print(msg_chunk.content, end="", flush=True)\\\\n\\\\nprint()# final newline
\\\\n\\\\n

Running result:

\\\\n\\\\n
Real-time streaming output:Tutorial(TUTORIALοΌ‰isaforprogramming beginnersoffreeonlinelearning platform,providesRich of technical Tutorial and practical examples.
\\\\n\\\\n

Understanding metadata

\\\\n\\\\n

metadata contains the source information for this chunk:

\\\\n\\\\n

Example

\\\\n\\\\n
print("view metadata information:\\\\\\\\n")\\\\n\\\\nfor msg_chunk, metadata in agent.stream(\\\\n\\\\n    {"messages": [HumanMessage(content="Hello, introduce yourself")]},\\\\n    \\\\n    stream_mode="messages",\\\\n\\\\n):\\\\n\\\\n    if msg_chunk.content and len(msg_chunk.content)>5:\\\\n    \\\\n        print(f"Content: {msg_chunk.content}")\\\\n        \\\\n        print(f"source node: {metadata.get('langgraph_node')}")\\\\n        \\\\n        print(f"Message Types: {type(msg_chunk).__name__}")\\\\n        \\\\n        break# only look at the first meaningful chunk
\\\\n\\\\n

Running result:

\\\\n\\\\n
View metadata information:Content: YouOkay! IisRunoobSource node: modelMessage type: AIMessageChunk
\\\\n\\\\n
\\\\n\\\\n

stream_mode="updates" β€” Step-by-Step View of Agent Execution

\\\\n\\\\n

This mode is very useful when building interfaces that need to display the "thinking process":

\\\\n\\\\n

Example

\\\\n\\\\n
from langchain.tools import tool\\\\n\\\\n@tool\\\\n\\\\ndef search_course(keyword: str) ->str:\\\\n\\\\n    """search for courses in Tutorial TUTORIAL"""\\\\n\\\\n    courses ={\\\\n\\\\n        "python": "Python3 Basic Tutorial (30 Chapter, 20 hours)",\\\\n\\\\n        "html": "HTML Basic Tutorial (25 Chapter, 15 hours)",\\\\n\\\\n    }\\\\n\\\\n    return courses.get(keyword.lower(),"No relevant courses found")\\\\n\\\\nagent = create_agent(\\\\n\\\\n    model=init_chat_model("deepseek:deepseek-v4-flash", temperature=0),\\\\n    \\\\n    tools=,\\\\n    \\\\n    system_prompt="YouisTutorial TUTORIAL ofCourse Consultant。",\\\\n\\\\n)\\\\n\\\\n# use updates mode to view each step\\\\n\\\\nprint("=== Agent Execute
\\\\n```
← Langchain Structured Output StLangchain Agent State β†’