Skip to content

Commit 53ebaa0

Browse files
committed
fix: Add local method override
1 parent 04c8547 commit 53ebaa0

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/strands/agent/agent.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ def __getattr__(self, name: str) -> Callable[..., Any]:
100100
AttributeError: If no tool with the given name exists or if multiple tools match the given name.
101101
"""
102102

103-
def caller(user_message_override: Optional[str] = None, **kwargs: Any) -> Any:
103+
def caller(
104+
user_message_override: Optional[str] = None,
105+
record_direct_tool_call: Optional[bool] = None,
106+
**kwargs: Any,
107+
) -> Any:
104108
"""Call a tool directly by name.
105109
106110
Args:
107111
user_message_override: Optional custom message to record instead of default
112+
record_direct_tool_call: Whether to record direct tool calls in message history. Overrides class
113+
attribute if provided.
108114
**kwargs: Keyword arguments to pass to the tool.
109115
110116
Returns:
@@ -134,7 +140,12 @@ def caller(user_message_override: Optional[str] = None, **kwargs: Any) -> Any:
134140
kwargs=kwargs,
135141
)
136142

137-
if self._agent.record_direct_tool_call:
143+
if record_direct_tool_call is not None:
144+
should_record_direct_tool_call = record_direct_tool_call
145+
else:
146+
should_record_direct_tool_call = self._agent.record_direct_tool_call
147+
148+
if should_record_direct_tool_call:
138149
# Create a record of this tool execution in the message history
139150
self._agent._record_tool_execution(
140151
tool_use, tool_result, user_message_override, self._agent.messages

tests/strands/agent/test_agent.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,18 @@ def test_agent_tool_do_not_record_tool(agent):
739739
assert tru_messages == exp_messages
740740

741741

742+
def test_agent_tool_do_not_record_tool_with_method_override(agent):
743+
agent.record_direct_tool_call = True
744+
agent.tool.tool_decorated(
745+
random_string="abcdEfghI123", user_message_override="test override", record_direct_tool_call=False
746+
)
747+
748+
tru_messages = agent.messages
749+
exp_messages = []
750+
751+
assert tru_messages == exp_messages
752+
753+
742754
def test_agent_tool_tool_does_not_exist(agent):
743755
with pytest.raises(AttributeError):
744756
agent.tool.does_not_exist()

0 commit comments

Comments
 (0)