Skip to content

Commit 2c590e5

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 2c590e5

File tree

1 file changed

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

1 file changed

+34
-26
lines changed

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

Lines changed: 34 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,37 @@ 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(
134+
todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]
135+
) -> Command:
136+
"""Create and manage a structured task list for your current work session."""
137+
return Command(
138+
update={
139+
"todos": todos,
140+
"messages": [
141+
ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)
142+
],
143+
}
144+
)
145+
146+
return write_todos
147+
148+
149+
# Module-level tool with default description
150+
write_todos = _create_write_todos_tool()
127151

128152

129153
class TodoListMiddleware(AgentMiddleware):
@@ -169,23 +193,7 @@ def __init__(
169193
super().__init__()
170194
self.system_prompt = system_prompt
171195
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]
196+
self.tools = [_create_write_todos_tool(self.tool_description)]
189197

190198
def wrap_model_call(
191199
self,

0 commit comments

Comments
 (0)