8000 feat(handlers): add reasoning text to callback handler and related te… · lgigit200/sdk-python@7042af1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7042af1

Browse files
feat(handlers): add reasoning text to callback handler and related tests (strands-agents#109)
* feat(handlers): add reasoning text to callback handler and related tests * feat(handlers): removed redundant comment in .gitignore file * feat(handlers): Updated reasoningText type as (Optional[str]
1 parent 63cef21 commit 7042af1

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ __pycache__*
77
.pytest_cache
88
.ruff_cache
99
*.bak
10+
.vscode

src/strands/handlers/callback_handler.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ def __call__(self, **kwargs: Any) -> None:
1717
1818
Args:
1919
**kwargs: Callback event data including:
20-
21-
- data (str): Text content to stream.
22-
- complete (bool): Whether this is the final chunk of a response.
23-
- current_tool_use (dict): Information about the current tool being used.
20+
- reasoningText (Optional[str]): Reasoning text to print if provided.
21+
- data (str): Text content to stream.
22+
- complete (bool): Whether this is the final chunk of a response.
23+
- current_tool_use (dict): Information about the current tool being used.
2424
"""
25+
reasoningText = kwargs.get("reasoningText", False)
2526
data = kwargs.get("data", "")
2627
complete = kwargs.get("complete", False)
2728
current_tool_use = kwargs.get("current_tool_use", {})
2829

30+
if reasoningText:
31+
print(reasoningText, end="")
32+
2933
if data:
3034
print(data, end="" if not complete else "\n")
3135

tests/strands/handlers/test_callback_handler.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ def test_call_with_empty_args(handler, mock_print):
3030
mock_print.assert_not_called()
3131

3232

33+
def test_call_handler_reasoningText(handler, mock_print):
34+
"""Test calling the handler with reasoningText."""
35+
handler(reasoningText="This is reasoning text")
36+
# Should print reasoning text without newline
37+
mock_print.assert_called_once_with("This is reasoning text", end="")
38+
39+
40+
def test_call_without_reasoningText(handler, mock_print):
41+
"""Test calling the handler without reasoningText argument."""
42+
handler(data="Some output")
43+
# Should only print data, not reasoningText
44+
mock_print.assert_called_once_with("Some output", end="")
45+
46+
47+
def test_call_with_reasoningText_and_data(handler, mock_print):
48+
"""Test calling the handler with both reasoningText and data."""
49+
handler(reasoningText="Reasoning", data="Output")
50+
# Should print reasoningText and data, both without newline
51+
calls = [
52+
unittest.mock.call("Reasoning", end=""),
53+
unittest.mock.call("Output", end=""),
54+
]
55+
mock_print.assert_has_calls(calls)
56+
57+
3358
def test_call_with_data_incomplete(handler, mock_print):
3459
"""Test calling the handler with data but not complete."""
3560
handler(data="Test output")

0 commit comments

Comments
 (0)
0