From bd1bce0829a1bf89d7210e213b7949d9b2f2ff5d Mon Sep 17 00:00:00 2001 From: Shubham Arora Date: Sun, 29 Mar 2026 14:45:38 +0530 Subject: [PATCH] fix: reject non-base64 document sources with a clear error URL-type document sources (e.g. {"type": "url", "url": "..."}) would crash with an opaque KeyError on missing 'media_type'. Guard at the top of _process_document_message and raise a clear ValueError since Bedrock Converse only supports base64-encoded document sources. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../prompt_templates/factory.py | 6 +++++ ...llm_core_utils_prompt_templates_factory.py | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 4990c7081b7..51e8dc6b69c 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -4739,6 +4739,12 @@ class BedrockConverseMessagesProcessor: {"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "..."}} """ source = element["source"] + source_type = source.get("type") + if source_type != "base64": + raise ValueError( + f"Bedrock Converse only supports base64-encoded document sources, got '{source_type}'. " + "Please convert the document to base64 before sending to Bedrock." + ) media_type: str = source["media_type"] data: str = source["data"] doc_format = BedrockImageProcessor._validate_format( diff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py index 462d697e0a3..27686162f70 100644 --- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py +++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py @@ -2508,3 +2508,26 @@ def test_bedrock_converse_messages_pt_document_deterministic_name(): name1 = result1[0]["content"][0]["document"]["name"] name2 = result2[0]["content"][0]["document"]["name"] assert name1 == name2 + + +def test_bedrock_converse_messages_pt_document_rejects_url_source(): + """Test that a URL-type document source raises a clear error instead of KeyError.""" + messages = [ + { + "role": "user", + "content": [ + { + "type": "document", + "source": { + "type": "url", + "url": "https://example.com/doc.pdf", + }, + }, + ], + } + ] + + with pytest.raises(ValueError, match="only supports base64-encoded"): + _bedrock_converse_messages_pt( + messages, "anthropic.claude-sonnet-4-6", "bedrock" + )