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 1 commit
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": | ||||||||||||||
| tool_result_content_blocks.append( | ||||||||||||||
| BedrockToolResultContentBlock(document=content) # Assume it matches Bedrock structure or Anthropic fallback | ||||||||||||||
| ) | ||||||||||||||
| 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": | ||||||||||||||
| tool_result_content_blocks.append( | ||||||||||||||
| BedrockToolResultContentBlock(document=content) # Assume it matches Bedrock structure or Anthropic fallback | ||||||||||||||
| ) | ||||||||||||||
| elif content["type"] == "image_url": | ||||||||||||||
| format: Optional[str] = None | ||||||||||||||
| if isinstance(content["image_url"], dict): | ||||||||||||||
|
|
@@ -4423,6 +4431,9 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915 | |||||||||||||
| guardContent={"text": {"text": element["text"]}} | ||||||||||||||
| ) | ||||||||||||||
| _parts.append(_part) | ||||||||||||||
| elif element["type"] == "document": | ||||||||||||||
| _part = BedrockContentBlock(document=element) # Pass it through since element already has format, name, source | ||||||||||||||
| _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 +4603,9 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915 | |||||||||||||
| text=element["text"] | ||||||||||||||
| ) | ||||||||||||||
| assistants_parts.append(assistants_part) | ||||||||||||||
| elif element["type"] == "document": | ||||||||||||||
| _part = BedrockContentBlock(document=element) # Pass it through since element already has format, name, source | ||||||||||||||
| _parts.append(_part) | ||||||||||||||
|
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.
In this assistant content block, all other content types append to
Suggested change
|
||||||||||||||
| 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:tool_result_content_blocksis not defined in this scopeconvert_to_anthropic_tool_resultis an Anthropic-specific function — it builds anAnthropicMessagesToolResultParam, not a Bedrock structure. There is notool_result_content_blocksvariable defined in this function, so this code will raiseNameError: name 'tool_result_content_blocks' is not definedat runtime whenever a tool message contains adocumentcontent block.The document should instead be appended to
anthropic_content_listin the Anthropic-compatible format, not routed to a Bedrock type.