Skip to content

The agent engineering platform.

feat(langchain_v1): add `async` support for `create_agent` (#33175)

Sydney Runklea10e880c0094
diff --git a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py
index 15c8a5a647..f9b1e4d31e 100644
--- a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py
+++ b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py
@@ -3,6 +3,7 @@
from typing import Any, Literal
from langchain_core.messages import AIMessage, ToolCall, ToolMessage
+from langgraph.runtime import Runtime
from langgraph.types import interrupt
from typing_extensions import NotRequired, TypedDict
@@ -145,7 +146,7 @@ class HumanInTheLoopMiddleware(AgentMiddleware):
self.interrupt_on = resolved_tool_configs
self.description_prefix = description_prefix
- def after_model(self, state: AgentState) -> dict[str, Any] | None: # type: ignore[override]
+ def after_model(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None: # noqa: ARG002
"""Trigger interrupt flows for relevant tool calls after an AIMessage."""
messages = state["messages"]
if not messages:
diff --git a/libs/langchain_v1/langchain/agents/middleware/planning.py b/libs/langchain_v1/langchain/agents/middleware/planning.py
index 5fb451dcf5..98e76b62fd 100644
--- a/libs/langchain_v1/langchain/agents/middleware/planning.py
+++ b/libs/langchain_v1/langchain/agents/middleware/planning.py
@@ -3,7 +3,7 @@
from __future__ import annotations
-from typing import Annotated, Literal
+from typing import TYPE_CHECKING, Annotated, Literal
from langchain_core.messages import ToolMessage
from langchain_core.tools import tool
@@ -13,6 +13,9 @@ from typing_extensions import NotRequired, TypedDict
from langchain.agents.middleware.types import AgentMiddleware, AgentState, ModelRequest
from langchain.tools import InjectedToolCallId
+if TYPE_CHECKING:
+ from langgraph.runtime import Runtime
+
class Todo(TypedDict):
"""A single todo item with content and status."""
@@ -183,10 +186,11 @@ class PlanningMiddleware(AgentMiddleware):
self.tools = [write_todos]
- def modify_model_request( # type: ignore[override]
+ def modify_model_request(
self,
request: ModelRequest,
- state: PlanningState, # noqa: ARG002
+ state: AgentState, # noqa: ARG002
+ runtime: Runtime, # noqa: ARG002
) -> ModelRequest:
"""Update the system prompt to include the todo system prompt."""
request.system_prompt = (
diff --git a/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py b/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
index 8f993cae06..3613263188 100644
--- a/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
+++ b/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
@@ -1,9 +1,11 @@
"""Anthropic prompt caching middleware."""
-from typing import Any, Literal
+from typing import Literal
from warnings import warn
-from langchain.agents.middleware.types import AgentMiddleware, ModelRequest
+from langgraph.runtime import Runtime
+
+from langchain.agents.middleware.types import AgentMiddleware, AgentState, ModelRequest
class AnthropicPromptCachingMiddleware(AgentMiddleware):
@@ -39,10 +41,11 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware):
self.min_messages_to_cache = min_messages_to_cache
self.unsupported_model_behavior = unsupported_model_behavior
- def modify_model_request( # type: ignore[override]
+ def modify_model_request(
self,
request: ModelRequest,
- state: dict[str, Any], # noqa: ARG002
+ state: AgentState, # noqa: ARG002
+ runtime: Runtime, # noqa: ARG002
) -> ModelRequest:
"""Modify the model request to add cache control blocks."""
try:
diff --git a/libs/langchain_v1/langchain/agents/middleware/summarization.py b/libs/langchain_v1/langchain/agents/middleware/summarization.py
index 814cc83a90..5138dc4267 100644
--- a/libs/langchain_v1/langchain/agents/middleware/summarization.py
+++ b/libs/langchain_v1/langchain/agents/middleware/summarization.py
@@ -16,6 +16,7 @@ from langchain_core.messages.utils import count_tokens_approximately, trim_messa
from langgraph.graph.message import (
REMOVE_ALL_MESSAGES,
)
+from langgraph.runtime import Runtime
from langchain.agents.middleware.types import AgentMiddleware, AgentState
from langchain.chat_models import BaseChatModel, init_chat_model
@@ -98,7 +99,7 @@ class SummarizationMiddleware(AgentMiddleware):
self.summary_prompt = summary_prompt
self.summary_prefix = summary_prefix
- def before_model(self, state: AgentState) -> dict[str, Any] | None: # type: ignore[override]
+ def before_model(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None: # noqa: ARG002
"""Process messages before model invocation, potentially triggering summarization."""
messages = state["messages"]
self._ensure_message_ids(messages)
diff --git a/libs/langchain_v1/langchain/agents/middleware/types.py b/libs/langchain_v1/langchain/agents/middleware/types.py
index b0612a2174..35ca31342c 100644
--- a/libs/langchain_v1/langchain/agents/middleware/types.py
+++ b/libs/langchain_v1/langchain/agents/middleware/types.py
@@ -3,7 +3,7 @@
from __future__ import annotations
from dataclasses import dataclass, field
-from inspect import signature
+from inspect import iscoroutinefunction
from typing import (
TYPE_CHECKING,
Annotated,
@@ -12,12 +12,15 @@ from typing import (
Generic,
Literal,
Protocol,
- TypeAlias,
- TypeGuard,
cast,
overload,
)
+from langchain_core.runnables import run_in_executor
+
+if TYPE_CHECKING:
+ from collections.abc import Awaitable
+
# needed as top level import for pydantic schema generation on AgentState
from langchain_core.messages import AnyMessage # noqa: TC002
from langgraph.channels.ephemeral_value import EphemeralValue
@@ -129,6 +132,11 @@ class AgentMiddleware(Generic[StateT, ContextT]):
def before_model(self, state: StateT, runtime: Runtime[ContextT]) -> dict[str, Any] | None:
"""Logic to run before the model is called."""
+ async def abefore_model(
+ self, state: StateT, runtime: Runtime[ContextT]
+ ) -> dict[str, Any] | None:
+ """Async logic to run before the model is called."""
+
def modify_model_request(
self,
request: ModelRequest,
@@ -138,16 +146,22 @@ class AgentMiddleware(Generic[StateT, ContextT]):
"""Logic to modify request kwargs before the model is called."""
return request
+ async def amodify_model_request(
+ self,
+ request: ModelRequest,
+ state: StateT,
+ runtime: Runtime[ContextT],
+ ) -> ModelRequest:
+ """Async logic to modify request kwargs before the model is called."""
+ return await run_in_executor(None, self.modify_model_request, request, state, runtime)
+
def after_model(self, state: StateT, runtime: Runtime[ContextT]) -> dict[str, Any] | None:
"""Logic to run after the model is called."""
-
-class _CallableWithState(Protocol[StateT_contra]):
- """Callable with AgentState as argument."""
-
- def __call__(self, state: StateT_contra) -> dict[str, Any] | Command | None:
- """Perform some logic with the state."""
- ...
+ async def aafter_model(
+ self, state: StateT, runtime: Runtime[ContextT]
+ ) -> dict[str, Any] | None:
+ """Async logic to run after the model is called."""
class _CallableWithStateAndRuntime(Protocol[StateT_contra, ContextT]):
@@ -155,53 +169,24 @@ class _CallableWithStateAndRuntime(Protocol[StateT_contra, ContextT]):
def __call__(
self, state: StateT_contra, runtime: Runtime[ContextT]
- ) -> dict[str, Any] | Command | None:
+ ) -> dict[str, Any] | Command | None | Awaitable[dict[str, Any] | Command | None]:
"""Perform some logic with the state and runtime."""
...
-class _CallableWithModelRequestAndState(Protocol[StateT_contra]):
- """Callable with ModelRequest and AgentState as arguments."""
-
- def __call__(self, request: ModelRequest, state: StateT_contra) -> ModelRequest:
- """Perform some logic with the model request and state."""
- ...
-
-
class _CallableWithModelRequestAndStateAndRuntime(Protocol[StateT_contra, ContextT]):
"""Callable with ModelRequest, AgentState, and Runtime as arguments."""
def __call__(
self, request: ModelRequest, state: StateT_contra, runtime: Runtime[ContextT]
- ) -> ModelRequest:
+ ) -> ModelRequest | Awaitable[ModelRequest]:
"""Perform some logic with the model request, state, and runtime."""
...
-_NodeSignature: TypeAlias = (
- _CallableWithState[StateT] | _CallableWithStateAndRuntime[StateT, ContextT]
-)
-_ModelRequestSignature: TypeAlias = (
- _CallableWithModelRequestAndState[StateT]
- | _CallableWithModelRequestAndStateAndRuntime[StateT, ContextT]
-)
-
-
-def is_callable_with_runtime(
- func: _NodeSignature[StateT, ContextT],
-) -> TypeGuard[_CallableWithStateAndRuntime[StateT, ContextT]]:
- return "runtime" in signature(func).parameters
-
-
-def is_callable_with_runtime_and_request(
- func: _ModelRequestSignature[StateT, ContextT],
-) -> TypeGuard[_CallableWithModelRequestAndStateAndRuntime[StateT, ContextT]]:
- return "runtime" in signature(func).parameters
-
-
@overload
def before_model(
- func: _NodeSignature[StateT, ContextT],
+ func: _CallableWithStateAndRuntime[StateT, ContextT],
) -> AgentMiddleware[StateT, ContextT]: ...
@@ -213,26 +198,27 @@ def before_model(
tools: list[BaseTool] | None = None,
jump_to: list[JumpTo] | None = None,
name: str | None = None,
-) -> Callable[[_NodeSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]: ...
+) -> Callable[
+ [_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]
+]: ...
def before_model(
- func: _NodeSignature[StateT, ContextT] | None = None,
+ func: _CallableWithStateAndRuntime[StateT, ContextT] | None = None,
*,
state_schema: type[StateT] | None = None,
tools: list[BaseTool] | None = None,
jump_to: list[JumpTo] | None = None,
name: str | None = None,
) -> (
- Callable[[_NodeSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]
+ Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]
| AgentMiddleware[StateT, ContextT]
):
"""Decorator used to dynamically create a middleware with the before_model hook.
Args:
- func: The function to be decorated. Can accept either:
- - `state: StateT` - Just the agent state
- - `state: StateT, runtime: Runtime[ContextT]` - State and runtime context
+ func: The function to be decorated. Must accept:
+ `state: StateT, runtime: Runtime[ContextT]` - State and runtime context
state_schema: Optional custom state schema type. If not provided, uses the default
AgentState schema.
tools: Optional list of additional tools to register with this middleware.
@@ -251,14 +237,14 @@ def before_model(
- `None` - No state updates or flow control
Examples:
- Basic usage with state only:
+ Basic usage:
```python
@before_model
- def log_before_model(state: AgentState) -> None:
+ def log_before_model(state: AgentState, runtime: Runtime) -> None:
print(f"About to call model with {len(state['messages'])} messages")
```
- Advanced usage with runtime and conditional jumping:
+ With conditional jumping:
```python
@before_model(jump_to=["end"])
def conditional_before_model(state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
@@ -269,36 +255,48 @@ def before_model(
With custom state schema:
```python
- @before_model(
- state_schema=MyCustomState,
- )
- def custom_before_model(state: MyCustomState) -> dict[str, Any]:
+ @before_model(state_schema=MyCustomState)
+ def custom_before_model(state: MyCustomState, runtime: Runtime) -> dict[str, Any]:
return {"custom_field": "updated_value"}
```
"""
- def decorator(func: _NodeSignature[StateT, ContextT]) -> AgentMiddleware[StateT, ContextT]:
- if is_callable_with_runtime(func):
-
- def wrapped_with_runtime(
- self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
- state: StateT,
- runtime: Runtime[ContextT],
- ) -> dict[str, Any] | Command | None:
- return func(state, runtime)
+ def decorator(
+ func: _CallableWithStateAndRuntime[StateT, ContextT],
+ ) -> AgentMiddleware[StateT, ContextT]:
+ is_async = iscoroutinefunction(func)
- wrapped = wrapped_with_runtime
- else:
+ if is_async:
- def wrapped_without_runtime(
+ async def async_wrapped(
self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
state: StateT,
+ runtime: Runtime[ContextT],
) -> dict[str, Any] | Command | None:
- return func(state) # type: ignore[call-arg]
-
- wrapped = wrapped_without_runtime # type: ignore[assignment]
+ return await func(state, runtime) # type: ignore[misc]
+
+ middleware_name = name or cast(
+ "str", getattr(func, "__name__", "BeforeModelMiddleware")
+ )
+
+ return type(
+ middleware_name,
+ (AgentMiddleware,),
+ {
+ "state_schema": state_schema or AgentState,
+ "tools": tools or [],
+ "before_model_jump_to": jump_to or [],
+ "abefore_model": async_wrapped,
+ },
+ )()
+
+ def wrapped(
+ self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
+ state: StateT,
+ runtime: Runtime[ContextT],
+ ) -> dict[str, Any] | Command | None:
+ return func(state, runtime) # type: ignore[return-value]
- # Use function name as default if no name provided
middleware_name = name or cast("str", getattr(func, "__name__", "BeforeModelMiddleware"))
return type(
@@ -319,7 +317,7 @@ def before_model(
@overload
def modify_model_request(
- func: _ModelRequestSignature[StateT, ContextT],
+ func: _CallableWithModelRequestAndStateAndRuntime[StateT, ContextT],
) -> AgentMiddleware[StateT, ContextT]: ...
@@ -330,26 +328,31 @@ def modify_model_request(
state_schema: type[StateT] | None = None,
tools: list[BaseTool] | None = None,
name: str | None = None,
-) -> Callable[[_ModelRequestSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]: ...
+) -> Callable[
+ [_CallableWithModelRequestAndStateAndRuntime[StateT, ContextT]],
+ AgentMiddleware[StateT, ContextT],
+]: ...
def modify_model_request(
- func: _ModelRequestSignature[StateT, ContextT] | None = None,
+ func: _CallableWithModelRequestAndStateAndRuntime[StateT, ContextT] | None = None,
*,
state_schema: type[StateT] | None = None,
tools: list[BaseTool] | None = None,
name: str | None = None,
) -> (
- Callable[[_ModelRequestSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]
+ Callable[
+ [_CallableWithModelRequestAndStateAndRuntime[StateT, ContextT]],
+ AgentMiddleware[StateT, ContextT],
+ ]
| AgentMiddleware[StateT, ContextT]
):
r"""Decorator used to dynamically create a middleware with the modify_model_request hook.
Args:
- func: The function to be decorated. Can accept either:
- - `request: ModelRequest, state: StateT` - Model request and agent state
- - `request: ModelRequest, state: StateT, runtime: Runtime[ContextT]` -
- Model request, state, and runtime context
+ func: The function to be decorated. Must accept:
+ `request: ModelRequest, state: StateT, runtime: Runtime[ContextT]` -
+ Model request, state, and runtime context
state_schema: Optional custom state schema type. If not provided, uses the default
AgentState schema.
tools: Optional list of additional tools to register with this middleware.
@@ -367,7 +370,9 @@ def modify_model_request(
Basic usage to modify system prompt:
```python
@modify_model_request
- def add_context_to_prompt(request: ModelRequest, state: AgentState) -> ModelRequest:
+ def add_context_to_prompt(
+ request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
if request.system_prompt:
request.system_prompt += "\n\nAdditional context: ..."
else:
@@ -375,7 +380,7 @@ def modify_model_request(
return request
```
- Advanced usage with runtime and custom model settings:
+ Usage with runtime and custom model settings:
```python
@modify_model_request
def dynamic_model_settings(
@@ -392,31 +397,42 @@ def modify_model_request(
"""
def decorator(
- func: _ModelRequestSignature[StateT, ContextT],
+ func: _CallableWithModelRequestAndStateAndRuntime[StateT, ContextT],
) -> AgentMiddleware[StateT, ContextT]:
- if is_callable_with_runtime_and_request(func):
+ is_async = iscoroutinefunction(func)
- def wrapped_with_runtime(
- self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
- request: ModelRequest,
- state: StateT,
- runtime: Runtime[ContextT],
- ) -> ModelRequest:
- return func(request, state, runtime)
+ if is_async:
- wrapped = wrapped_with_runtime
- else:
-
- def wrapped_without_runtime(
+ async def async_wrapped(
self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
request: ModelRequest,
state: StateT,
+ runtime: Runtime[ContextT],
) -> ModelRequest:
- return func(request, state) # type: ignore[call-arg]
-
- wrapped = wrapped_without_runtime # type: ignore[assignment]
+ return await func(request, state, runtime) # type: ignore[misc]
+
+ middleware_name = name or cast(
+ "str", getattr(func, "__name__", "ModifyModelRequestMiddleware")
+ )
+
+ return type(
+ middleware_name,
+ (AgentMiddleware,),
+ {
+ "state_schema": state_schema or AgentState,
+ "tools": tools or [],
+ "amodify_model_request": async_wrapped,
+ },
+ )()
+
+ def wrapped(
+ self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
+ request: ModelRequest,
+ state: StateT,
+ runtime: Runtime[ContextT],
+ ) -> ModelRequest:
+ return func(request, state, runtime) # type: ignore[return-value]
- # Use function name as default if no name provided
middleware_name = name or cast(
"str", getattr(func, "__name__", "ModifyModelRequestMiddleware")
)
@@ -438,7 +454,7 @@ def modify_model_request(
@overload
def after_model(
- func: _NodeSignature[StateT, ContextT],
+ func: _CallableWithStateAndRuntime[StateT, ContextT],
) -> AgentMiddleware[StateT, ContextT]: ...
@@ -450,26 +466,27 @@ def after_model(
tools: list[BaseTool] | None = None,
jump_to: list[JumpTo] | None = None,
name: str | None = None,
-) -> Callable[[_NodeSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]: ...
+) -> Callable[
+ [_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]
+]: ...
def after_model(
- func: _NodeSignature[StateT, ContextT] | None = None,
+ func: _CallableWithStateAndRuntime[StateT, ContextT] | None = None,
*,
state_schema: type[StateT] | None = None,
tools: list[BaseTool] | None = None,
jump_to: list[JumpTo] | None = None,
name: str | None = None,
) -> (
- Callable[[_NodeSignature[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]
+ Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]]
| AgentMiddleware[StateT, ContextT]
):
"""Decorator used to dynamically create a middleware with the after_model hook.
Args:
- func: The function to be decorated. Can accept either:
- - `state: StateT` - Just the agent state (includes model response)
- - `state: StateT, runtime: Runtime[ContextT]` - State and runtime context
+ func: The function to be decorated. Must accept:
+ `state: StateT, runtime: Runtime[ContextT]` - State and runtime context
state_schema: Optional custom state schema type. If not provided, uses the default
AgentState schema.
tools: Optional list of additional tools to register with this middleware.
@@ -491,40 +508,52 @@ def after_model(
Basic usage for logging model responses:
```python
@after_model
- def log_latest_message(state: AgentState) -> None:
+ def log_latest_message(state: AgentState, runtime: Runtime) -> None:
print(state["messages"][-1].content)
```
With custom state schema:
```python
@after_model(state_schema=MyCustomState, name="MyAfterModelMiddleware")
- def custom_after_model(state: MyCustomState) -> dict[str, Any]:
+ def custom_after_model(state: MyCustomState, runtime: Runtime) -> dict[str, Any]:
return {"custom_field": "updated_after_model"}
```
"""
- def decorator(func: _NodeSignature[StateT, ContextT]) -> AgentMiddleware[StateT, ContextT]:
- if is_callable_with_runtime(func):
-
- def wrapped_with_runtime(
- self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
- state: StateT,
- runtime: Runtime[ContextT],
- ) -> dict[str, Any] | Command | None:
- return func(state, runtime)
+ def decorator(
+ func: _CallableWithStateAndRuntime[StateT, ContextT],
+ ) -> AgentMiddleware[StateT, ContextT]:
+ is_async = iscoroutinefunction(func)
- wrapped = wrapped_with_runtime
- else:
+ if is_async:
- def wrapped_without_runtime(
+ async def async_wrapped(
self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
state: StateT,
+ runtime: Runtime[ContextT],
) -> dict[str, Any] | Command | None:
- return func(state) # type: ignore[call-arg]
-
- wrapped = wrapped_without_runtime # type: ignore[assignment]
+ return await func(state, runtime) # type: ignore[misc]
+
+ middleware_name = name or cast("str", getattr(func, "__name__", "AfterModelMiddleware"))
+
+ return type(
+ middleware_name,
+ (AgentMiddleware,),
+ {
+ "state_schema": state_schema or AgentState,
+ "tools": tools or [],
+ "after_model_jump_to": jump_to or [],
+ "aafter_model": async_wrapped,
+ },
+ )()
+
+ def wrapped(
+ self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
+ state: StateT,
+ runtime: Runtime[ContextT],
+ ) -> dict[str, Any] | Command | None:
+ return func(state, runtime) # type: ignore[return-value]
- # Use function name as default if no name provided
middleware_name = name or cast("str", getattr(func, "__name__", "AfterModelMiddleware"))
return type(
diff --git a/libs/langchain_v1/langchain/agents/middleware_agent.py b/libs/langchain_v1/langchain/agents/middleware_agent.py
index d2ca5a284c..f8494263bc 100644
--- a/libs/langchain_v1/langchain/agents/middleware_agent.py
+++ b/libs/langchain_v1/langchain/agents/middleware_agent.py
@@ -2,7 +2,6 @@
import itertools
from collections.abc import Callable, Sequence
-from inspect import signature
from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints
from langchain_core.language_models.chat_models import BaseChatModel
@@ -38,6 +37,8 @@ from langchain.tools import ToolNode
STRUCTURED_OUTPUT_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."
+ResponseT = TypeVar("ResponseT")
+
def _resolve_schema(schemas: set[type], schema_name: str, omit_flag: str | None = None) -> type:
"""Resolve schema by merging schemas and optionally respecting OmitFromSchema annotations.
@@ -130,9 +131,6 @@ def _handle_structured_output_error(
return False, ""
-ResponseT = TypeVar("ResponseT")
-
-
def create_agent( # noqa: PLR0915
*,
model: str | BaseChatModel,
@@ -212,15 +210,22 @@ def create_agent( # noqa: PLR0915
"Please remove duplicate middleware instances."
)
middleware_w_before = [
- m for m in middleware if m.__class__.before_model is not AgentMiddleware.before_model
+ m
+ for m in middleware
+ if m.__class__.before_model is not AgentMiddleware.before_model
+ or m.__class__.abefore_model is not AgentMiddleware.abefore_model
]
middleware_w_modify_model_request = [
m
for m in middleware
if m.__class__.modify_model_request is not AgentMiddleware.modify_model_request
+ or m.__class__.amodify_model_request is not AgentMiddleware.amodify_model_request
]
middleware_w_after = [
- m for m in middleware if m.__class__.after_model is not AgentMiddleware.after_model
+ m
+ for m in middleware
+ if m.__class__.after_model is not AgentMiddleware.after_model
+ or m.__class__.aafter_model is not AgentMiddleware.aafter_model
]
state_schemas = {m.state_schema for m in middleware}
@@ -367,13 +372,6 @@ def create_agent( # noqa: PLR0915
)
return request.model.bind(**request.model_settings)
- model_request_signatures: list[
- tuple[bool, AgentMiddleware[AgentState[ResponseT], ContextT]]
- ] = [
- ("runtime" in signature(m.modify_model_request).parameters, m)
- for m in middleware_w_modify_model_request
- ]
-
def model_request(state: AgentState, runtime: Runtime[ContextT]) -> dict[str, Any]:
"""Sync model request handler with sequential middleware processing."""
request = ModelRequest(
@@ -386,11 +384,17 @@ def create_agent( # noqa: PLR0915
)
# Apply modify_model_request middleware in sequence
- for use_runtime, m in model_request_signatures:
- if use_runtime:
+ for m in middleware_w_modify_model_request:
+ if m.__class__.modify_model_request is not AgentMiddleware.modify_model_request:
m.modify_model_request(request, state, runtime)
else:
- m.modify_model_request(request, state) # type: ignore[call-arg]
+ msg = (
+ f"No synchronous function provided for "
+ f'{m.__class__.__name__}.amodify_model_request".'
+ "\nEither initialize with a synchronous function or invoke"
+ " via the async API (ainvoke, astream, etc.)"
+ )
+ raise TypeError(msg)
# Get the final model and messages
model_ = _get_bound_model(request)
@@ -403,7 +407,6 @@ def create_agent( # noqa: PLR0915
async def amodel_request(state: AgentState, runtime: Runtime[ContextT]) -> dict[str, Any]:
"""Async model request handler with sequential middleware processing."""
- # Start with the base model request
request = ModelRequest(
model=model,
tools=[t.name for t in default_tools],
@@ -414,11 +417,8 @@ def create_agent( # noqa: PLR0915
)
# Apply modify_model_request middleware in sequence
- for use_runtime, m in model_request_signatures:
- if use_runtime:
- m.modify_model_request(request, state, runtime)
- else:
- m.modify_model_request(request, state) # type: ignore[call-arg]
+ for m in middleware_w_modify_model_request:
+ await m.amodify_model_request(request, state, runtime)
# Get the final model and messages
model_ = _get_bound_model(request)
@@ -440,14 +440,46 @@ def create_agent( # noqa: PLR0915
# Add middleware nodes
for m in middleware:
- if m.__class__.before_model is not AgentMiddleware.before_model:
+ if (
+ m.__class__.before_model is not AgentMiddleware.before_model
+ or m.__class__.abefore_model is not AgentMiddleware.abefore_model
+ ):
+ # Use RunnableCallable to support both sync and async
+ # Pass None for sync if not overridden to avoid signature conflicts
+ sync_before = (
+ m.before_model
+ if m.__class__.before_model is not AgentMiddleware.before_model
+ else None
+ )
+ async_before = (
+ m.abefore_model
+ if m.__class__.abefore_model is not AgentMiddleware.abefore_model
+ else None
+ )
+ before_node = RunnableCallable(sync_before, async_before)
graph.add_node(
- f"{m.__class__.__name__}.before_model", m.before_model, input_schema=state_schema
+ f"{m.__class__.__name__}.before_model", before_node, input_schema=state_schema
)
- if m.__class__.after_model is not AgentMiddleware.after_model:
+ if (
+ m.__class__.after_model is not AgentMiddleware.after_model
+ or m.__class__.aafter_model is not AgentMiddleware.aafter_model
+ ):
+ # Use RunnableCallable to support both sync and async
+ # Pass None for sync if not overridden to avoid signature conflicts
+ sync_after = (
+ m.after_model
+ if m.__class__.after_model is not AgentMiddleware.after_model
+ else None
+ )
+ async_after = (
+ m.aafter_model
+ if m.__class__.aafter_model is not AgentMiddleware.aafter_model
+ else None
+ )
+ after_node = RunnableCallable(sync_after, async_after)
graph.add_node(
- f"{m.__class__.__name__}.after_model", m.after_model, input_schema=state_schema
+ f"{m.__class__.__name__}.after_model", after_node, input_schema=state_schema
)
# add start edge
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
index 900a1cc9d2..3febca2e98 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
@@ -58,68 +58,68 @@ def test_create_agent_diagram(
snapshot: SnapshotAssertion,
):
class NoopOne(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
class NoopTwo(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
class NoopThree(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
class NoopFour(AgentMiddleware):
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopFive(AgentMiddleware):
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopSix(AgentMiddleware):
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopSeven(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopEight(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopNine(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopTen(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
- def modify_model_request(self, request, state):
+ def modify_model_request(self, request, state, runtime):
pass
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
class NoopEleven(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
pass
- def modify_model_request(self, request, state):
+ def modify_model_request(self, request, state, runtime):
pass
- def after_model(self, state):
+ def after_model(self, state, runtime):
pass
agent_zero = create_agent(
@@ -237,25 +237,25 @@ def test_create_agent_invoke(
calls = []
class NoopSeven(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
calls.append("NoopSeven.before_model")
- def modify_model_request(self, request, state):
+ def modify_model_request(self, request, state, runtime):
calls.append("NoopSeven.modify_model_request")
return request
- def after_model(self, state):
+ def after_model(self, state, runtime):
calls.append("NoopSeven.after_model")
class NoopEight(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
calls.append("NoopEight.before_model")
- def modify_model_request(self, request, state):
+ def modify_model_request(self, request, state, runtime):
calls.append("NoopEight.modify_model_request")
return request
- def after_model(self, state):
+ def after_model(self, state, runtime):
calls.append("NoopEight.after_model")
@tool
@@ -329,28 +329,28 @@ def test_create_agent_jump(
calls = []
class NoopSeven(AgentMiddleware):
- def before_model(self, state):
+ def before_model(self, state, runtime):
calls.append("NoopSeven.before_model")
- def modify_model_request(self, request, state):
+ def modify_model_request(self, request, state, runtime):
calls.append("NoopSeven.modify_model_request")
return request
- def after_model(self, state):
+ def after_model(self, state, runtime):
calls.append("NoopSeven.after_model")
class NoopEight(AgentMiddleware):
before_model_jump_to = ["end"]
- def before_model(self, state) -> dict[str, Any]:
+ def before_model(self, state, runtime) -> dict[str, Any]:
calls.append("NoopEight.before_model")
return {"jump_to": "end"}
- def modify_model_request(self, request, state) -> ModelRequest:
+ def modify_model_request(self, request, state, runtime) -> ModelRequest:
calls.append("NoopEight.modify_model_request")
return request
- def after_model(self, state):
+ def after_model(self, state, runtime):
calls.append("NoopEight.after_model")
@tool
@@ -404,12 +404,12 @@ def test_human_in_the_loop_middleware_no_interrupts_needed() -> None:
# Test with no messages
state: dict[str, Any] = {"messages": []}
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is None
# Test with message but no tool calls
state = {"messages": [HumanMessage(content="Hello"), AIMessage(content="Hi there")]}
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is None
# Test with tool calls that don't require interrupts
@@ -418,7 +418,7 @@ def test_human_in_the_loop_middleware_no_interrupts_needed() -> None:
tool_calls=[{"name": "other_tool", "args": {"input": "test"}, "id": "1"}],
)
state = {"messages": [HumanMessage(content="Hello"), ai_message]}
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is None
@@ -441,7 +441,7 @@ def test_human_in_the_loop_middleware_single_tool_accept() -> None:
return [{"type": "accept", "args": None}]
with patch("langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_accept):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -453,7 +453,7 @@ def test_human_in_the_loop_middleware_single_tool_accept() -> None:
)
state["messages"].append(AIMessage(content="test_tool called with result: Tool message"))
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
# No interrupts needed
assert result is None
@@ -484,7 +484,7 @@ def test_human_in_the_loop_middleware_single_tool_edit() -> None:
]
with patch("langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_edit):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -513,7 +513,7 @@ def test_human_in_the_loop_middleware_single_tool_response() -> None:
with patch(
"langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_response
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 2
@@ -552,7 +552,7 @@ def test_human_in_the_loop_middleware_multiple_tools_mixed_responses() -> None:
with patch(
"langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_mixed_responses
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert (
@@ -612,7 +612,7 @@ def test_human_in_the_loop_middleware_multiple_tools_edit_responses() -> None:
with patch(
"langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_edit_responses
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -654,7 +654,7 @@ def test_human_in_the_loop_middleware_edit_with_modified_args() -> None:
"langchain.agents.middleware.human_in_the_loop.interrupt",
side_effect=mock_edit_with_args,
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -687,7 +687,7 @@ def test_human_in_the_loop_middleware_unknown_response_type() -> None:
ValueError,
match=r"Unexpected human response: {'type': 'unknown', 'args': None}. Response action 'unknown' is not allowed for tool 'test_tool'. Expected one of \['accept', 'edit', 'response'\] based on the tool's configuration.",
):
- middleware.after_model(state)
+ middleware.after_model(state, None)
def test_human_in_the_loop_middleware_disallowed_action() -> None:
@@ -725,7 +725,7 @@ def test_human_in_the_loop_middleware_disallowed_action() -> None:
ValueError,
match=r"Unexpected human response: {'type': 'edit', 'args': {'action': 'test_tool', 'args': {'input': 'modified'}}}. Response action 'edit' is not allowed for tool 'test_tool'. Expected one of \['accept', 'response'\] based on the tool's configuration.",
):
- middleware.after_model(state)
+ middleware.after_model(state, None)
def test_human_in_the_loop_middleware_mixed_auto_approved_and_interrupt() -> None:
@@ -750,7 +750,7 @@ def test_human_in_the_loop_middleware_mixed_auto_approved_and_interrupt() -> Non
return [{"type": "accept", "args": None}]
with patch("langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_accept):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -787,7 +787,7 @@ def test_human_in_the_loop_middleware_interrupt_request_structure() -> None:
with patch(
"langchain.agents.middleware.human_in_the_loop.interrupt", side_effect=mock_capture_requests
):
- middleware.after_model(state)
+ middleware.after_model(state, None)
assert len(captured_requests) == 1
request = captured_requests[0]
@@ -820,7 +820,7 @@ def test_human_in_the_loop_middleware_boolean_configs() -> None:
"langchain.agents.middleware.human_in_the_loop.interrupt",
return_value=[{"type": "accept", "args": None}],
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -839,7 +839,7 @@ def test_human_in_the_loop_middleware_boolean_configs() -> None:
}
],
):
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
assert result is not None
assert "messages" in result
assert len(result["messages"]) == 1
@@ -847,7 +847,7 @@ def test_human_in_the_loop_middleware_boolean_configs() -> None:
middleware = HumanInTheLoopMiddleware(interrupt_on={"test_tool": False})
- result = middleware.after_model(state)
+ result = middleware.after_model(state, None)
# No interruption should occur
assert result is None
@@ -871,7 +871,7 @@ def test_human_in_the_loop_middleware_sequence_mismatch() -> None:
ValueError,
match=r"Number of human responses \(0\) does not match number of hanging tool calls \(1\)\.",
):
- middleware.after_model(state)
+ middleware.after_model(state, None)
# Test with too many responses
with patch(
@@ -885,7 +885,7 @@ def test_human_in_the_loop_middleware_sequence_mismatch() -> None:
ValueError,
match=r"Number of human responses \(2\) does not match number of hanging tool calls \(1\)\.",
):
- middleware.after_model(state)
+ middleware.after_model(state, None)
# Tests for AnthropicPromptCachingMiddleware
@@ -915,7 +915,7 @@ def test_anthropic_prompt_caching_middleware_initialization() -> None:
model_settings={},
)
- assert middleware.modify_model_request(fake_request, {}).model_settings == {
+ assert middleware.modify_model_request(fake_request, {}, None).model_settings == {
"cache_control": {"type": "ephemeral", "ttl": "5m"}
}
@@ -938,7 +938,7 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
ValueError,
match="AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models. Please install langchain-anthropic.",
):
- middleware.modify_model_request(fake_request, {})
+ middleware.modify_model_request(fake_request, {}, None)
langchain_anthropic = ModuleType("langchain_anthropic")
@@ -952,12 +952,12 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
ValueError,
match="AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models, not instances of",
):
- middleware.modify_model_request(fake_request, {})
+ middleware.modify_model_request(fake_request, {}, None)
middleware = AnthropicPromptCachingMiddleware(unsupported_model_behavior="warn")
with warnings.catch_warnings(record=True) as w:
- result = middleware.modify_model_request(fake_request, {})
+ result = middleware.modify_model_request(fake_request, {}, None)
assert len(w) == 1
assert (
"AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models. Please install langchain-anthropic."
@@ -967,7 +967,7 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
with warnings.catch_warnings(record=True) as w:
with patch.dict("sys.modules", {"langchain_anthropic": langchain_anthropic}):
- result = middleware.modify_model_request(fake_request, {})
+ result = middleware.modify_model_request(fake_request, {}, None)
assert result is fake_request
assert len(w) == 1
assert (
@@ -977,11 +977,11 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
middleware = AnthropicPromptCachingMiddleware(unsupported_model_behavior="ignore")
- result = middleware.modify_model_request(fake_request, {})
+ result = middleware.modify_model_request(fake_request, {}, None)
assert result is fake_request
with patch.dict("sys.modules", {"langchain_anthropic": {"ChatAnthropic": object()}}):
- result = middleware.modify_model_request(fake_request, {})
+ result = middleware.modify_model_request(fake_request, {}, None)
assert result is fake_request
@@ -1020,7 +1020,7 @@ def test_summarization_middleware_no_summarization_cases() -> None:
# Test when summarization is disabled
middleware_disabled = SummarizationMiddleware(model=model, max_tokens_before_summary=None)
state = {"messages": [HumanMessage(content="Hello"), AIMessage(content="Hi")]}
- result = middleware_disabled.before_model(state)
+ result = middleware_disabled.before_model(state, None)
assert result is None
# Test when token count is below threshold
@@ -1028,7 +1028,7 @@ def test_summarization_middleware_no_summarization_cases() -> None:
return 500 # Below threshold
middleware.token_counter = mock_token_counter
- result = middleware.before_model(state)
+ result = middleware.before_model(state, None)
assert result is None
@@ -1181,7 +1181,7 @@ def test_summarization_middleware_full_workflow() -> None:
]
state = {"messages": messages}
- result = middleware.before_model(state)
+ result = middleware.before_model(state, None)
assert result is not None
assert "messages" in result
@@ -1204,7 +1204,9 @@ def test_summarization_middleware_full_workflow() -> None:
def test_modify_model_request() -> None:
class ModifyMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
request.messages.append(HumanMessage("remember to be nice!"))
return request
@@ -1450,7 +1452,7 @@ def test_planning_middleware_modify_model_request(original_prompt, expected_prom
)
state: PlanningState = {"messages": [HumanMessage(content="Hello")]}
- modified_request = middleware.modify_model_request(request, state)
+ modified_request = middleware.modify_model_request(request, state, None)
assert modified_request.system_prompt.startswith(expected_prompt_prefix)
@@ -1577,7 +1579,7 @@ def test_planning_middleware_custom_system_prompt() -> None:
)
state: PlanningState = {"messages": [HumanMessage(content="Hello")]}
- modified_request = middleware.modify_model_request(request, state)
+ modified_request = middleware.modify_model_request(request, state, None)
assert modified_request.system_prompt == f"Original prompt\n\n{custom_system_prompt}"
@@ -1613,7 +1615,7 @@ def test_planning_middleware_custom_system_prompt_and_tool_description() -> None
)
state: PlanningState = {"messages": [HumanMessage(content="Hello")]}
- modified_request = middleware.modify_model_request(request, state)
+ modified_request = middleware.modify_model_request(request, state, None)
assert modified_request.system_prompt == custom_system_prompt
# Verify tool description
@@ -1661,3 +1663,236 @@ def test_planning_middleware_custom_system_prompt() -> None:
assert result["todos"] == [{"content": "Custom task", "status": "pending"}]
# assert custom system prompt is in the first AI message
assert "call the write_todos tool" in result["messages"][1].content
+
+
+# Async Middleware Tests
+async def test_create_agent_async_invoke() -> None:
+ """Test async invoke with async middleware hooks."""
+ calls = []
+
+ class AsyncMiddleware(AgentMiddleware):
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddleware.abefore_model")
+
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("AsyncMiddleware.amodify_model_request")
+ request.messages.append(HumanMessage("async middleware message"))
+ return request
+
+ async def aafter_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddleware.aafter_model")
+
+ @tool
+ def my_tool(input: str) -> str:
+ """A great tool"""
+ calls.append("my_tool")
+ return input.upper()
+
+ agent = create_agent(
+ model=FakeToolCallingModel(
+ tool_calls=[
+ [{"args": {"input": "yo"}, "id": "1", "name": "my_tool"}],
+ [],
+ ]
+ ),
+ tools=[my_tool],
+ system_prompt="You are a helpful assistant.",
+ middleware=[AsyncMiddleware()],
+ ).compile()
+
+ result = await agent.ainvoke({"messages": [HumanMessage("hello")]})
+
+ # Should have:
+ # 1. Original hello message
+ # 2. Async middleware message (first invoke)
+ # 3. AI message with tool call
+ # 4. Tool message
+ # 5. Async middleware message (second invoke)
+ # 6. Final AI message
+ assert len(result["messages"]) == 6
+ assert result["messages"][0].content == "hello"
+ assert result["messages"][1].content == "async middleware message"
+ assert calls == [
+ "AsyncMiddleware.abefore_model",
+ "AsyncMiddleware.amodify_model_request",
+ "AsyncMiddleware.aafter_model",
+ "my_tool",
+ "AsyncMiddleware.abefore_model",
+ "AsyncMiddleware.amodify_model_request",
+ "AsyncMiddleware.aafter_model",
+ ]
+
+
+async def test_create_agent_async_invoke_multiple_middleware() -> None:
+ """Test async invoke with multiple async middleware hooks."""
+ calls = []
+
+ class AsyncMiddlewareOne(AgentMiddleware):
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddlewareOne.abefore_model")
+
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("AsyncMiddlewareOne.amodify_model_request")
+ return request
+
+ async def aafter_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddlewareOne.aafter_model")
+
+ class AsyncMiddlewareTwo(AgentMiddleware):
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddlewareTwo.abefore_model")
+
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("AsyncMiddlewareTwo.amodify_model_request")
+ return request
+
+ async def aafter_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddlewareTwo.aafter_model")
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ tools=[],
+ system_prompt="You are a helpful assistant.",
+ middleware=[AsyncMiddlewareOne(), AsyncMiddlewareTwo()],
+ ).compile()
+
+ result = await agent.ainvoke({"messages": [HumanMessage("hello")]})
+
+ assert calls == [
+ "AsyncMiddlewareOne.abefore_model",
+ "AsyncMiddlewareTwo.abefore_model",
+ "AsyncMiddlewareOne.amodify_model_request",
+ "AsyncMiddlewareTwo.amodify_model_request",
+ "AsyncMiddlewareTwo.aafter_model",
+ "AsyncMiddlewareOne.aafter_model",
+ ]
+
+
+async def test_create_agent_async_jump() -> None:
+ """Test async invoke with async middleware using jump_to."""
+ calls = []
+
+ class AsyncMiddlewareOne(AgentMiddleware):
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddlewareOne.abefore_model")
+
+ class AsyncMiddlewareTwo(AgentMiddleware):
+ before_model_jump_to = ["end"]
+
+ async def abefore_model(self, state, runtime) -> dict[str, Any]:
+ calls.append("AsyncMiddlewareTwo.abefore_model")
+ return {"jump_to": "end"}
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ tools=[],
+ system_prompt="You are a helpful assistant.",
+ middleware=[AsyncMiddlewareOne(), AsyncMiddlewareTwo()],
+ ).compile()
+
+ result = await agent.ainvoke({"messages": []})
+
+ assert result == {"messages": []}
+ assert calls == ["AsyncMiddlewareOne.abefore_model", "AsyncMiddlewareTwo.abefore_model"]
+
+
+async def test_create_agent_mixed_sync_async_middleware() -> None:
+ """Test async invoke with mixed sync and async middleware."""
+ calls = []
+
+ class SyncMiddleware(AgentMiddleware):
+ def before_model(self, state, runtime) -> None:
+ calls.append("SyncMiddleware.before_model")
+
+ def modify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("SyncMiddleware.modify_model_request")
+ return request
+
+ def after_model(self, state, runtime) -> None:
+ calls.append("SyncMiddleware.after_model")
+
+ class AsyncMiddleware(AgentMiddleware):
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddleware.abefore_model")
+
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("AsyncMiddleware.amodify_model_request")
+ return request
+
+ async def aafter_model(self, state, runtime) -> None:
+ calls.append("AsyncMiddleware.aafter_model")
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ tools=[],
+ system_prompt="You are a helpful assistant.",
+ middleware=[SyncMiddleware(), AsyncMiddleware()],
+ ).compile()
+
+ result = await agent.ainvoke({"messages": [HumanMessage("hello")]})
+
+ # In async mode, both sync and async middleware should work
+ assert calls == [
+ "SyncMiddleware.before_model",
+ "AsyncMiddleware.abefore_model",
+ "SyncMiddleware.modify_model_request",
+ "AsyncMiddleware.amodify_model_request",
+ "AsyncMiddleware.aafter_model",
+ "SyncMiddleware.after_model",
+ ]
+
+
+def test_create_agent_sync_invoke_with_only_async_middleware_raises_error() -> None:
+ """Test that sync invoke with only async middleware works via run_in_executor."""
+
+ class AsyncOnlyMiddleware(AgentMiddleware):
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ return request
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ tools=[],
+ system_prompt="You are a helpful assistant.",
+ middleware=[AsyncOnlyMiddleware()],
+ ).compile()
+
+ with pytest.raises(
+ TypeError,
+ match="No synchronous function provided for AsyncOnlyMiddleware.amodify_model_request",
+ ):
+ agent.invoke({"messages": [HumanMessage("hello")]})
+
+
+def test_create_agent_sync_invoke_with_mixed_middleware() -> None:
+ """Test that sync invoke works with mixed sync/async middleware when sync versions exist."""
+ calls = []
+
+ class MixedMiddleware(AgentMiddleware):
+ def before_model(self, state, runtime) -> None:
+ calls.append("MixedMiddleware.before_model")
+
+ async def abefore_model(self, state, runtime) -> None:
+ calls.append("MixedMiddleware.abefore_model")
+
+ def modify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("MixedMiddleware.modify_model_request")
+ return request
+
+ async def amodify_model_request(self, request, state, runtime) -> ModelRequest:
+ calls.append("MixedMiddleware.amodify_model_request")
+ return request
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ tools=[],
+ system_prompt="You are a helpful assistant.",
+ middleware=[MixedMiddleware()],
+ ).compile()
+
+ result = agent.invoke({"messages": [HumanMessage("hello")]})
+
+ # In sync mode, only sync methods should be called
+ assert calls == [
+ "MixedMiddleware.before_model",
+ "MixedMiddleware.modify_model_request",
+ ]
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py
index a0e97731d2..ec8607ab19 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py
@@ -1,10 +1,12 @@
"""Consolidated tests for middleware decorators: before_model, after_model, and modify_model_request."""
+import pytest
from typing import Any
from typing_extensions import NotRequired
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.tools import tool
+from langgraph.runtime import Runtime
from langgraph.types import Command
from langchain.agents.middleware.types import (
@@ -37,7 +39,7 @@ def test_before_model_decorator() -> None:
@before_model(
state_schema=CustomState, tools=[test_tool], jump_to=["__end__"], name="CustomBeforeModel"
)
- def custom_before_model(state: CustomState) -> dict[str, Any]:
+ def custom_before_model(state: CustomState, runtime: Runtime) -> dict[str, Any]:
return {"jump_to": "__end__"}
assert isinstance(custom_before_model, AgentMiddleware)
@@ -46,7 +48,7 @@ def test_before_model_decorator() -> None:
assert custom_before_model.before_model_jump_to == ["__end__"]
assert custom_before_model.__class__.__name__ == "CustomBeforeModel"
- result = custom_before_model.before_model({"messages": [HumanMessage("Hello")]})
+ result = custom_before_model.before_model({"messages": [HumanMessage("Hello")]}, None)
assert result == {"jump_to": "__end__"}
@@ -59,7 +61,7 @@ def test_after_model_decorator() -> None:
jump_to=["model", "__end__"],
name="CustomAfterModel",
)
- def custom_after_model(state: CustomState) -> dict[str, Any]:
+ def custom_after_model(state: CustomState, runtime: Runtime) -> dict[str, Any]:
return {"jump_to": "model"}
# Verify all options were applied
@@ -70,7 +72,9 @@ def test_after_model_decorator() -> None:
assert custom_after_model.__class__.__name__ == "CustomAfterModel"
# Verify it works
- result = custom_after_model.after_model({"messages": [HumanMessage("Hello"), AIMessage("Hi!")]})
+ result = custom_after_model.after_model(
+ {"messages": [HumanMessage("Hello"), AIMessage("Hi!")]}, None
+ )
assert result == {"jump_to": "model"}
@@ -78,7 +82,9 @@ def test_modify_model_request_decorator() -> None:
"""Test modify_model_request decorator with all configuration options."""
@modify_model_request(state_schema=CustomState, tools=[test_tool], name="CustomModifyRequest")
- def custom_modify_request(request: ModelRequest, state: CustomState) -> ModelRequest:
+ def custom_modify_request(
+ request: ModelRequest, state: CustomState, runtime: Runtime
+ ) -> ModelRequest:
request.system_prompt = "Modified"
return request
@@ -98,7 +104,7 @@ def test_modify_model_request_decorator() -> None:
response_format=None,
)
result = custom_modify_request.modify_model_request(
- original_request, {"messages": [HumanMessage("Hello")]}
+ original_request, {"messages": [HumanMessage("Hello")]}, None
)
assert result.system_prompt == "Modified"
@@ -108,17 +114,17 @@ def test_all_decorators_integration() -> None:
call_order = []
@before_model
- def track_before(state: AgentState) -> None:
+ def track_before(state: AgentState, runtime: Runtime) -> None:
call_order.append("before")
return None
@modify_model_request
- def track_modify(request: ModelRequest, state: AgentState) -> ModelRequest:
+ def track_modify(request: ModelRequest, state: AgentState, runtime: Runtime) -> ModelRequest:
call_order.append("modify")
return request
@after_model
- def track_after(state: AgentState) -> None:
+ def track_after(state: AgentState, runtime: Runtime) -> None:
call_order.append("after")
return None
@@ -135,18 +141,186 @@ def test_decorators_use_function_names_as_default() -> None:
"""Test that decorators use function names as default middleware names."""
@before_model
- def my_before_hook(state: AgentState) -> None:
+ def my_before_hook(state: AgentState, runtime: Runtime) -> None:
return None
@modify_model_request
- def my_modify_hook(request: ModelRequest, state: AgentState) -> ModelRequest:
+ def my_modify_hook(request: ModelRequest, state: AgentState, runtime: Runtime) -> ModelRequest:
return request
@after_model
- def my_after_hook(state: AgentState) -> None:
+ def my_after_hook(state: AgentState, runtime: Runtime) -> None:
return None
# Verify that function names are used as middleware class names
assert my_before_hook.__class__.__name__ == "my_before_hook"
assert my_modify_hook.__class__.__name__ == "my_modify_hook"
assert my_after_hook.__class__.__name__ == "my_after_hook"
+
+
+# Async Decorator Tests
+
+
+def test_async_before_model_decorator() -> None:
+ """Test before_model decorator with async function."""
+
+ @before_model(state_schema=CustomState, tools=[test_tool], name="AsyncBeforeModel")
+ async def async_before_model(state: CustomState, runtime: Runtime) -> dict[str, Any]:
+ return {"custom_field": "async_value"}
+
+ assert isinstance(async_before_model, AgentMiddleware)
+ assert async_before_model.state_schema == CustomState
+ assert async_before_model.tools == [test_tool]
+ assert async_before_model.__class__.__name__ == "AsyncBeforeModel"
+
+
+def test_async_after_model_decorator() -> None:
+ """Test after_model decorator with async function."""
+
+ @after_model(state_schema=CustomState, tools=[test_tool], name="AsyncAfterModel")
+ async def async_after_model(state: CustomState, runtime: Runtime) -> dict[str, Any]:
+ return {"custom_field": "async_value"}
+
+ assert isinstance(async_after_model, AgentMiddleware)
+ assert async_after_model.state_schema == CustomState
+ assert async_after_model.tools == [test_tool]
+ assert async_after_model.__class__.__name__ == "AsyncAfterModel"
+
+
+def test_async_modify_model_request_decorator() -> None:
+ """Test modify_model_request decorator with async function."""
+
+ @modify_model_request(state_schema=CustomState, tools=[test_tool], name="AsyncModifyRequest")
+ async def async_modify_request(
+ request: ModelRequest, state: CustomState, runtime: Runtime
+ ) -> ModelRequest:
+ request.system_prompt = "Modified async"
+ return request
+
+ assert isinstance(async_modify_request, AgentMiddleware)
+ assert async_modify_request.state_schema == CustomState
+ assert async_modify_request.tools == [test_tool]
+ assert async_modify_request.__class__.__name__ == "AsyncModifyRequest"
+
+
+def test_mixed_sync_async_decorators() -> None:
+ """Test decorators with both sync and async functions."""
+
+ @before_model(name="MixedBeforeModel")
+ def sync_before(state: AgentState, runtime: Runtime) -> None:
+ return None
+
+ @before_model(name="MixedBeforeModel")
+ async def async_before(state: AgentState, runtime: Runtime) -> None:
+ return None
+
+ @modify_model_request(name="MixedModifyRequest")
+ def sync_modify(request: ModelRequest, state: AgentState, runtime: Runtime) -> ModelRequest:
+ return request
+
+ @modify_model_request(name="MixedModifyRequest")
+ async def async_modify(
+ request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
+ return request
+
+ # Both should create valid middleware instances
+ assert isinstance(sync_before, AgentMiddleware)
+ assert isinstance(async_before, AgentMiddleware)
+ assert isinstance(sync_modify, AgentMiddleware)
+ assert isinstance(async_modify, AgentMiddleware)
+
+
+async def test_async_decorators_integration() -> None:
+ """Test async decorators working together in an agent."""
+ call_order = []
+
+ @before_model
+ async def track_async_before(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("async_before")
+ return None
+
+ @modify_model_request
+ async def track_async_modify(
+ request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
+ call_order.append("async_modify")
+ return request
+
+ @after_model
+ async def track_async_after(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("async_after")
+ return None
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ middleware=[track_async_before, track_async_modify, track_async_after],
+ )
+ agent = agent.compile()
+ await agent.ainvoke({"messages": [HumanMessage("Hello")]})
+
+ assert call_order == ["async_before", "async_modify", "async_after"]
+
+
+async def test_mixed_sync_async_decorators_integration() -> None:
+ """Test mixed sync/async decorators working together in an agent."""
+ call_order = []
+
+ @before_model
+ def track_sync_before(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("sync_before")
+ return None
+
+ @before_model
+ async def track_async_before(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("async_before")
+ return None
+
+ @modify_model_request
+ def track_sync_modify(
+ request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
+ call_order.append("sync_modify")
+ return request
+
+ @modify_model_request
+ async def track_async_modify(
+ request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
+ call_order.append("async_modify")
+ return request
+
+ @after_model
+ async def track_async_after(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("async_after")
+ return None
+
+ @after_model
+ def track_sync_after(state: AgentState, runtime: Runtime) -> None:
+ call_order.append("sync_after")
+ return None
+
+ agent = create_agent(
+ model=FakeToolCallingModel(),
+ middleware=[
+ track_sync_before,
+ track_async_before,
+ track_sync_modify,
+ track_async_modify,
+ track_async_after,
+ track_sync_after,
+ ],
+ )
+ agent = agent.compile()
+ await agent.ainvoke({"messages": [HumanMessage("Hello")]})
+
+ assert call_order == [
+ "sync_before",
+ "async_before",
+ "sync_modify",
+ "async_modify",
+ "sync_after",
+ "async_after",
+ ]
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
index 8db208a0b3..e22a9ad222 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
@@ -7,6 +7,7 @@ from langchain.agents.middleware_agent import create_agent
from langchain_core.messages import HumanMessage, ToolMessage
from langchain_core.tools import tool
from .model import FakeToolCallingModel
+from langgraph.runtime import Runtime
def test_model_request_tools_are_strings() -> None:
@@ -24,7 +25,9 @@ def test_model_request_tools_are_strings() -> None:
return f"Result: {expression}"
class RequestCapturingMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
captured_requests.append(request)
return request
@@ -67,7 +70,9 @@ def test_middleware_can_modify_tool_names() -> None:
return "C"
class ToolFilteringMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
# Only allow tool_a and tool_b
request.tools = ["tool_a", "tool_b"]
return request
@@ -102,7 +107,9 @@ def test_unknown_tool_name_raises_error() -> None:
return "result"
class BadMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
# Add an unknown tool name
request.tools = ["known_tool", "unknown_tool"]
return request
@@ -137,7 +144,9 @@ def test_middleware_can_add_and_remove_tools() -> None:
class ConditionalToolMiddleware(AgentMiddleware[AdminState]):
state_schema = AdminState
- def modify_model_request(self, request: ModelRequest, state: AdminState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AdminState, runtime: Runtime
+ ) -> ModelRequest:
# Remove admin_tool if not admin
if not state.get("is_admin", False):
request.tools = [name for name in request.tools if name != "admin_tool"]
@@ -171,7 +180,9 @@ def test_empty_tools_list_is_valid() -> None:
return "result"
class NoToolsMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
# Remove all tools
request.tools = []
return request
@@ -210,14 +221,18 @@ def test_tools_preserved_across_multiple_middleware() -> None:
return "C"
class FirstMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
modification_order.append(request.tools.copy())
# Remove tool_c
request.tools = [name for name in request.tools if name != "tool_c"]
return request
class SecondMiddleware(AgentMiddleware):
- def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest:
+ def modify_model_request(
+ self, request: ModelRequest, state: AgentState, runtime: Runtime
+ ) -> ModelRequest:
modification_order.append(request.tools.copy())
# Should not see tool_c here
assert "tool_c" not in request.tools