Address comments

This commit is contained in:
Tao Chen
2026-04-27 09:48:05 -07:00
Unverified
parent 78c7d5fc84
commit 8940b45d5b
7 changed files with 91 additions and 8 deletions
@@ -1087,9 +1087,16 @@ def _convert_file_data(data_uri: str, filename: str | None = None) -> Content:
header, encoded = data_uri.split(";base64,", 1)
media_type = header[len("data:") :]
if media_type.startswith("text/"):
decoded_text = base64.b64decode(encoded).decode("utf-8")
prefix = f"[File: {filename}]\n" if filename else ""
return Content.from_text(f"{prefix}{decoded_text}")
try:
decoded_text = base64.b64decode(encoded).decode("utf-8")
except (ValueError, UnicodeDecodeError):
logger.warning(
"Failed to decode text/* file_data as UTF-8, falling through to URI passthrough.",
exc_info=True,
)
else:
prefix = f"[File: {filename}]\n" if filename else ""
return Content.from_text(f"{prefix}{decoded_text}")
additional_properties = {"filename": filename} if filename else None
return Content.from_uri(data_uri, additional_properties=additional_properties)
@@ -1127,6 +1134,8 @@ def _convert_message_content(content: MessageContent) -> Content:
if content.type == "input_image":
image = cast(MessageContentInputImageContent, content)
if image.image_url:
if image.image_url.startswith("data:"):
return Content.from_uri(image.image_url)
return Content.from_uri(image.image_url, media_type="image/*")
if image.file_id:
return Content.from_hosted_file(image.file_id)
@@ -1548,6 +1548,80 @@ class TestMultiTurnMixedContent:
assert messages[0].contents[1].type == "data"
assert messages[0].contents[1].uri == "data:application/pdf;base64,JVBERi0xLjQ="
async def test_text_mime_file_data_decoded(self) -> None:
"""Agent receives a text/* file_data that is base64-decoded to plain text."""
agent = _make_agent(
response=AgentResponse(messages=[Message(role="assistant", contents=[Content.from_text("Got it")])])
)
server = _make_server(agent)
import base64
encoded = base64.b64encode(b"Hello, world!").decode()
resp = await _post_json(
server,
{
"model": "test-model",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_file",
"file_data": f"data:text/plain;base64,{encoded}",
"filename": "greeting.txt",
},
],
}
],
"stream": False,
},
)
assert resp.status_code == 200
messages = agent.run.call_args.kwargs["messages"]
assert len(messages) == 1
assert messages[0].contents[0].type == "text"
assert messages[0].contents[0].text == "[File: greeting.txt]\nHello, world!"
async def test_text_mime_file_data_invalid_base64_falls_through(self) -> None:
"""Invalid base64 in a text/* file_data falls through to URI passthrough."""
agent = _make_agent(
response=AgentResponse(messages=[Message(role="assistant", contents=[Content.from_text("Got it")])])
)
server = _make_server(agent)
resp = await _post_json(
server,
{
"model": "test-model",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_file",
"file_data": "data:text/plain;base64,!!!invalid!!!",
"filename": "bad.txt",
},
],
}
],
"stream": False,
},
)
assert resp.status_code == 200
messages = agent.run.call_args.kwargs["messages"]
assert len(messages) == 1
assert messages[0].contents[0].type == "data"
assert messages[0].contents[0].uri == "data:text/plain;base64,!!!invalid!!!"
async def test_mixed_text_and_image_input(self) -> None:
"""Agent receives a single message with both text and image content."""
agent = _make_agent(