LangChain integration
This guide covers the LangChain(opens in a new tab) adapter for Document Authoring AI.
For the broader AI architecture, refer to the overview guide.
For a similar integration pattern, refer to the Vercel AI SDK integration guide. The server gives document tools to the model, and the browser executes validated calls.
Only the server-side adapter changes.
For the conceptual model and validation logic, refer to the agentic tools guide and the review and approval guide.
Tool definitions
Use the framework-neutral helpers first. Then convert them to the LangChain shape with @nutrient-sdk/document-authoring-ai/langchain.
import { getAiPromptGuide, getAiToolDefinitions,} from "@nutrient-sdk/document-authoring-ai";import { toLangChainTools } from "@nutrient-sdk/document-authoring-ai/langchain";
const definitions = getAiToolDefinitions();const promptGuide = getAiPromptGuide();const tools = toLangChainTools(definitions);toLangChainTools returns name, description, and schema for each tool, but it doesn’t attach execution handlers.
Document edits run in the browser because the editor runs there.
Each tool’s schema is already a Zod schema. Pass the tool definitions directly to model.bindTools(opens in a new tab), or use them to construct DynamicStructuredTool(opens in a new tab) instances.
You don’t need additional wrapping.
Use promptGuide as the system prompt when you invoke the model. The Vercel integration uses the same prompt guide.
The prompt guide tells the model how to handle element IDs, and it also explains the read-before-write rule.
If your backend uses Python LangChain, LangGraph, or another non-TypeScript runtime, export the tool and prompt definitions as JSON. For more information, refer to the non-TypeScript backends guide.
Workflow structured output
For proofreading, translation, or a custom workflow, bind the workflow schema to the model with model.withStructuredOutput(opens in a new tab).
Install @langchain/openai for this OpenAI example and set OPENAI_API_KEY on your server. Use Document Authoring AI 2.0 with Document Authoring SDK 1.21.0 or later.
import { ChatOpenAI } from "@langchain/openai";import { getBuiltInWorkflow, prepareWorkflowRun, type WorkflowInput,} from "@nutrient-sdk/document-authoring-ai";import { toLangChainWorkflowEditsSchema } from "@nutrient-sdk/document-authoring-ai/langchain";
const model = new ChatOpenAI({ model: "gpt-5.4-mini" });
export async function translate(workflowInput: WorkflowInput) { const workflow = getBuiltInWorkflow("translation", { targetLanguage: "french" }); const run = prepareWorkflowRun({ workflow, input: workflowInput }); const structuredModel = model.withStructuredOutput( toLangChainWorkflowEditsSchema(), { name: workflow.name }, );
try { const edits = await structuredModel.invoke([ { role: "system", content: run.systemPrompt }, { role: "user", content: run.createPrompt() }, ]); return run.apply(edits); } catch (error) { console.error("Translation workflow failed", error); throw error; }}Pass the complete WorkflowInput from the browser to this function. The adapter returns JSON Schema for focused edits, and run.apply(...) converts the model response into { replacementFragment }.
Return that output to the browser for applyWorkflowOutput(...), and keep the same run for the request and response. The browser validates the replacement before changing the document. See the workflows guide for browser code and retry handling.
Browser-side execution
The browser code works the same way for each server framework, so use the same three steps for LangChain.
- Build a toolkit from the live
DocAuthEditorwithgetAiToolkitfrom@nutrient-sdk/document-authoring-ai/editor. - Apply your Edit, Review, or View policy with
isAiWriteToolName. - Pass the raw tool call to
executeTool.
The toolkit validates the call before it changes the document.
For the full browser loop, refer to the agentic tools guide.
Attach the executed result to the agent’s tool-call message, and use the same pattern you use for other tool results in your LangChain setup.