Skip to content

Commit aad356e

Browse files
committed
move classmethod to BinaryContent
1 parent ae701f3 commit aad356e

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,25 @@ def from_data_uri(cls, data_uri: str) -> BinaryContent:
532532
media_type, data = data_uri[len(prefix) :].split(';base64,', 1)
533533
return cls.narrow_type(cls(data=base64.b64decode(data), media_type=media_type))
534534

535+
@classmethod
536+
def from_path(cls, path: PathLike[str]) -> BinaryContent:
537+
"""Create a `BinaryContent` from a path.
538+
539+
Defaults to 'application/octet-stream' if the media type cannot be inferred.
540+
541+
Raises:
542+
FileNotFoundError: if the file does not exist.
543+
PermissionError: if the file cannot be read.
544+
"""
545+
path = Path(path)
546+
if not path.exists():
547+
raise FileNotFoundError(f'File not found: {path}')
548+
media_type, _ = guess_type(path.as_posix())
549+
if media_type is None:
550+
media_type = 'application/octet-stream'
551+
552+
return cls(data=path.read_bytes(), media_type=media_type)
553+
535554
@pydantic.computed_field
536555
@property
537556
def identifier(self) -> str:
@@ -1074,26 +1093,6 @@ def has_content(self) -> bool:
10741093
"""Return `True` if the file content is non-empty."""
10751094
return bool(self.content.data)
10761095

1077-
@classmethod
1078-
def from_path(cls, path: PathLike[str]) -> FilePart:
1079-
"""Create a `BinaryContent` from a path.
1080-
1081-
Defaults to 'application/octet-stream' if the media type cannot be inferred.
1082-
1083-
Raises:
1084-
FileNotFoundError: if the file does not exist.
1085-
PermissionError: if the file cannot be read.
1086-
"""
1087-
path = Path(path)
1088-
if not path.exists():
1089-
raise FileNotFoundError(f'File not found: {path}')
1090-
media_type, _ = guess_type(path.as_posix())
1091-
if media_type is None:
1092-
media_type = 'application/octet-stream'
1093-
1094-
binary_content = BinaryContent(data=path.read_bytes(), media_type=media_type)
1095-
return cls(content=binary_content)
1096-
10971096
__repr__ = _utils.dataclasses_no_defaults_repr
10981097

10991098

tests/test_messages.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,20 +619,16 @@ def test_file_part_from_path(tmp_path: Path):
619619
# test normal file
620620
test_xml_file = tmp_path / 'test.xml'
621621
test_xml_file.write_text('<think>about trains</think>', encoding='utf-8')
622-
file_part = FilePart.from_path(test_xml_file)
623-
assert file_part == snapshot(
624-
FilePart(content=BinaryContent(data=b'<think>about trains</think>', media_type='application/xml'))
625-
)
622+
binary_content = BinaryContent.from_path(test_xml_file)
623+
assert binary_content == snapshot(BinaryContent(data=b'<think>about trains</think>', media_type='application/xml'))
626624

627625
# test non-existent file
628626
non_existent_file = tmp_path / 'non-existent.txt'
629627
with pytest.raises(FileNotFoundError, match='File not found:'):
630-
FilePart.from_path(non_existent_file)
628+
BinaryContent.from_path(non_existent_file)
631629

632630
# test file with unknown media type
633631
test_unknown_file = tmp_path / 'test.unknownext'
634632
test_unknown_file.write_text('some content', encoding='utf-8')
635-
file_part = FilePart.from_path(test_unknown_file)
636-
assert file_part == snapshot(
637-
FilePart(content=BinaryContent(data=b'some content', media_type='application/octet-stream'))
638-
)
633+
binary_content = BinaryContent.from_path(test_unknown_file)
634+
assert binary_content == snapshot(BinaryContent(data=b'some content', media_type='application/octet-stream'))

0 commit comments

Comments
 (0)