Already connecting an agent? Start with Connect LangChain to Slack. This page is the adapter API.
@novu/framework/langchain lets you write agent handlers with LangChain. Return a LangChainAgentConfig from onMessage and Novu runs the agent with createAgent() for you - including streaming, tool approval gating, and resume from ctx.history.
Before you start#
Completed Connect LangChain? Your agent, bridge, and project are already set up - jump to Minimal agent.
Otherwise connect first, then come back here for adapter details.
Prerequisites#
Install the framework, LangChain, and a model provider:
@novu/framework, langchain, and @langchain/core are required; pick one provider package. Examples on this page use OpenAI model strings - see the LangChain integrations catalog for others.
Minimal agent#
Import agent from @novu/framework/langchain, then return a LangChainAgentConfig from onMessage:
Make sure the agent id ('support-bot') matches the Identifier in your dashboard, and that this handler is registered on your bridge route - see Connecting your app.
On Next.js, model strings need LangChain listed in serverExternalPackages. See Next.js and Turbopack. npx novu connect --runtime langchain configures this for you.
For handlers, replies, signals, typing, and tool approval, see Building blocks. The sections below cover what’s specific to the LangChain adapter.
Returning from onMessage#
On the LangChain path, onMessage (and onToolApproval) can return a LangChain result in addition to the usual reply types:
| Return | Behavior |
|---|---|
LangChainAgentConfig | Novu runs createAgent() and streams the assistant text into the thread - see Stream a reply |
{ messages: BaseMessage[] } | Novu delivers the final assistant text from your pre-invoked agent or graph. Wrap history with hydrateUnreachableAttachmentUrls before invoke - see Invoke your own agent |
BaseMessage | Delivered as a normal reply (assistant text extracted from the message) |
string / JSX Card | Delivered as a normal reply - see Reply |
| nothing | After you call ctx.reply() yourself |
Shorthand when you only need onMessage: pass the handler directly.
Next.js and Turbopack#
LangChain resolves provider packages for model strings via a runtime dynamic import(). Next.js Turbopack cannot analyze that expression and fails with:
Keep model strings and externalize LangChain in next.config.mjs:
npx novu connect --runtime langchain scaffolds this for you (and adds the provider package you selected).
toLangChainMessages()#
Convert conversation context into LangChain BaseMessage[] when you invoke an agent or graph yourself. Pass ctx so a workflow-origin notification (ctx.notification) is prepended as an AIMessage with capped notification text and the full JSON payload. See Workflow-assigned agents for how that notification is assigned and hydrated.
ctx.historyalready includes the current inbound message - do not append themessageargument on top of it.- When
ctx.notificationis set, it includesbody(capped outbound notification text) pluspayload,workflowId, and ids. - Pass
systemonly when you are not settingsystemonLangChainAgentConfig- Novu forwards that field tocreateAgent({ prompt })for you. - Tool approval cycles in history are mapped automatically, so auto-resume (below) works.
- Returning a
LangChainAgentConfigalready hydrates unreachable attachment URLs (http/localhost) before invoke. If you callinvoke()yourself, wraptoLangChainMessages()inhydrateUnreachableAttachmentUrls. Public HTTPS signed URLs stay as URLs; local or HTTP URLs are fetched and inlined as base64 so hosted providers can read the file. - Pass
ctx.historyinstead ofctxto skip automatic origin injection when handling typed fields withisFromWorkflow; prepend your ownAIMessageinstead.
Invoke your own agent#
If you already run a LangChain agent or graph in your handler, return its messages and Novu delivers the final text - tool approval is not managed on this path:
This path delivers the text once your invoke finishes. To stream from your own agent or graph, pass its text chunks to ctx.reply() - see Stream a reply.
Automatic tool approval#
Set needsApproval on LangChainAgentConfig. When the model calls a gated tool, Novu posts the Approve / Deny card and pauses the turn. After the user decides, Novu re-runs onMessage - toLangChainMessages(ctx) replays the approval cycle so the agent continues with no extra code from you.
Add onToolApproval when you need a hook after the click. See Tool approval for the full API.
Turn failures and onError#
When Novu runs the agent for a returned LangChainAgentConfig and the run fails - for example the provider errors or the model call fails - the failure propagates to the same dispatch boundary as a thrown handler error. The same applies when you await your own agent or graph in onMessage and the invoke throws before you return. Register onError on the agent to log, suppress user notification, send custom copy, or fall back to Novu’s generic message.
A gated tool waiting for user approval pauses the turn with an approval card - that is not a turn failure and does not trigger onError. See Handlers and context - onError for the full pipeline.
If you stream inside onMessage yourself, stream failures throw from your handler the same way as any other handler error.
Related#
Return AI SDK results from handlers with automatic tool approval resume.
Show a Thinking… or custom status while your handler runs.
Markdown, attachments, and interactive cards.
The shared Approve / Deny primitive.
Wire handlers without a Novu runtime adapter.