Skip to content

Commit c7a677b

Browse files
authored
chore(langchain): add async implementation to todolist and test (#36313)
add async func as well
1 parent 0351588 commit c7a677b

File tree

2 files changed

+47
-0
lines changed
  • libs/langchain_v1
    • langchain/agents/middleware
    • tests/unit_tests/agents/middleware/implementations

2 files changed

+47
-0
lines changed

libs/langchain_v1/langchain/agents/middleware/todo.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ def _write_todos(
152152
)
153153

154154

155+
async def _awrite_todos(
156+
runtime: ToolRuntime[ContextT, PlanningState[ResponseT]], todos: list[Todo]
157+
) -> Command[Any]:
158+
"""Create and manage a structured task list for your current work session."""
159+
return _write_todos(runtime, todos)
160+
161+
155162
class TodoListMiddleware(AgentMiddleware[PlanningState[ResponseT], ContextT, ResponseT]):
156163
"""Middleware that provides todo list management capabilities to agents.
157164
@@ -203,6 +210,7 @@ def __init__(
203210
name="write_todos",
204211
description=tool_description,
205212
func=_write_todos,
213+
coroutine=_awrite_todos,
206214
args_schema=WriteTodosInput,
207215
infer_schema=False,
208216
)

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,45 @@ def test_single_write_todos_call_allowed() -> None:
649649
assert result is None
650650

651651

652+
async def test_todo_middleware_agent_creation_with_middleware_async() -> None:
653+
"""Test async agent execution with the planning middleware."""
654+
model = FakeToolCallingModel(
655+
tool_calls=[
656+
[
657+
{
658+
"args": {"todos": [{"content": "Task 1", "status": "pending"}]},
659+
"name": "write_todos",
660+
"type": "tool_call",
661+
"id": "test_call",
662+
}
663+
],
664+
[
665+
{
666+
"args": {"todos": [{"content": "Task 1", "status": "in_progress"}]},
667+
"name": "write_todos",
668+
"type": "tool_call",
669+
"id": "test_call",
670+
}
671+
],
672+
[
673+
{
674+
"args": {"todos": [{"content": "Task 1", "status": "completed"}]},
675+
"name": "write_todos",
676+
"type": "tool_call",
677+
"id": "test_call",
678+
}
679+
],
680+
[],
681+
]
682+
)
683+
middleware = TodoListMiddleware()
684+
agent = create_agent(model=model, middleware=[middleware])
685+
686+
result = await agent.ainvoke({"messages": [HumanMessage("Hello")]})
687+
assert result["todos"] == [{"content": "Task 1", "status": "completed"}]
688+
assert len(result["messages"]) == 8
689+
690+
652691
async def test_parallel_write_todos_calls_rejected_async() -> None:
653692
"""Test async version - parallel write_todos calls are rejected with error messages."""
654693
middleware = TodoListMiddleware()

0 commit comments

Comments
 (0)