Skip to content

Conversation

@christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Sep 4, 2025

We want to introduce a new middleware concept that allows user to create various middlewares for createAgent.

Requirements

We expect a middleware to:

  • contain the following properties:

    • name - for logging and introspection

    • stateSchema - defines a specification for state updates

    • contextSchema - defines a specification for context

    • beforeModel - runs before the model is invoked, allows to update state or use controls

      (property) beforeModel?: (
        state: TState,
        runtime: Runtime<TState>,
        controls: Controls<TState>
      ) => MiddlewareResult<TState> | Promise<MiddlewareResult<TState>>
    • prepareModelRequest - runs before the model is invoked, allows to update model call parameters

      (property) prepareModelRequest?: (
        options: ModelRequest,
        state: TState,
        runtime: Runtime<TState>
      ) => Promise<PreparedCall | undefined> | PreparedCall | undefined;
    • afterModel - runs after the model is invoked, allows to update state or use controls

      (property) afterModel?: (
        state: TState,
        runtime: Runtime<TState>,
        controls: Controls<TState>
      ) => MiddlewareResult<TState> | Promise<MiddlewareResult<TState>>
  • have access to state: an objects that holds information that persists across multiple invocations and is a union of the middlewares defined stateSchema and a built-in schema, e.g. currently only containing messages

  • have access to runtime: an object that holds readonly information containing context data and other useful meta information useful for a specific invocation

  • Any errors thrown within prepareCall or beforeModel and afterModel should bubble up

For more documentation, please refer to: https://docs.langchain.com/oss/javascript/langchain-middleware

@changeset-bot
Copy link

changeset-bot bot commented Sep 4, 2025

🦋 Changeset detected

Latest commit: 0891fdb

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Sep 4, 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 Sep 8, 2025 7:58am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Sep 8, 2025 7:58am

Introduce a new middleware concept for createAgent in langchain.
@christian-bromann christian-bromann marked this pull request as ready for review September 8, 2025 07:55
@christian-bromann christian-bromann merged commit ed18642 into v1 Sep 8, 2025
35 checks passed
@christian-bromann christian-bromann deleted the cb/agent-middleware branch September 8, 2025 08:02
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