mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
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
This commit is contained in:
parent
33c3f13443
commit
420c8c77be
1 changed files with 20 additions and 0 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue