fix(anthropic): keep the charset of a mixed-case text/plain data URI

This commit is contained in:
mateo-berri 2026-10-29 11:14:48 -07:00
parent 53f69b483d
commit 3c7a77e3b0
2 changed files with 15 additions and 3 deletions

View file

@ -883,10 +883,10 @@ _CHARSET_AWARE_MEDIA_TYPE: Final = "text/plain"
def _media_type_with_original_parameters(format_override: str, original_media_type: str) -> str:
if format_override.strip() != _CHARSET_AWARE_MEDIA_TYPE:
if format_override.strip().lower() != _CHARSET_AWARE_MEDIA_TYPE:
return format_override
base_type, separator, parameters = original_media_type.partition(";")
if not separator or base_type.strip() != format_override.strip():
if not separator or base_type.strip().lower() != format_override.strip().lower():
return format_override
return f"{format_override}{separator}{parameters}"

View file

@ -5235,8 +5235,20 @@ _LATIN1_TEXT_DATA_URI = "data:text/plain;charset=iso-8859-1;base64," + base64.b6
{"type": "image_url", "image_url": {"url": "data:TEXT/PLAIN;base64," + _TEXT_B64}},
"the secret code word is MANGO\n",
),
(
{"type": "image_url", "image_url": {"url": _LATIN1_TEXT_DATA_URI.replace("text/plain", "TEXT/PLAIN", 1)}},
"the secret code word is MANGÓ\n",
),
],
ids=[
"image_url",
"file",
"image_url_charset",
"file_charset",
"file_latin1",
"image_url_uppercase",
"uppercase_latin1",
],
ids=["image_url", "file", "image_url_charset", "file_charset", "file_latin1", "image_url_uppercase"],
)
def test_anthropic_messages_pt_text_plain_data_uri_becomes_text_source_document(block, expected_text):
messages = [{"role": "user", "content": [{"type": "text", "text": "What is the code word?"}, block]}]