Skip to content

Conversation

christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Aug 26, 2025

This patch migrates the createReactAgent from LangGraphJS to LangChain canonical. Here is a list of changes that users can expect from this updated agent:

💥 Breaking Changes

  • The createReactAgent doesn't support pre-bound models anymore as it is required to bind tools itself
    const llm = new ChatOpenAI({ model: "gpt-4o" }).bindTools([tool]);
    // ❌ not supported anymore
    const agent = createReactAgent({ llm });
    Instead, users have to pass a along tools to the createReactAgent function.
    const llm = new ChatOpenAI({ model: "gpt-4o" })
    // ✅ supported
    const agent = createReactAgent({ llm });
  • The ToolNode doesn't send an Please fix your mistakes. message anymore, instead it will just throw
    directly if the user doesn't handle the error via handleToolError

🚀 New Features

  • ToolNode now also takes a callback as argument to catch errors and send custom tool call responses back to the model, e.g.:
    const toolNode = new ToolNode([toolWithError], {
      handleToolErrors: (error, toolCall) => {
        // handle the error and return a custom tool call response
        console.error(error);
        return new ToolMessage({
          content: "handled error",
          tool_call_id: toolCall.id!,
        });
      },
    });
    const agent = createReactAgent({
      llm,
      tools: [toolNode],
    });
  • Creating structured outputs is now supported via tool calls or JSON schema (if supported by the model).
    While before the agent would make an additional call to the model to parse the output, now it will just
    return the raw output from the tool call or model directly.
    const llm = new ChatOpenAI({ model: "gpt-4o" })
    const agent = createReactAgent({
      llm,
      // by default, the agent will use tool calls to create structured outputs
      responseFormat: z.object({
        weather: z.string(),
        temperature: z.number(),
      }),
    });
    const result = await agent.invoke({
      messages: [new HumanMessage("What is the weather in Tokyo?")],
    });
    console.log(result.structuredOutput);
    Alternatively, users can also pass a JSON schema to the responseFormat option to create structured outputs.
    const agent = createReactAgent({
      llm,
      responseFormat: z.object({
        weather: z.string(),
        temperature: z.number(),
      }),
    });
    If your model supports JSON schema, you can explicitly request it via the nativeOutput function:
    import { nativeOutput } from "langchain";
    const agent = createReactAgent({
      llm,
      responseFormat: nativeOutput(z.object({
        weather: z.string(),
        temperature: z.number(),
      })),
    });
  • When using tool calls to create structured outputs, users now have the ability to catch issues
    with multiple tool calls or invalid parsing by providing a handleError function.
    const agent = createReactAgent({
      llm,
      responseFormat: toolOutput([
        z.object({
          weather: z.string(),
          temperature: z.number(),
        })
      ], {
        handleError: (error) => {
          return `Failed to parse the output: ${error}, please try again.`;
        },
      }),    

createReactAgent API Reference

The createReactAgent function is the main entry point for creating a React agent. It takes in a set of parameters and returns an agent instance. All related types are exported from the langchain package.

  • createReactAgent: the main function to create a React agent.
  • toolOutput: a function to create a tool output.
  • nativeOutput: a function to create a native output.
  • AgentState: the state of the agent.
  • AgentRuntime: the runtime of the agent.
  • CreateAgentToolConfig: the runnable config when accessing runtime information within tools.
import {
  createReactAgent,
  toolOutput,
  nativeOutput,
  type AgentState,
  type AgentRuntime,
  type CreateAgentToolConfig
} from "langchain";

Copy link

vercel bot commented Aug 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Error Error Comment Aug 27, 2025 10:49pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Aug 27, 2025 10:49pm

@christian-bromann
Copy link
Member Author

Blocked on #8793

linter

more fixes

fix formatting

feat(agents): migrate createReactAgent to core LangChain project

update pnpm-lock

migrate code

ported all code

add tests

type massage

remove coverage dir

add readme

minor tweaks

add integration tests

don't generate import maps if not exported as named import

more PR updates

lint fixes

migrate: use Send for each of the tool calls

migrate: Allow partially applying tool calls via postModelHook

more tests

fix linter

update vitest config

docs(agents): create extensive docs for various createReactAgent features

update examples

fix environment tests

update custom system prompt example

update example for control over message generation

rename to message preparation

add preModelHook for better destinction with controlOverMessagePreparation

update accessExternalContext example

update accessExternalContextInTools

update accessLongTermMemory

update example for accessLongTermMemoryInTools

update accessThreadLevelState example

update accessThreadLevelStateInTools example

update postModelHook example

update updateThreadLevel example

update updateToolsBeforeModelCall example

use InMemoryStore in updateLongTermMemoryInTools

only export what's important for the examples

demonstrate model choice by context

proper expose LangGraphRunnableConfig

enable human-in-the-loop

feat(createReactAgent): `createReactAgent` v1 development (#8667)
@christian-bromann christian-bromann force-pushed the cb/v1-prebuilts-examples branch from 6387c21 to c3af124 Compare August 27, 2025 22:45
@christian-bromann christian-bromann merged commit 2e757e8 into v1 Aug 27, 2025
108 of 109 checks passed
@christian-bromann christian-bromann deleted the cb/v1-prebuilts-examples branch August 27, 2025 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants