Skip to content

Commit 3a734d9

Browse files
committed
fix: Add local method override
1 parent a13c6c2 commit 3a734d9

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
@@ -97,11 +97,17 @@ def __getattr__(self, name: str) -> Callable[..., Any]:
9797
AttributeError: If no tool with the given name exists or if multiple tools match the given name.
9898
"""
9999

100-
def caller(user_message_override: Optional[str] = None, **kwargs: Any) -> Any:
100+
def caller(
101+
user_message_override: Optional[str] = None,
102+
record_direct_tool_call: Optional[bool] = None,
103+
**kwargs: Any,
104+
) -> Any:
101105
"""Call a tool directly by name.
102106
103107
Args:
104108
user_message_override: Optional custom message to record instead of default
109+
record_direct_tool_call: Whether to record direct tool calls in message history. Overrides class
110+
attribute if provided.
105111
**kwargs: Keyword arguments to pass to the tool.
106112
107113
Returns:
@@ -131,7 +137,12 @@ def caller(user_message_override: Optional[str] = None, **kwargs: Any) -> Any:
131137
kwargs=kwargs,
132138
)
133139

134-
if self._agent.record_direct_tool_call:
140+
if record_direct_tool_call is not None:
141+
should_record_direct_tool_call = record_direct_tool_call
142+
else:
143+
should_record_direct_tool_call = self._agent.record_direct_tool_call
144+
145+
if should_record_direct_tool_call:
135146
# Create a record of this tool execution in the message history
136147
self._agent._record_tool_execution(
137148
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
@@ -737,6 +737,18 @@ def test_agent_tool_do_not_record_tool(agent):
737737
assert tru_messages == exp_messages
738738

739739

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

0 commit comments

Comments
 (0)