File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
pydantic_ai_slim/pydantic_ai Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -545,11 +545,11 @@ def from_path(cls, path: PathLike[str]) -> BinaryContent:
545545 path = Path (path )
546546 if not path .exists ():
547547 raise FileNotFoundError (f'File not found: { path } ' )
548- media_type , _ = guess_type (path . as_posix () )
548+ media_type , _ = guess_type (path )
549549 if media_type is None :
550550 media_type = 'application/octet-stream'
551551
552- return cls ( data = path .read_bytes (), media_type = media_type )
552+ return cls . narrow_type ( cls ( data = path .read_bytes (), media_type = media_type ) )
553553
554554 @pydantic .computed_field
555555 @property
Original file line number Diff line number Diff line change @@ -615,7 +615,7 @@ def test_binary_content_validation_with_optional_identifier():
615615 )
616616
617617
618- def test_file_part_from_path (tmp_path : Path ):
618+ def test_binary_content_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' )
@@ -632,3 +632,18 @@ def test_file_part_from_path(tmp_path: Path):
632632 test_unknown_file .write_text ('some content' , encoding = 'utf-8' )
633633 binary_content = BinaryContent .from_path (test_unknown_file )
634634 assert binary_content == snapshot (BinaryContent (data = b'some content' , media_type = 'application/octet-stream' ))
635+
636+ # test string path
637+ test_txt_file = tmp_path / 'test.txt'
638+ test_txt_file .write_text ('just some text' , encoding = 'utf-8' )
639+ string_path = test_txt_file .as_posix ()
640+ binary_content = BinaryContent .from_path (string_path ) # pyright: ignore[reportArgumentType]
641+ assert binary_content == snapshot (BinaryContent (data = b'just some text' , media_type = 'text/plain' ))
642+
643+ # test image file
644+ test_jpg_file = tmp_path / 'test.jpg'
645+ test_jpg_file .write_bytes (b'\xff \xd8 \xff \xe0 ' + b'0' * 100 ) # minimal JPEG header + padding
646+ binary_content = BinaryContent .from_path (test_jpg_file )
647+ assert binary_content == snapshot (
648+ BinaryImage (data = b'\xff \xd8 \xff \xe0 ' + b'0' * 100 , media_type = 'image/jpeg' , _identifier = 'bc8d49' )
649+ )
You can’t perform that action at this time.
0 commit comments