Skip to content

Commit 009fba5

Browse files
fix(streaming): apply structured logging format and add warning test
- Switch warning to project's field=<value> | message convention per AGENTS.md - Add dedicated test that asserts logger.warning is emitted with the expected tool_name and raw_input on malformed JSON Address auto-review feedback on #2054.
1 parent 257df32 commit 009fba5

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/strands/event_loop/streaming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def handle_content_block_stop(state: dict[str, Any]) -> dict[str, Any]:
280280
current_tool_use["input"] = json.loads(current_tool_use["input"])
281281
except ValueError:
282282
logger.warning(
283-
"Failed to parse tool input JSON for '%s': %s",
283+
"tool_name=<%s>, raw_input=<%s> | failed to parse tool input json, defaulting to empty dict",
284284
current_tool_use.get("name", "unknown"),
285285
current_tool_use["input"][:200] if isinstance(current_tool_use.get("input"), str) else "",
286286
)

tests/strands/event_loop/test_streaming.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,25 @@ def test_handle_content_block_stop(state, exp_updated_state):
546546
assert tru_updated_state == exp_updated_state
547547

548548

549+
@unittest.mock.patch("strands.event_loop.streaming.logger")
550+
def test_handle_content_block_stop_logs_warning_on_malformed_json(mock_logger):
551+
state = {
552+
"content": [],
553+
"current_tool_use": {"toolUseId": "123", "name": "test_tool", "input": "{invalid json}"},
554+
"text": "",
555+
"reasoningText": "",
556+
"citationsContent": [],
557+
"redactedContent": b"",
558+
}
559+
560+
strands.event_loop.streaming.handle_content_block_stop(state)
561+
562+
mock_logger.warning.assert_called_once()
563+
call_args = mock_logger.warning.call_args
564+
assert "test_tool" in str(call_args)
565+
assert "{invalid json}" in str(call_args)
566+
567+
549568
def test_handle_message_stop():
550569
event: MessageStopEvent = {"stopReason": "end_turn"}
551570

0 commit comments

Comments
 (0)