From 1f033b3e01e6c67e56186be5de5deb110ec4e610 Mon Sep 17 00:00:00 2001 From: Ingo Guehring Date: Thu, 26 Jun 2025 20:00:35 +0000 Subject: [PATCH] feat: add debug logging for model converse requests --- src/strands/types/models/model.py | 1 + tests/strands/types/models/test_model.py | 29 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/strands/types/models/model.py b/src/strands/types/models/model.py index 0a289cf53..8ce577db9 100644 --- a/src/strands/types/models/model.py +++ b/src/strands/types/models/model.py @@ -130,6 +130,7 @@ def converse( """ logger.debug("formatting request") request = self.format_request(messages, tool_specs, system_prompt) + logger.debug("formatted request=<%s>", request) logger.debug("invoking model") response = self.stream(request) diff --git a/tests/strands/types/models/test_model.py b/tests/strands/types/models/test_model.py index 03690733a..dddb763d0 100644 --- a/tests/strands/types/models/test_model.py +++ b/tests/strands/types/models/test_model.py @@ -96,3 +96,32 @@ def test_structured_output(model): response = model.structured_output(Person) assert response == Person(name="test", age=20) + + +def test_converse_logging(model, messages, tool_specs, system_prompt, caplog): + """Test that converse method logs the formatted request at debug level.""" + import logging + + # Set the logger to debug level to capture debug messages + caplog.set_level(logging.DEBUG, logger="strands.types.models.model") + + # Execute the converse method + response = model.converse(messages, tool_specs, system_prompt) + list(response) # Consume the generator to trigger all logging + + # Check that the expected log messages are present + assert "formatting request" in caplog.text + assert "formatted request=" in caplog.text + assert "invoking model" in caplog.text + assert "got response from model" in caplog.text + assert "finished streaming response from model" in caplog.text + + # Check that the formatted request is logged with the expected content + expected_request_str = str( + { + "messages": messages, + "tool_specs": tool_specs, + "system_prompt": system_prompt, + } + ) + assert expected_request_str in caplog.text