> ## Documentation Index
> Fetch the complete documentation index at: https://flow.engineer/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> Connect a LangChain model or chain to your messaging conversations.

Connect your LangChain model or chain to the Messaging API. Each incoming message starts a run, and Flow delivers its streamed answer as chat bubbles.

```ts TypeScript
// npm install @flow-engineer/messaging @langchain/openai
import { ChatOpenAI } from "@langchain/openai";
import { FlowMessaging, contentText } from "@flow-engineer/messaging";

const flow = new FlowMessaging();
const model = new ChatOpenAI({ model: "gpt-4.1-mini" });

for await (const event of flow.events.stream({ types: ["message.received"] })) {
  const stream = await model.stream([
    ["system", "You are the assistant for Asha's Bakery. Answer in short messages."],
    ["human", contentText(event.data.message.content)],
  ]);
  await event.conversation.reply(stream); // reads each chunk's content
}
```

```python Python (webhook reply)
# pip install langchain-openai fastapi
# Plain HTTP, no Flow SDK; the Python SDK (pip install flow-messaging) wraps the same calls. This answers in the webhook response.
from fastapi import FastAPI, Request
from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4.1-mini")
app = FastAPI()

@app.post("/flow")
async def flow_webhook(request: Request):
    event = await request.json()  # verify Flow-Signature first: see Events and webhooks
    if event["type"] != "message.received":
        return {}
    text = event["data"]["message"]["content"].get("text", "")
    answer = await model.ainvoke([("system", "You are the assistant for Asha's Bakery."), ("human", text)])
    return {"reply": answer.content}  # a string is a basic message, sent exactly as written
```

## How it fits

* Any runnable whose `.stream()` yields message chunks works with `reply()`: chat models, `prompt.pipe(model)` chains, and chains ending in a string output parser.
* The Python version answers in the webhook response (one round trip). Flow waits 10 seconds for the answer, so this suits fast chains; for slower ones answer `{}` at once and send later with `POST /v0/conversations/{conversation_id}/basic`. Flow sends the text exactly as written (it renders no markdown), so ask the model for plain text; the reply goes out as one message, so keep it within the channel's text limit (4096 characters on Telegram). See [Events and webhooks](https://flow.engineer/docs/unified-messaging-api/concepts/events-and-webhooks.md#reply-in-the-webhook-response).
* Keep chat history per `conversation.id`, or read it from `GET /v0/conversations/{conversation_id}/messages`.
* To see each turn's model and tool calls and get every reply graded, send this agent's traces to Flow Observe: [Traces from agent frameworks](https://flow.engineer/docs/unified-messaging-api/observe/frameworks.md).

## Full example

[examples/langchain](https://github.com/flow-engineer/sdk/tree/main/examples/langchain) on GitHub.
