Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions litellm/litellm_core_utils/prompt_templates/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 NameError: tool_result_content_blocks is not defined in this scope

convert_to_anthropic_tool_result is an Anthropic-specific function — it builds an AnthropicMessagesToolResultParam, not a Bedrock structure. There is no tool_result_content_blocks variable defined in this function, so this code will raise NameError: name 'tool_result_content_blocks' is not defined at runtime whenever a tool message contains a document content block.

The document should instead be appended to anthropic_content_list in the Anthropic-compatible format, not routed to a Bedrock type.

Suggested change
elif content["type"] == "document":
tool_result_content_blocks.append(
BedrockToolResultContentBlock(document=content) # Assume it matches Bedrock structure or Anthropic fallback
)
elif content["type"] == "document":
anthropic_content_list.append(content)

elif content["type"] == "image_url":
format = (
content["image_url"].get("format")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 type key passed to Bedrock DocumentBlock may cause API validation errors

The raw OpenAI-format content element (element) is passed directly as document=element. This dict includes a "type": "document" key, but Bedrock's DocumentBlock TypedDict only defines format, source, and name. The extra type field is serialized and sent to the Bedrock Converse API, which may reject the request with a validation error.

The same issue applies to BedrockToolResultContentBlock(document=content) at lines 3978 and 1731.

The type key should be stripped before passing:

# 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 content is passed to BedrockToolResultContentBlock(document=content).

Comment on lines 4431 to +4437

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 No tests included for the fix

The PR description claims to fix issue #24641 but no unit tests were added to verify the new document block pass-through behaviour in any of the four code paths. Per project policy, fixes should be accompanied by evidence of resolution (e.g. mock unit tests). Relevant test files already exist:

  • tests/litellm_core_utils/test_bedrock_converse_dedup_factory.py
  • tests/llm_translation/test_bedrock_completion.py

Consider adding at minimum one test for _bedrock_converse_messages_pt and one for _convert_to_bedrock_tool_call_result that asserts document blocks are preserved in the output.

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):
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Wrong list used in assistant content branch

In this assistant content block, all other content types append to assistants_parts (see text on line 4605, image_url on line 4617). However, this document branch incorrectly appends to _parts, which belongs to the outer user content loop (_parts: List[BedrockContentBlock] defined around line 4422). This silently contaminates the user content list with assistant document blocks and drops them from the assistant message.

Suggested change
elif element["type"] == "document":
_part = BedrockContentBlock(document=element) # Pass it through since element already has format, name, source
_parts.append(_part)
elif element["type"] == "document":
_part = BedrockContentBlock(document=element) # Pass it through since element already has format, name, source
assistants_parts.append(_part)

elif element["type"] == "image_url":
if isinstance(element["image_url"], dict):
image_url = element["image_url"]["url"]
Expand Down
Loading