Skip to content

Commit fb8a861

Browse files
feat(callback_handler): optional verbose output for PrintingCallbackHandler (#1211)
Make the verbose description and counting of tool use optional in PrintingCallbackHandler. --------- Co-authored-by: Dean Schmigelski <[email protected]>
1 parent 432d269 commit fb8a861

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/strands/handlers/callback_handler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
class PrintingCallbackHandler:
88
"""Handler for streaming text output and tool invocations to stdout."""
99

10-
def __init__(self) -> None:
11-
"""Initialize handler."""
10+
def __init__(self, verbose_tool_use: bool = True) -> None:
11+
"""Initialize handler.
12+
13+
Args:
14+
verbose_tool_use: Print out verbose information about tool calls.
15+
"""
1216
self.tool_count = 0
1317
self.previous_tool_use = None
18+
self._verbose_tool_use = verbose_tool_use
1419

1520
def __call__(self, **kwargs: Any) -> None:
1621
"""Stream text output and tool invocations to stdout.
@@ -34,11 +39,12 @@ def __call__(self, **kwargs: Any) -> None:
3439
print(data, end="" if not complete else "\n")
3540

3641
if current_tool_use and current_tool_use.get("name"):
37-
tool_name = current_tool_use.get("name", "Unknown tool")
3842
if self.previous_tool_use != current_tool_use:
3943
self.previous_tool_use = current_tool_use
4044
self.tool_count += 1
41-
print(f"\nTool #{self.tool_count}: {tool_name}")
45+
if self._verbose_tool_use:
46+
tool_name = current_tool_use.get("name", "Unknown tool")
47+
print(f"\nTool #{self.tool_count}: {tool_name}")
4248

4349
if complete and data:
4450
print("\n")

tests/strands/handlers/test_callback_handler.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,25 @@ def test_composite_handler_forwards_to_all_handlers():
202202
# Verify each handler was called with the same arguments
203203
for handler in mock_handlers:
204204
handler.assert_called_once_with(**kwargs)
205+
206+
207+
def test_verbose_tool_use_default():
208+
"""Test that _verbose_tool_use defaults to True."""
209+
handler = PrintingCallbackHandler()
210+
assert handler._verbose_tool_use is True
211+
212+
213+
def test_verbose_tool_use_disabled(mock_print):
214+
"""Test that tool use output is suppressed when verbose_tool_use=False but counting still works."""
215+
handler = PrintingCallbackHandler(verbose_tool_use=False)
216+
assert handler._verbose_tool_use is False
217+
218+
current_tool_use = {"name": "test_tool", "input": {"param": "value"}}
219+
handler(current_tool_use=current_tool_use)
220+
221+
# Should not print tool information when verbose_tool_use is False
222+
mock_print.assert_not_called()
223+
224+
# Should still update tool count and previous_tool_use
225+
assert handler.tool_count == 1
226+
assert handler.previous_tool_use == current_tool_use

0 commit comments

Comments
 (0)