Skip to content

Conversation

christian-bromann
Copy link
Member

Description

This PR adds support for AbortSignal in the createReactAgent function, allowing users to cancel agent execution and tool invocations. This is particularly useful for long-running agent operations where users may want to cancel execution based on external events or user interaction.

Key Changes:

  1. Added abortSignal parameter to CreateReactAgentParams - Users can now pass an AbortSignal when creating an agent
  2. Signal propagation to tools - The abort signal is passed to the ToolNode and properly merged with any existing signals from LangGraph's Pregel class
  3. Signal merging with AbortSignal.any() - When both agent-level and config-level signals are present, they are merged using AbortSignal.any() (with appropriate TypeScript declarations for this newer API)
  4. Improved error propagation - Abort signal reasons are now properly propagated throughout the execution chain
  5. Comprehensive test coverage - Added tests to verify abort functionality works correctly at both the agent and tool levels

Usage Example:

const abortController = new AbortController();

const agent = createReactAgent({
  llm: model,
  tools: [myTool],
  abortSignal: abortController.signal,
});

// Start agent execution
const executionPromise = agent.invoke({
  messages: [{ role: "user", content: "Do something long running" }],
});

// Cancel after some condition
setTimeout(() => {
  abortController.abort("User cancelled operation");
}, 5000);

Additional Improvements:

  • Fixed build script to not clean in watch mode (prevents IDE issues with missing types)
  • Updated signal utilities in langchain-core to properly extract and propagate abort reasons
  • Added TypeScript declarations for AbortSignal.any() which is not yet in TypeScript's lib

Fixes #(issue)

Copy link

vercel bot commented Aug 11, 2025

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

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Ready Ready Preview Comment Aug 16, 2025 9:14pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Aug 16, 2025 9:14pm

@hntrl
Copy link
Contributor

hntrl commented Aug 12, 2025

I'd make the argument that an AbortSignal is better served as a call option then it is a "constructor" option.

@christian-bromann
Copy link
Member Author

I'd make the argument that an AbortSignal is better served as a call option then it is a "constructor" option.

We should support both, a global abort signal that acts as default and one as invoke option that allows to overwrite the global one.

@christian-bromann christian-bromann merged commit c9d3603 into cb/v1-prebuilts-typing Aug 16, 2025
30 of 33 checks passed
@christian-bromann christian-bromann deleted the cb/v1-prebuilts-abortsignal branch August 16, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants