Skip to content

The agent engineering platform.

chore(langchain_v1): stricter handling of sync vs. async for wrap_model_call and wrap_tool_call (#33429)

Eugene Yurtsev6dc34beb7117
diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.py
index e009b2e3ab..a373774538 100644
--- a/libs/langchain_v1/langchain/agents/factory.py
+++ b/libs/langchain_v1/langchain/agents/factory.py
@@ -725,13 +725,23 @@ def create_agent( # noqa: PLR0915
if m.__class__.after_agent is not AgentMiddleware.after_agent
or m.__class__.aafter_agent is not AgentMiddleware.aafter_agent
]
+ # Collect middleware with wrap_model_call or awrap_model_call hooks
+ # Include middleware with either implementation to ensure NotImplementedError is raised
+ # when middleware doesn't support the execution path
middleware_w_wrap_model_call = [
- m for m in middleware if m.__class__.wrap_model_call is not AgentMiddleware.wrap_model_call
+ m
+ for m in middleware
+ if m.__class__.wrap_model_call is not AgentMiddleware.wrap_model_call
+ or m.__class__.awrap_model_call is not AgentMiddleware.awrap_model_call
]
+ # Collect middleware with awrap_model_call or wrap_model_call hooks
+ # Include middleware with either implementation to ensure NotImplementedError is raised
+ # when middleware doesn't support the execution path
middleware_w_awrap_model_call = [
m
for m in middleware
if m.__class__.awrap_model_call is not AgentMiddleware.awrap_model_call
+ or m.__class__.wrap_model_call is not AgentMiddleware.wrap_model_call
]
# Compose wrap_model_call handlers into a single middleware stack (sync)
diff --git a/libs/langchain_v1/langchain/agents/middleware/types.py b/libs/langchain_v1/langchain/agents/middleware/types.py
index 0b8bcdf50f..91072ea7ea 100644
--- a/libs/langchain_v1/langchain/agents/middleware/types.py
+++ b/libs/langchain_v1/langchain/agents/middleware/types.py
@@ -1150,6 +1150,16 @@ def dynamic_prompt(
request.system_prompt = prompt
return handler(request)
+ async def async_wrapped_from_sync(
+ self: AgentMiddleware[StateT, ContextT], # noqa: ARG001
+ request: ModelRequest,
+ handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
+ ) -> ModelCallResult:
+ # Delegate to sync function
+ prompt = cast("str", func(request))
+ request.system_prompt = prompt
+ return await handler(request)
+
middleware_name = cast("str", getattr(func, "__name__", "DynamicPromptMiddleware"))
return type(
@@ -1159,6 +1169,7 @@ def dynamic_prompt(
"state_schema": AgentState,
"tools": [],
"wrap_model_call": wrapped,
+ "awrap_model_call": async_wrapped_from_sync,
},
)()
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 a385cc161c..888049923a 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
@@ -54,6 +54,8 @@ from langchain.agents.middleware.types import (
OmitFromInput,
OmitFromOutput,
PrivateStateAttr,
+ ModelResponse,
+ ModelCallResult,
)
from langchain.agents.factory import create_agent
from langchain.agents.structured_output import ToolStrategy
@@ -2196,20 +2198,28 @@ async def test_create_agent_mixed_sync_async_middleware() -> None:
"""Test async invoke with mixed sync and async middleware."""
calls = []
- class SyncMiddleware(AgentMiddleware):
+ class MostlySyncMiddleware(AgentMiddleware):
def before_model(self, state, runtime) -> None:
- calls.append("SyncMiddleware.before_model")
+ calls.append("MostlySyncMiddleware.before_model")
def wrap_model_call(
self,
request: ModelRequest,
handler: Callable[[ModelRequest], AIMessage],
) -> AIMessage:
- calls.append("SyncMiddleware.wrap_model_call")
+ calls.append("MostlySyncMiddleware.wrap_model_call")
return handler(request)
+ async def awrap_model_call(
+ self,
+ request: ModelRequest,
+ handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
+ ) -> ModelCallResult:
+ calls.append("MostlySyncMiddleware.awrap_model_call")
+ return await handler(request)
+
def after_model(self, state, runtime) -> None:
- calls.append("SyncMiddleware.after_model")
+ calls.append("MostlySyncMiddleware.after_model")
class AsyncMiddleware(AgentMiddleware):
async def abefore_model(self, state, runtime) -> None:
@@ -2230,20 +2240,21 @@ async def test_create_agent_mixed_sync_async_middleware() -> None:
model=FakeToolCallingModel(),
tools=[],
system_prompt="You are a helpful assistant.",
- middleware=[SyncMiddleware(), AsyncMiddleware()],
+ middleware=[MostlySyncMiddleware(), AsyncMiddleware()],
)
- result = await agent.ainvoke({"messages": [HumanMessage("hello")]})
+ await agent.ainvoke({"messages": [HumanMessage("hello")]})
# In async mode, both sync and async middleware should work
# Note: Sync wrap_model_call is not called when running in async mode,
# as the async version is preferred
assert calls == [
- "SyncMiddleware.before_model",
+ "MostlySyncMiddleware.before_model",
"AsyncMiddleware.abefore_model",
+ "MostlySyncMiddleware.awrap_model_call",
"AsyncMiddleware.awrap_model_call",
"AsyncMiddleware.aafter_model",
- "SyncMiddleware.after_model",
+ "MostlySyncMiddleware.after_model",
]
@@ -2638,10 +2649,8 @@ def test_create_agent_sync_invoke_with_only_async_middleware_raises_error() -> N
middleware=[AsyncOnlyMiddleware()],
)
- # This should work now via run_in_executor
- result = agent.invoke({"messages": [HumanMessage("hello")]})
- assert result is not None
- assert "messages" in result
+ with pytest.raises(NotImplementedError):
+ agent.invoke({"messages": [HumanMessage("hello")]})
def test_create_agent_sync_invoke_with_mixed_middleware() -> None:
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 7663616bbc..b8969bcaa2 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
@@ -18,6 +18,7 @@ from langchain.agents.middleware.types import (
after_model,
dynamic_prompt,
wrap_model_call,
+ wrap_tool_call,
hook_config,
)
from langchain.agents.factory import create_agent, _get_can_jump_to
@@ -356,16 +357,16 @@ async def test_mixed_sync_async_decorators_integration() -> None:
call_order.append("async_before")
return None
- @wrap_model_call
- def track_sync_on_call(request, handler):
- call_order.append("sync_on_call")
- return handler(request)
-
@wrap_model_call
async def track_async_on_call(request, handler):
call_order.append("async_on_call")
return await handler(request)
+ @wrap_tool_call
+ async def track_sync_on_tool_call(request, handler):
+ call_order.append("async_on_tool_call")
+ return await handler(request)
+
@after_model
async def track_async_after(state: AgentState, runtime: Runtime) -> None:
call_order.append("async_after")
@@ -381,8 +382,8 @@ async def test_mixed_sync_async_decorators_integration() -> None:
middleware=[
track_sync_before,
track_async_before,
- track_sync_on_call,
track_async_on_call,
+ track_sync_on_tool_call,
track_async_after,
track_sync_after,
],
@@ -390,8 +391,9 @@ async def test_mixed_sync_async_decorators_integration() -> None:
# Agent is already compiled
await agent.ainvoke({"messages": [HumanMessage("Hello")]})
- # In async mode, only async wrap_model_call is invoked (not sync)
- # Sync before_model and after_model are still called via run_in_executor
+ # In async mode, we can automatically delegate to sync middleware for nodes
+ # (although we cannot delegate to sync middleware for model call or tool call)
+
assert call_order == [
"sync_before",
"async_before",
@@ -714,3 +716,51 @@ def test_dynamic_prompt_multiple_in_sequence() -> None:
result = agent.invoke({"messages": [HumanMessage("Hello")]})
assert result["messages"][-1].content == "Second prompt.-Hello"
+
+
+def test_async_dynamic_prompt_skipped_on_sync_invoke() -> None:
+ """Test that async dynamic_prompt raises NotImplementedError when invoked via sync path (.invoke).
+
+ When an async-only middleware is defined, it cannot be called from the sync path.
+ The framework will raise NotImplementedError when trying to invoke the sync method.
+ """
+ calls = []
+
+ @dynamic_prompt
+ async def async_only_prompt(request: ModelRequest) -> str:
+ calls.append("async_prompt")
+ return "Async prompt"
+
+ agent = create_agent(model=FakeToolCallingModel(), middleware=[async_only_prompt])
+
+ # Async-only middleware raises NotImplementedError in sync path
+ with pytest.raises(NotImplementedError):
+ agent.invoke({"messages": [HumanMessage("Hello")]})
+
+ # The async prompt was not called
+ assert calls == []
+
+
+async def test_sync_dynamic_prompt_on_async_invoke() -> None:
+ """Test that sync dynamic_prompt works when invoked via async path (.ainvoke).
+
+ When a sync middleware is defined with @dynamic_prompt, it automatically creates
+ both sync and async implementations. The async implementation delegates to the
+ sync function, allowing the middleware to work in both sync and async contexts.
+ """
+ calls = []
+
+ @dynamic_prompt
+ def sync_prompt(request: ModelRequest) -> str:
+ calls.append("sync_prompt")
+ return "Sync prompt"
+
+ agent = create_agent(model=FakeToolCallingModel(), middleware=[sync_prompt])
+
+ # Sync dynamic_prompt now works in async path via delegation
+ result = await agent.ainvoke({"messages": [HumanMessage("Hello")]})
+
+ # The sync prompt function was called via async delegation
+ assert calls == ["sync_prompt"]
+ # The model executed with the custom prompt
+ assert result["messages"][-1].content == "Sync prompt-Hello"