!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
    *   [Debugging](/v0.1/docs/guides/debugging/)
    *   [Deployment](/v0.1/docs/guides/deployment/)
        
        *   [Next.js](/v0.1/docs/guides/deployment/nextjs/)
        *   [SvelteKit](/v0.1/docs/guides/deployment/sveltekit/)
    *   [Evaluation](/v0.1/docs/guides/evaluation/)
        
    *   [Extending LangChain.js](/v0.1/docs/guides/extending_langchain/)
    *   [Fallbacks](/v0.1/docs/guides/fallbacks/)
    *   [LangSmith Walkthrough](/v0.1/docs/guides/langsmith_evaluation/)
    *   [Migrating to 0.1](/v0.1/docs/guides/migrating/)
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Guides](/v0.1/docs/guides/)
*   Deployment

On this page

Deployment
==========

We strive to make deploying production apps using LangChain.js as intuitive as possible.

Compatibility[​](#compatibility "Direct link to Compatibility")
---------------------------------------------------------------

You can use LangChain in a variety of environments, including:

*   Node.js (ESM and CommonJS) - 18.x, 19.x, 20.x
*   Cloudflare Workers
*   Vercel / Next.js (Browser, Serverless and Edge functions)
*   Supabase Edge Functions
*   Browser
*   Deno

Note that individual integrations may not be supported in all environments.

For additional compatibility tips, such as deploying to other environments like older versions of Node, see [the installation section of the docs](/v0.1/docs/get_started/installation/).

Streaming over HTTP[​](#streaming-over-http "Direct link to Streaming over HTTP")
---------------------------------------------------------------------------------

LangChain is designed to interact with web streaming APIs via LangChain Expression Language (LCEL)'s [`.stream()`](/v0.1/docs/expression_language/interface/#stream) and [`.streamLog()`](/v0.1/docs/expression_language/interface/#stream-log) methods, which both return a web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) instance that also implements [async iteration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of). Certain modules like [output parsers](/v0.1/docs/modules/model_io/output_parsers/) also support "transform"-style streaming, where streamed LLM or chat model chunks are transformed into a different format as they are generated.

LangChain also includes a special [`HttpResponseOutputParser`](/v0.1/docs/modules/model_io/output_parsers/types/http_response/) for transforming LLM outputs into encoded byte streams for `text/plain` and `text/event-stream` content types.

Thus, you can pass streaming LLM responses directly into [web HTTP response objects](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) like this:

    import { ChatOpenAI } from "@langchain/openai";import { ChatPromptTemplate } from "@langchain/core/prompts";import { HttpResponseOutputParser } from "langchain/output_parsers";const TEMPLATE = `You are a pirate named Patchy. All responses must be extremely verbose and in pirate dialect.{input}`;const prompt = ChatPromptTemplate.fromTemplate(TEMPLATE);export async function POST() {  const model = new ChatOpenAI({    temperature: 0.8,    model: "gpt-3.5-turbo-1106",  });  const outputParser = new HttpResponseOutputParser();  const chain = prompt.pipe(model).pipe(outputParser);  const stream = await chain.stream({    input: "Hi there!",  });  return new Response(stream);}

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [HttpResponseOutputParser](https://api.js.langchain.com/classes/langchain_output_parsers.HttpResponseOutputParser.html) from `langchain/output_parsers`

### Streaming intermediate chain steps[​](#streaming-intermediate-chain-steps "Direct link to Streaming intermediate chain steps")

The `.streamLog` LCEL method streams back intermediate chain steps as [JSONPatch](https://jsonpatch.com/) chunks. See [this page for an in-depth example](/v0.1/docs/expression_language/interface/#stream-log), noting that because LangChain.js works in the browser, you can import and use the `applyPatch` method from there.

Error handling[​](#error-handling "Direct link to Error handling")
------------------------------------------------------------------

You can handle errors via try/catch for the standard `.invoke()` LCEL method as usual:

    import { ChatOpenAI } from "@langchain/openai";import { ChatPromptTemplate } from "@langchain/core/prompts";import { HttpResponseOutputParser } from "langchain/output_parsers";const TEMPLATE = `You are a pirate named Patchy. All responses must be extremely verbose and in pirate dialect.{input}`;const prompt = ChatPromptTemplate.fromTemplate(TEMPLATE);const model = new ChatOpenAI({  temperature: 0.8,  model: "gpt-3.5-turbo-1106",  apiKey: "INVALID_KEY",});const outputParser = new HttpResponseOutputParser();const chain = prompt.pipe(model).pipe(outputParser);try {  await chain.invoke({    input: "Hi there!",  });} catch (e) {  console.log(e);}/*  AuthenticationError: 401 Incorrect API key provided: INVALID_KEY. You can find your API key at https://platform.openai.com/account/api-keys.      at Function.generate (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/error.ts:71:14)      at OpenAI.makeStatusError (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/core.ts:371:21)      at OpenAI.makeRequest (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/core.ts:429:24)      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)      at async file:///Users/jacoblee/langchain/langchainjs/libs/langchain-openai/dist/chat_models.js:646:29      at RetryOperation._fn (/Users/jacoblee/langchain/langchainjs/node_modules/p-retry/index.js:50:12) {    status: 401,*/

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [HttpResponseOutputParser](https://api.js.langchain.com/classes/langchain_output_parsers.HttpResponseOutputParser.html) from `langchain/output_parsers`

The `.stream()` method will also wait until the first chunk is ready before resolving. This means that you can handle immediate errors that occur with the same pattern:

    import { ChatOpenAI } from "@langchain/openai";import { ChatPromptTemplate } from "@langchain/core/prompts";import { HttpResponseOutputParser } from "langchain/output_parsers";const TEMPLATE = `You are a pirate named Patchy. All responses must be extremely verbose and in pirate dialect.{input}`;const prompt = ChatPromptTemplate.fromTemplate(TEMPLATE);const model = new ChatOpenAI({  temperature: 0.8,  model: "gpt-3.5-turbo-1106",  apiKey: "INVALID_KEY",});const outputParser = new HttpResponseOutputParser();const chain = prompt.pipe(model).pipe(outputParser);try {  await chain.stream({    input: "Hi there!",  });} catch (e) {  console.log(e);}/*  AuthenticationError: 401 Incorrect API key provided: INVALID_KEY. You can find your API key at https://platform.openai.com/account/api-keys.      at Function.generate (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/error.ts:71:14)      at OpenAI.makeStatusError (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/core.ts:371:21)      at OpenAI.makeRequest (/Users/jacoblee/langchain/langchainjs/node_modules/openai/src/core.ts:429:24)      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)      at async file:///Users/jacoblee/langchain/langchainjs/libs/langchain-openai/dist/chat_models.js:646:29      at RetryOperation._fn (/Users/jacoblee/langchain/langchainjs/node_modules/p-retry/index.js:50:12) {    status: 401,*/

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [HttpResponseOutputParser](https://api.js.langchain.com/classes/langchain_output_parsers.HttpResponseOutputParser.html) from `langchain/output_parsers`

Note that other errors that occur while streaming (for example, broken connections) cannot be handled this way since once the initial HTTP response is sent, there is no way to alter things like status codes or headers.

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

*   [Next.js](/v0.1/docs/guides/deployment/nextjs/)
*   [SvelteKit](/v0.1/docs/guides/deployment/sveltekit/)
*   [WebLangChain](https://github.com/langchain-ai/weblangchain/blob/main/nextjs/app/api/chat/stream_log/route.ts), a live deployment of a Next.js backend that uses `streamLog`.

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Debugging

](/v0.1/docs/guides/debugging/)[

Next

Next.js

](/v0.1/docs/guides/deployment/nextjs/)

*   [Compatibility](#compatibility)
*   [Streaming over HTTP](#streaming-over-http)
    *   [Streaming intermediate chain steps](#streaming-intermediate-chain-steps)
*   [Error handling](#error-handling)
*   [Next steps](#next-steps)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.