{
  "source_file": "0930",
  "source_platform": "Ghost",
  "author": "Deep Learning Nerds",
  "title_listed": "How to Stream LangChain Responses in Python",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://www.deeplearningnerds.com/langchain-streaming-responses-python/",
  "publish_date": "2026-06",
  "corpus_stage": "",
  "notes": "",
  "id": "546bb9a32514",
  "collected_at": "2026-10-02T12:24:57",
  "title": "How to Stream LangChain Responses in Python",
  "body_markdown": "📘 Introduction\nMost beginner LLM examples wait for the full response before printing anything. That is fine for small scripts, but it can feel slow in a real AI app. When the model writes a longer answer, users often expect to see the text appear step by step.\nIn this tutorial, you will learn how to stream LangChain responses in Python. We will first call a model with a normal request, then update the script so it prints response chunks as they arrive.\n💡\nStreaming does not make the model think faster. It makes the experience feel faster because the user can start reading before the full answer is complete.\n💡 What are we implementing?\nWe will build this small workflow:\nUser prompt -> ChatOpenAI -> response chunks -> terminal output\nA normal model call returns one complete message. A streaming call returns smaller chunks while the answer is being generated. Your application can print those chunks in the terminal, send them to a web app, or show them in a chat interface.\n✅ Prerequisites\nBefore getting started, make sure you have:\n☑️ Python installed\n☑️ Basic Python knowledge\n☑️ An OpenAI API key\n☑️ A terminal or command prompt\n⚙️1️⃣ Create a project folder\nCreate a new local project folder for this tutorial:\nmkdir langchain-streaming-responses\ncd langchain-streaming-responses\n🐍2️⃣ Create a virtual environment\nCreate and activate a virtual environment:\npython -m venv .venv\nsource .venv/bin/activate\nOn Windows, activate it with:\n.venv\\Scripts\\activate\n📦3️⃣ Install libraries\nInstall LangChain and the OpenAI integration package:\npip install -U langchain langchain-openai\n🔐4️⃣ Set your API key\nSet your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.\nexport OPENAI_API_KEY=\"your_api_key_here\"\nOn Windows PowerShell, use:\n$env:OPENAI_API_KEY=\"your_api_key_here\"\n🔐\nUse environment variables for credentials. Do not paste private API keys directly into Python files.\n🧱5️⃣ Create a normal response script\nFirst, create a file named\nnormal_response.py\n. This version uses a regular model call and waits until the full answer is ready.\nfrom langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(model=\"gpt-4.1-mini\", temperature=0)\n\nresponse = model.invoke(\n    \"Explain why streaming responses are useful in AI apps in three short bullet points.\"\n)\n\nprint(response.content)\nnormal_response.py\nRun the script:\npython normal_response.py\nYou should see the full response after the model finishes generating it:\n- Users can start reading before the full response is complete.\n- Long answers feel faster and more interactive.\n- Streaming is useful for chatbots, copilots, and AI assistants.\n✍️6️⃣ Stream the response chunk by chunk\nNow create a file named\nstream_response.py\n. This version uses the streaming interface and prints each chunk as it arrives.\nfrom langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(model=\"gpt-4.1-mini\", temperature=0)\n\nstream = model.stream(\n    \"Write a short beginner-friendly explanation of streaming in AI apps.\"\n)\n\nfor chunk in stream:\n    print(chunk.content, end=\"\", flush=True)\n\nprint()\nstream_response.py\nRun it:\npython stream_response.py\nThis time, the answer should appear piece by piece. The exact chunks can vary, but the important behavior is that the terminal starts printing before the complete message is finished.\n🎓\nWant to go deeper? In the Academy section, we add async streaming, inspect usage metadata, and troubleshoot common streaming issues.\n⚡7️⃣ Stream asynchronously with astream\nFor many web apps and backend services, async code is useful because it lets your application handle other work while waiting for model chunks. Create a file named\nasync_stream_response.py\n.\nYou can view this post with the tier:\n      Academy Membership\nJoin academy now to read the post and get access to the full library of premium posts for academy members only.\nJoin Academy\nAlready have an account?\nSign In",
  "metadata": {
    "title": "How to Stream LangChain Responses in Python",
    "description": "Learn how to stream LangChain model responses in Python so users can see output chunks as they are generated instead of waiting for the full answer.",
    "keywords": null,
    "author": null,
    "og:site_name": "Deep Learning Nerds | The ultimate Learning Platform for AI and Data Science",
    "og:type": "article",
    "og:title": "How to Stream LangChain Responses in Python",
    "og:description": "Learn how to stream LangChain model responses in Python so users can see output chunks as they are generated instead of waiting for the full answer.",
    "og:url": "https://www.deeplearningnerds.com/langchain-streaming-responses-python/",
    "og:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/How-to-Stream-LangChain-Responses-in-Python.png",
    "og:image:width": "1200",
    "og:image:height": "839",
    "twitter:card": "summary_large_image",
    "twitter:title": "How to Stream LangChain Responses in Python",
    "twitter:description": "Learn how to stream LangChain model responses in Python so users can see output chunks as they are generated instead of waiting for the full answer.",
    "twitter:url": "https://www.deeplearningnerds.com/langchain-streaming-responses-python/",
    "twitter:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/How-to-Stream-LangChain-Responses-in-Python.png",
    "twitter:label1": "Written by",
    "twitter:data1": "AI Developer",
    "twitter:label2": "Filed under",
    "twitter:data2": "LangChain, Python, OpenAI, Artificial Intelligence",
    "twitter:site": "@DLNerds",
    "article:published_time": "2026-06-04T18:06:54.000Z",
    "article:modified_time": "2026-06-04T18:06:54.000Z",
    "article:tag": "Artificial Intelligence",
    "article:publisher": "https://www.facebook.com/deeplearningnerds/",
    "article:author": "https://www.facebook.com/deeplearningnerds/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/546bb9a32514/code/body-000.txt",
      "chars": 63,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-001.txt",
      "chars": 68,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-002.txt",
      "chars": 46,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-003.txt",
      "chars": 22,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-004.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-005.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-006.txt",
      "chars": 39,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-007.txt",
      "chars": 239,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-008.txt",
      "chars": 25,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-009.txt",
      "chars": 177,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-010.txt",
      "chars": 273,
      "language": "text"
    },
    {
      "file": "attachments/546bb9a32514/code/body-011.txt",
      "chars": 25,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://www.deeplearningnerds.com/membership/",
      "label": "Join Academy",
      "type": "a"
    },
    {
      "href": "https://www.deeplearningnerds.com/signin/",
      "label": "Sign In",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:47:42Z",
  "source_url": "https://www.deeplearningnerds.com/langchain-streaming-responses-python/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 3946,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/546bb9a32514/pages/source.html",
  "local_html": "attachments/546bb9a32514/pages/article.html",
  "body_base_path": "attachments/546bb9a32514/pages/",
  "body_markdown_path": "attachments/546bb9a32514/pages/article.md"
}