Skip to content

Commit 91fd7f1

Browse files
RingoIngo2Ingo Guehring
andauthored
feat: add debug logging for model converse requests (#297)
Co-authored-by: Ingo Guehring <ingogue@amazon.com>
1 parent 13d7b81 commit 91fd7f1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/strands/types/models/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def converse(
130130
"""
131131
logger.debug("formatting request")
132132
request = self.format_request(messages, tool_specs, system_prompt)
133+
logger.debug("formatted request=<%s>", request)
133134

134135
logger.debug("invoking model")
135136
response = self.stream(request)

tests/strands/types/models/test_model.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,32 @@ def test_structured_output(model):
9696
response = model.structured_output(Person)
9797

9898
assert response == Person(name="test", age=20)
99+
100+
101+
def test_converse_logging(model, messages, tool_specs, system_prompt, caplog):
102+
"""Test that converse method logs the formatted request at debug level."""
103+
import logging
104+
105+
# Set the logger to debug level to capture debug messages
106+
caplog.set_level(logging.DEBUG, logger="strands.types.models.model")
107+
108+
# Execute the converse method
109+
response = model.converse(messages, tool_specs, system_prompt)
110+
list(response) # Consume the generator to trigger all logging
111+
112+
# Check that the expected log messages are present
113+
assert "formatting request" in caplog.text
114+
assert "formatted request=" in caplog.text
115+
assert "invoking model" in caplog.text
116+
assert "got response from model" in caplog.text
117+
assert "finished streaming response from model" in caplog.text
118+
119+
# Check that the formatted request is logged with the expected content
120+
expected_request_str = str(
121+
{
122+
"messages": messages,
123+
"tool_specs": tool_specs,
124+
"system_prompt": system_prompt,
125+
}
126+
)
127+
assert expected_request_str in caplog.text

0 commit comments

Comments
 (0)