Skip to content

Commit b47b675

Browse files
committed
fix: Bedrock hang when exception occurs during message conversion
Previously (#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
1 parent 72709cf commit b47b675

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
@@ -418,14 +418,14 @@ def _stream(
418418
ContextWindowOverflowException: If the input exceeds the model's context window.
419419
ModelThrottledException: If the model service is throttling requests.
420420
"""
421-
logger.debug("formatting request")
422-
request = self.format_request(messages, tool_specs, system_prompt)
423-
logger.debug("request=<%s>", request)
421+
try:
422+
logger.debug("formatting request")
423+
request = self.format_request(messages, tool_specs, system_prompt)
424+
logger.debug("request=<%s>", request)
424425

425-
logger.debug("invoking model")
426-
streaming = self.config.get("streaming", True)
426+
logger.debug("invoking model")
427+
streaming = self.config.get("streaming", True)
427428

428-
try:
429429
logger.debug("got response from model")
430430
if streaming:
431431
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)