Repository navigation
fix(bedrock): preserve document content blocks for Converse API (#24641) #24646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
23e1edd
fc4a67f
2052bed
b61d28f
be5d213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1726,6 +1726,10 @@ def convert_to_anthropic_tool_result( | |
| if cache_control_value is not None: | ||
| text_content["cache_control"] = cache_control_value | ||
| anthropic_content_list.append(text_content) | ||
| elif content["type"] == "document": | ||
| # Remove 'type' key from content dict to pass to Anthropic / Bedrock block | ||
| doc_content = {k: v for k, v in content.items() if k != "type"} | ||
| anthropic_content_list.append(doc_content) | ||
| elif content["type"] == "image_url": | ||
| format = ( | ||
| content["image_url"].get("format") | ||
|
|
@@ -3969,6 +3973,10 @@ def _convert_to_bedrock_tool_call_result( | |
| tool_result_content_blocks.append( | ||
| BedrockToolResultContentBlock(text=content["text"]) | ||
| ) | ||
| elif content["type"] == "document": | ||
| # Remove 'type' key from content dict to pass to Anthropic / Bedrock block | ||
| doc_content = {k: v for k, v in content.items() if k != "type"} | ||
| anthropic_content_list.append(doc_content) | ||
| elif content["type"] == "image_url": | ||
| format: Optional[str] = None | ||
| if isinstance(content["image_url"], dict): | ||
|
|
@@ -4423,6 +4431,10 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915 | |
| guardContent={"text": {"text": element["text"]}} | ||
| ) | ||
| _parts.append(_part) | ||
| elif element["type"] == "document": | ||
| doc_content = {k: v for k, v in element.items() if k != "type"} | ||
| _part = BedrockContentBlock(document=doc_content) | ||
| _parts.append(_part) | ||
|
Comment on lines
+4434
to
+4437
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The raw OpenAI-format content element ( The same issue applies to The # Extract only the fields Bedrock's DocumentBlock expects
_document_block = {
k: v for k, v in element.items() if k != "type"
}
_part = BedrockContentBlock(document=_document_block)
_parts.append(_part)The same fix applies at lines ~3978 and ~1731 where
Comment on lines
4431
to
+4437
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR description claims to fix issue #24641 but no unit tests were added to verify the new
Consider adding at minimum one test for Rule Used: What: Ensure that any PR claiming to fix an issue ... (source) |
||
| elif element["type"] == "image_url": | ||
| format: Optional[str] = None | ||
| if isinstance(element["image_url"], dict): | ||
|
|
@@ -4592,6 +4604,10 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915 | |
| text=element["text"] | ||
| ) | ||
| assistants_parts.append(assistants_part) | ||
| elif element["type"] == "document": | ||
| doc_content = {k: v for k, v in element.items() if k != "type"} | ||
| _part = BedrockContentBlock(document=doc_content) | ||
| _parts.append(_part) | ||
| elif element["type"] == "image_url": | ||
| if isinstance(element["image_url"], dict): | ||
| image_url = element["image_url"]["url"] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NameError:anthropic_content_listis not defined in_convert_to_bedrock_tool_call_resultThis function only initialises
tool_result_content_blocks(line 3964). There is noanthropic_content_listvariable anywhere in_convert_to_bedrock_tool_call_result, so this line will raiseNameError: name 'anthropic_content_list' is not definedat runtime whenever a tool-message content list contains adocumentblock — the exact scenario this PR intends to fix.The document block should be wrapped in a
BedrockToolResultContentBlockand appended totool_result_content_blocks, mirroring thetextandimagebranches immediately above: