Skip to content

Commit a562495

Browse files
LJlkdskdjflsaclaude
andcommitted
refactor(langchain): use factory function for write_todos tool creation
Consolidate duplicate `write_todos` tool definitions into a single factory function `_create_write_todos_tool()`. This eliminates code duplication between the module-level tool and the one created inside `TodoListMiddleware.__init__`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent b8a76cb commit a562495

File tree

1 file changed

+32
-26
lines changed
  • libs/langchain_v1/langchain/agents/middleware

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
if TYPE_CHECKING:
88
from collections.abc import Awaitable, Callable
99

10+
from langchain_core.tools import BaseTool
11+
1012
from langchain_core.messages import SystemMessage, ToolMessage
1113
from langchain_core.tools import tool
1214
from langgraph.types import Command
@@ -115,15 +117,35 @@ class PlanningState(AgentState):
115117
- Don't be afraid to revise the To-Do list as you go. New information may reveal new tasks that need to be done, or old tasks that are irrelevant.""" # noqa: E501
116118

117119

118-
@tool(description=WRITE_TODOS_TOOL_DESCRIPTION)
119-
def write_todos(todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]) -> Command:
120-
"""Create and manage a structured task list for your current work session."""
121-
return Command(
122-
update={
123-
"todos": todos,
124-
"messages": [ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)],
125-
}
126-
)
120+
def _create_write_todos_tool(
121+
description: str = WRITE_TODOS_TOOL_DESCRIPTION,
122+
) -> BaseTool:
123+
"""Factory function to create a write_todos tool with custom description.
124+
125+
Args:
126+
description: The description for the tool.
127+
128+
Returns:
129+
A configured write_todos tool instance.
130+
"""
131+
132+
@tool(description=description)
133+
def write_todos(todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]) -> Command:
134+
"""Create and manage a structured task list for your current work session."""
135+
return Command(
136+
update={
137+
"todos": todos,
138+
"messages": [
139+
ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)
140+
],
141+
}
142+
)
143+
144+
return write_todos
145+
146+
147+
# Module-level tool with default description
148+
write_todos = _create_write_todos_tool()
127149

128150

129151
class TodoListMiddleware(AgentMiddleware):
@@ -169,23 +191,7 @@ def __init__(
169191
super().__init__()
170192
self.system_prompt = system_prompt
171193
self.tool_description = tool_description
172-
173-
# Dynamically create the write_todos tool with the custom description
174-
@tool(description=self.tool_description)
175-
def write_todos(
176-
todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]
177-
) -> Command:
178-
"""Create and manage a structured task list for your current work session."""
179-
return Command(
180-
update={
181-
"todos": todos,
182-
"messages": [
183-
ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)
184-
],
185-
}
186-
)
187-
188-
self.tools = [write_todos]
194+
self.tools = [_create_write_todos_tool(self.tool_description)]
189195

190196
def wrap_model_call(
191197
self,

0 commit comments

Comments
 (0)