Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

from __future__ import annotations

import base64
import binascii
import json
import logging
import mimetypes
import time
import uuid
import warnings
Expand All @@ -32,6 +35,7 @@
get_namespaced_value,
)
from agent_framework._telemetry import mark_feature_used
from agent_framework.exceptions import ContentError
from agent_framework.observability import OBSERVABILITY_SETTINGS
from agent_framework_hosting import AgentRunArgs
from openai.types.responses import (
Expand Down Expand Up @@ -141,8 +145,8 @@ def _content_from_input_item(item: Mapping[str, Any]) -> Content:
"""Convert a single OpenAI Responses ``input`` item into a :class:`Content` part.

Handles the ``input_text``/``output_text``/``text`` text variants,
``input_image`` URL references, and ``input_file`` references via either
a public URL or a hosted ``file_id``. Raises ``ValueError`` for any
``input_image`` URL references, and ``input_file`` references via a public
URL, a hosted ``file_id``, or inline ``file_data``. Raises ``ValueError`` for any
unsupported item type so the surrounding parser can return a 422.
"""
item_type = item.get("type")
Expand Down Expand Up @@ -208,7 +212,33 @@ def _content_from_input_item(item: Mapping[str, Any]) -> Content:
**({"filename": filename} if isinstance(filename, str) else {}),
},
)
raise ValueError("input_file requires a non-empty string `file_url` or `file_id`")
file_data = item.get("file_data")
if file_data is not None:
if not isinstance(file_data, str) or not file_data:
raise ValueError("input_file `file_data` must be a non-empty string")
filename = item.get("filename")
if file_data.startswith("data:"):
header, comma, payload = file_data.partition(",")
if not comma or not header.endswith(";base64"):
raise ValueError("input_file `file_data` must be a base64 data URI or plain base64")
else:
# Plain base64 without a data URI prefix; take the media type from the filename.
guessed = mimetypes.guess_type(filename)[0] if isinstance(filename, str) else None
header, payload = f"data:{guessed or 'application/octet-stream'};base64", file_data
try:
base64.b64decode(payload, validate=True)
return Content.from_uri(
uri=f"{header},{payload}",
name=filename if isinstance(filename, str) else None,
additional_properties={
"openai": {"content_type": "input_file"},
"openai_content_type": "input_file",
**({"filename": filename} if isinstance(filename, str) else {}),
},
)
except (binascii.Error, ContentError) as exc:
raise ValueError(f"input_file `file_data` is not valid base64 data: {exc}") from exc
raise ValueError("input_file requires a non-empty string `file_url`, `file_id`, or `file_data`")
raise ValueError(f"Unsupported Responses input content type: {item_type!r}")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,44 @@ def test_input_file_via_file_id(self) -> None:
assert content.additional_properties["openai"]["content_type"] == "input_file"
assert content.additional_properties["openai_content_type"] == "input_file"

@pytest.mark.parametrize(
"file_data",
["data:application/pdf;base64,JVBERi0xLjQK", "JVBERi0xLjQK"],
ids=["data_uri", "plain_base64"],
)
def test_input_file_via_file_data(self, file_data: str) -> None:
msgs = messages_from_responses_input([{"type": "input_file", "file_data": file_data, "filename": "report.pdf"}])
content = msgs[0].contents[0]
assert content.type == "data"
assert content.media_type == "application/pdf"
assert content.uri == "data:application/pdf;base64,JVBERi0xLjQK"
assert content.name == "report.pdf"
assert content.additional_properties["openai_content_type"] == "input_file"

@pytest.mark.parametrize("filename", [None, "data.unknownext"])
def test_input_file_plain_base64_without_known_type_is_octet_stream(self, filename: str | None) -> None:
item: dict[str, Any] = {"type": "input_file", "file_data": "JVBERi0xLjQK"}
if filename is not None:
item["filename"] = filename
content = messages_from_responses_input([item])[0].contents[0]
assert content.media_type == "application/octet-stream"

@pytest.mark.parametrize(
"file_data",
[
"",
123,
"data:application/pdf;base64",
"data:text/plain,hello%20world",
"data:application/pdf;base64,!!!!",
"!!!!",
"A",
],
)
def test_input_file_with_invalid_file_data_raises(self, file_data: object) -> None:
with pytest.raises(ValueError, match="file_data"):
messages_from_responses_input([{"type": "input_file", "file_data": file_data}])

def test_input_file_missing_anchor_raises(self) -> None:
with pytest.raises(ValueError, match="input_file"):
messages_from_responses_input([{"type": "input_file"}])
Expand Down
Loading