From a011b0528c618466e898d0c78d57b9cbdeb951b1 Mon Sep 17 00:00:00 2001 From: Ratish1 Date: Fri, 21 Nov 2025 19:04:20 +0400 Subject: [PATCH 1/2] fix(bedrock): allow text and content in Bedrock document sources --- src/strands/models/bedrock.py | 2 +- src/strands/types/media.py | 8 ++++++-- tests/strands/models/test_bedrock.py | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/strands/models/bedrock.py b/src/strands/models/bedrock.py index 4a7c81672..d4c61a017 100644 --- a/src/strands/models/bedrock.py +++ b/src/strands/models/bedrock.py @@ -398,7 +398,7 @@ def _format_request_message_content(self, content: ContentBlock) -> dict[str, An # Handle source if "source" in document: - result["source"] = {"bytes": document["source"]["bytes"]} + result["source"] = document["source"] # Handle optional fields if "citations" in document and document["citations"] is not None: diff --git a/src/strands/types/media.py b/src/strands/types/media.py index 69cd60cf3..a2b2f2979 100644 --- a/src/strands/types/media.py +++ b/src/strands/types/media.py @@ -5,7 +5,7 @@ - Bedrock docs: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Types_Amazon_Bedrock_Runtime.html """ -from typing import Literal, Optional +from typing import Any, List, Literal, Optional from typing_extensions import TypedDict @@ -15,14 +15,18 @@ """Supported document formats.""" -class DocumentSource(TypedDict): +class DocumentSource(TypedDict, total=False): """Contains the content of a document. Attributes: bytes: The binary content of the document. + text: The text content of the document source. + content: The structured content of the document source. """ bytes: bytes + text: str + content: List[Any] class DocumentContent(TypedDict, total=False): diff --git a/tests/strands/models/test_bedrock.py b/tests/strands/models/test_bedrock.py index 2809e8a72..78e78e92d 100644 --- a/tests/strands/models/test_bedrock.py +++ b/tests/strands/models/test_bedrock.py @@ -19,6 +19,7 @@ DEFAULT_BEDROCK_REGION, DEFAULT_READ_TIMEOUT, ) +from strands.types.content import ContentBlock from strands.types.exceptions import ModelThrottledException from strands.types.tools import ToolSpec @@ -2070,3 +2071,23 @@ async def test_stream_backward_compatibility_system_prompt(bedrock_client, model "system": [{"text": system_prompt}], } bedrock_client.converse_stream.assert_called_once_with(**expected_request) + + +def test_format_request_document_with_text_source(model): + """Test that _format_request_message_content correctly handles a document with a 'text' source.""" + document_text = "This is the document content." + content_block: ContentBlock = { + "document": { + "name": "test_doc", + "source": {"text": document_text}, + "format": "txt", + } + } + + formatted_content = model._format_request_message_content(content_block) + + expected_source = {"text": document_text} + assert "document" in formatted_content + assert "source" in formatted_content["document"] + + assert formatted_content["document"]["source"] == expected_source From 8f77cd8a419061e2af1ccc0f4f1899df33d537e5 Mon Sep 17 00:00:00 2001 From: Ratish1 Date: Fri, 21 Nov 2025 19:22:50 +0400 Subject: [PATCH 2/2] fix(bedrock): allow text and content in Bedrock document sources --- tests/strands/models/test_bedrock.py | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/strands/models/test_bedrock.py b/tests/strands/models/test_bedrock.py index 78e78e92d..ea1ad574e 100644 --- a/tests/strands/models/test_bedrock.py +++ b/tests/strands/models/test_bedrock.py @@ -2086,8 +2086,36 @@ def test_format_request_document_with_text_source(model): formatted_content = model._format_request_message_content(content_block) - expected_source = {"text": document_text} - assert "document" in formatted_content - assert "source" in formatted_content["document"] + assert formatted_content["document"]["source"] == {"text": document_text} - assert formatted_content["document"]["source"] == expected_source + +def test_format_request_document_with_bytes_source(model): + """Test that _format_request_message_content correctly handles a 'bytes' source.""" + + content_block: ContentBlock = { + "document": { + "name": "test_doc", + "source": {"bytes": b"some byte data"}, + "format": "txt", + } + } + + formatted_content = model._format_request_message_content(content_block) + + assert formatted_content["document"]["source"] == {"bytes": b"some byte data"} + + +def test_format_request_document_with_content_source(model): + """Test that _format_request_message_content correctly handles a 'content' source.""" + doc_content = [{"text": "structured content"}] + content_block: ContentBlock = { + "document": { + "name": "test_doc", + "source": {"content": doc_content}, + "format": "txt", + } + } + + formatted_content = model._format_request_message_content(content_block) + + assert formatted_content["document"]["source"] == {"content": doc_content}