LangChain
LangChain · LangChain WhatsApp · LangChain Telegram bot · LangChain iMessage · LangChain chatbot WhatsApp API · LangChain messaging integrationConnect 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.
// npm install @flow-engineer/messaging @langchain/openaiimport { 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}# 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, Requestfrom 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 writtenHow it fits
Section titled “How it fits”- Any runnable whose
.stream()yields message chunks works withreply(): 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 withPOST /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. - Keep chat history per
conversation.id, or read it fromGET /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.
Full example
Section titled “Full example”examples/langchain on GitHub.
Was this page helpful?
Thanks, we read every comment.