From 420c8c77be5c4231ff747ceb07114d01eb690df5 Mon Sep 17 00:00:00 2001 From: "xy.kong" Date: Wed, 1 Apr 2026 14:27:15 +0800 Subject: [PATCH] fix(anthropic): auto-detect image media_type from magic bytes Anthropic API rejects requests where the declared media_type in the data URI prefix (e.g. 'data:image/png;base64,...') does not match the actual image bytes, returning: 'The image was specified using the image/png media type, but the image appears to be a image/jpeg image' convert_to_anthropic_image_obj() trusted the declared type from the data URI without verifying it against the actual bytes. Now it decodes the first 128 bytes and runs them through the existing get_image_type() magic-byte detector. If the detected MIME type differs, the detected type is used. Detection failure falls back to the declared type gracefully. Fixes #24900 --- .../prompt_templates/factory.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index d29ca1649ff..38a8856d8f4 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -898,6 +898,26 @@ def convert_to_anthropic_image_obj( else: media_type = media_type.replace("\\/", "/") + # Anthropic rejects requests where the declared media_type mismatches the actual + # image bytes. Auto-detect from magic bytes and override the declared type when needed. + try: + from litellm.litellm_core_utils.token_counter import get_image_type + + image_bytes = base64.b64decode(base64_data[:128]) + detected = get_image_type(image_bytes) + if detected is not None: + detected_mime = "image/{}".format(detected) + if detected_mime != media_type: + verbose_logger.debug( + "convert_to_anthropic_image_obj: declared media_type=%s does not match " + "detected type=%s; using detected type.", + media_type, + detected_mime, + ) + media_type = detected_mime + except Exception: + pass + return GenericImageParsingChunk( type="base64", media_type=media_type,