Skip to content

Commit da921f8

Browse files
zastrowmdbschmigelski
authored andcommitted
fix: Bedrock hang when exception occurs during message conversion (strands-agents#643)
Previously (strands-agents#642) bedrock would hang during message conversion because the exception was not being caught and thus the queue was always empty. Now all exceptions during conversion are caught Co-authored-by: Mackenzie Zastrow <[email protected]>
1 parent 45c94c5 commit da921f8

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ test-integ = [
234234
"hatch test tests_integ {args}"
235235
]
236236
prepare = [
237-
"hatch fmt --linter",
238237
"hatch fmt --formatter",
238+
"hatch fmt --linter",
239239
"hatch run test-lint",
240240
"hatch test --all"
241241
]

src/strands/models/bedrock.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ def _stream(
422422
ContextWindowOverflowException: If the input exceeds the model's context window.
423423
ModelThrottledException: If the model service is throttling requests.
424424
"""
425-
logger.debug("formatting request")
426-
request = self.format_request(messages, tool_specs, system_prompt)
427-
logger.debug("request=<%s>", request)
425+
try:
426+
logger.debug("formatting request")
427+
request = self.format_request(messages, tool_specs, system_prompt)
428+
logger.debug("request=<%s>", request)
428429

429-
logger.debug("invoking model")
430-
streaming = self.config.get("streaming", True)
430+
logger.debug("invoking model")
431+
streaming = self.config.get("streaming", True)
431432

432-
try:
433433
logger.debug("got response from model")
434434
if streaming:
435435
response = self.client.converse_stream(**request)

tests/strands/models/test_bedrock.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ async def test_stream_throttling_exception_from_event_stream_error(bedrock_clien
419419
)
420420

421421

422+
@pytest.mark.asyncio
423+
async def test_stream_with_invalid_content_throws(bedrock_client, model, alist):
424+
# We used to hang on None, so ensure we don't regress: https://github.com/strands-agents/sdk-python/issues/642
425+
messages = [{"role": "user", "content": None}]
426+
427+
with pytest.raises(TypeError):
428+
await alist(model.stream(messages))
429+
430+
422431
@pytest.mark.asyncio
423432
async def test_stream_throttling_exception_from_general_exception(bedrock_client, model, messages, alist):
424433
error_message = "ThrottlingException: Rate exceeded for ConverseStream"

0 commit comments

Comments
 (0)