diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 76b3f47db18..7bb25cabf58 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -2418,6 +2418,24 @@ def anthropic_messages_pt( "url": image_url_value["url"], "format": image_url_value.get("format"), } + url_str = ( + image_url_value.get("url") if isinstance(image_url_value, dict) else image_url_value + ) + if isinstance(url_str, str) and _is_anthropic_document_data_uri(url_str): + synth_file_message: ChatCompletionFileObject = { + "type": "file", + "file": {"file_data": url_str}, + } + _document_content_element = anthropic_process_openai_file_message(synth_file_message) + _document_content_element = add_cache_control_to_content( + anthropic_content_element=cast( + AnthropicMessagesDocumentParam, + _document_content_element, + ), + original_content_element=dict(m), + ) + user_content.append(cast(AnthropicMessagesDocumentParam, _document_content_element)) + continue # Bedrock invoke models have format: invoke/... # Vertex AI Anthropic also doesn't support URL sources for images is_bedrock_invoke = model.lower().startswith("invoke/") 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 8edc6a91cbf..4a06ce02359 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 @@ -2163,6 +2163,34 @@ def test_anthropic_messages_pt_file_block_without_cache_control(): assert "cache_control" not in file_block +def test_anthropic_messages_pt_maps_pdf_data_uri_to_document_block(): + messages = [ + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": {"url": "data:application/pdf;base64,AAAA"}, + } + ], + } + ] + + result = anthropic_messages_pt( + messages=messages, + model="claude-sonnet-4-5", + llm_provider="anthropic", + ) + + document = result[0]["content"][0] + assert document["type"] == "document" + assert document["source"] == { + "type": "base64", + "media_type": "application/pdf", + "data": "AAAA", + } + + # ── _convert_to_bedrock_tool_call_invoke tests ──