Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__*
.pytest_cache
.ruff_cache
*.bak
.vscode
12 changes: 8 additions & 4 deletions src/strands/handlers/callback_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ def __call__(self, **kwargs: Any) -> None:

Args:
**kwargs: Callback event data including:

- data (str): Text content to stream.
- complete (bool): Whether this is the final chunk of a response.
- current_tool_use (dict): Information about the current tool being used.
- reasoningText (Optional[str]): Reasoning text to print if provided.
- data (str): Text content to stream.
- complete (bool): Whether this is the final chunk of a response.
- current_tool_use (dict): Information about the current tool being used.
"""
reasoningText = kwargs.get("reasoningText", False)
data = kwargs.get("data", "")
complete = kwargs.get("complete", False)
current_tool_use = kwargs.get("current_tool_use", {})

if reasoningText:
print(reasoningText, end="")

if data:
print(data, end="" if not complete else "\n")

Expand Down
25 changes: 25 additions & 0 deletions tests/strands/handlers/test_callback_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ def test_call_with_empty_args(handler, mock_print):
mock_print.assert_not_called()


def test_call_handler_reasoningText(handler, mock_print):
"""Test calling the handler with reasoningText."""
handler(reasoningText="This is reasoning text")
# Should print reasoning text without newline
mock_print.assert_called_once_with("This is reasoning text", end="")


def test_call_without_reasoningText(handler, mock_print):
"""Test calling the handler without reasoningText argument."""
handler(data="Some output")
# Should only print data, not reasoningText
mock_print.assert_called_once_with("Some output", end="")


def test_call_with_reasoningText_and_data(handler, mock_print):
"""Test calling the handler with both reasoningText and data."""
handler(reasoningText="Reasoning", data="Output")
# Should print reasoningText and data, both without newline
calls = [
unittest.mock.call("Reasoning", end=""),
unittest.mock.call("Output", end=""),
]
mock_print.assert_has_calls(calls)


def test_call_with_data_incomplete(handler, mock_print):
"""Test calling the handler with data but not complete."""
handler(data="Test output")
Expand Down