From 5f6d07ff326424821d6262736c8614f2003fae1c Mon Sep 17 00:00:00 2001 From: Ruiming Zhao Date: Fri, 14 Aug 2026 16:37:44 -0700 Subject: [PATCH] fix(anthropic): build document blocks directly from the data URI --- .../prompt_templates/factory.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 7bb25cabf58..574d53c8313 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -2422,17 +2422,21 @@ def anthropic_messages_pt( 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, + image_chunk: Final = convert_to_anthropic_image_obj( + openai_image_url=url_str, + format=None, + ) + _document_content_element: Final = AnthropicMessagesDocumentParam( + type="document", + source=AnthropicContentParamSource( + type="base64", + media_type=image_chunk["media_type"], + data=image_chunk["data"], ), - original_content_element=dict(m), + ) + add_cache_control_to_content( + anthropic_content_element=_document_content_element, + original_content_element=m, ) user_content.append(cast(AnthropicMessagesDocumentParam, _document_content_element)) continue