Skip to content
Closed
Changes from 2 commits
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
16 changes: 16 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":
# 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")
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":
# 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)

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: anthropic_content_list is not defined in _convert_to_bedrock_tool_call_result

This function only initialises tool_result_content_blocks (line 3964). There is no anthropic_content_list variable anywhere in _convert_to_bedrock_tool_call_result, so this line will raise NameError: name 'anthropic_content_list' is not defined at runtime whenever a tool-message content list contains a document block — the exact scenario this PR intends to fix.

The document block should be wrapped in a BedrockToolResultContentBlock and appended to tool_result_content_blocks, mirroring the text and image branches immediately above:

Suggested change
anthropic_content_list.append(doc_content)
doc_content = {k: v for k, v in content.items() if k != "type"}
tool_result_content_blocks.append(
BedrockToolResultContentBlock(document=doc_content)
)

elif content["type"] == "image_url":
format: Optional[str] = None
if isinstance(content["image_url"], dict):
Expand Down Expand Up @@ -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

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 +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"]
Expand Down
Loading