fix(streaming): log warning when tool input JSON is malformed#2054
fix(streaming): log warning when tool input JSON is malformed#2054Ratansairohith wants to merge 2 commits intostrands-agents:mainfrom
Conversation
Malformed tool input JSON was silently replaced with an empty dict, with no logging. The tool then ran with empty arguments and the model had no indication its JSON was invalid. Added a logger.warning() call before the fallback so operators can detect and diagnose this in production. Fixes strands-agents#2051
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| logger.warning( | ||
| "Failed to parse tool input JSON for '%s': %s", | ||
| current_tool_use.get("name", "unknown"), | ||
| current_tool_use["input"][:200] if isinstance(current_tool_use.get("input"), str) else "", |
There was a problem hiding this comment.
Issue: The log message doesn't follow the project's structured logging format defined in AGENTS.md.
The format requires field=<value> pairs separated by commas, followed by a pipe | and a lowercase human-readable message. Compare with the existing warning on line 349 in this same file:
logger.warning(
"original_stop_reason=<%s>, new_stop_reason=<%s> | "
"overriding stop reason due to toolUse blocks in response",
"end_turn",
"tool_use",
)Suggestion:
logger.warning(
"tool_name=<%s>, raw_input=<%s> | failed to parse tool input json, defaulting to empty dict",
current_tool_use.get("name", "unknown"),
current_tool_use["input"][:200] if isinstance(current_tool_use.get("input"), str) else "",
)| "redactedContent": b"", | ||
| }, | ||
| ), | ||
| # Tool Use - Malformed input JSON |
There was a problem hiding this comment.
Issue: The new parametrized test case verifies the state transformation (malformed JSON → {}) but doesn't assert that logger.warning was actually called. Since the purpose of this PR is to add the warning log, the test should verify it.
Suggestion: Add a dedicated test that uses unittest.mock.patch to assert the warning is emitted with the expected arguments:
@unittest.mock.patch("strands.event_loop.streaming.logger")
def test_handle_content_block_stop_logs_warning_on_malformed_json(mock_logger):
state = {
"content": [],
"current_tool_use": {"toolUseId": "123", "name": "test_tool", "input": "{invalid json}"},
"text": "",
"reasoningText": "",
"citationsContent": [],
"redactedContent": b"",
}
strands.event_loop.streaming.handle_content_block_stop(state)
mock_logger.warning.assert_called_once()
call_args = mock_logger.warning.call_args
assert "test_tool" in str(call_args)
assert "{invalid json}" in str(call_args)|
Assessment: Comment Good improvement for observability — logging malformed JSON will help operators debug tool invocation failures, especially with smaller models. Review Details
Clean, small, well-scoped change — just the two items above to address. |
- 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 strands-agents#2054.
|
@mkmeral thanks for the approval! Addressed the auto-review feedback in 009fba5:
The |
Fixes #2051
Changes
src/strands/event_loop/streaming.py:logger.warning()in theexcept ValueErrorblock (line 282) that logs the tool name and raw input string when JSON parsing fails. The fallback to{}is preserved.tests/strands/event_loop/test_streaming.py:Test plan