@@ -438,14 +438,23 @@ async def test_use_path_output(use_async_client):
438438 )
439439 mock_prediction_endpoints (output_data = "https://example.com/output.jpg" )
440440
441+ # Mock the file download
442+ respx .get ("https://example.com/output.jpg" ).mock (
443+ return_value = httpx .Response (200 , content = b"fake image data" )
444+ )
445+
441446 # Call use with "acme/hotdog-detector"
442447 hotdog_detector = replicate .use ("acme/hotdog-detector" )
443448
444449 # Call function with prompt="hello world"
445450 output = hotdog_detector (prompt = "hello world" )
446451
447- # Assert that output is returned as a string URL
448- assert output == "https://example.com/output.jpg"
452+ # Assert that output is returned as a Path object
453+ from pathlib import Path
454+
455+ assert isinstance (output , Path )
456+ assert output .exists ()
457+ assert output .read_bytes () == b"fake image data"
449458
450459
451460@pytest .mark .asyncio
@@ -474,17 +483,29 @@ async def test_use_list_of_paths_output(use_async_client):
474483 ]
475484 )
476485
486+ # Mock the file downloads
487+ respx .get ("https://example.com/output1.jpg" ).mock (
488+ return_value = httpx .Response (200 , content = b"fake image 1 data" )
489+ )
490+ respx .get ("https://example.com/output2.jpg" ).mock (
491+ return_value = httpx .Response (200 , content = b"fake image 2 data" )
492+ )
493+
477494 # Call use with "acme/hotdog-detector"
478495 hotdog_detector = replicate .use ("acme/hotdog-detector" )
479496
480497 # Call function with prompt="hello world"
481498 output = hotdog_detector (prompt = "hello world" )
482499
483- # Assert that output is returned as a list of URLs
484- assert output == [
485- "https://example.com/output1.jpg" ,
486- "https://example.com/output2.jpg" ,
487- ]
500+ # Assert that output is returned as a list of Path objects
501+ from pathlib import Path
502+
503+ assert isinstance (output , list )
504+ assert len (output ) == 2
505+ assert all (isinstance (path , Path ) for path in output )
506+ assert all (path .exists () for path in output )
507+ assert output [0 ].read_bytes () == b"fake image 1 data"
508+ assert output [1 ].read_bytes () == b"fake image 2 data"
488509
489510
490511@pytest .mark .asyncio
@@ -514,17 +535,29 @@ async def test_use_iterator_of_paths_output(use_async_client):
514535 ]
515536 )
516537
538+ # Mock the file downloads
539+ respx .get ("https://example.com/output1.jpg" ).mock (
540+ return_value = httpx .Response (200 , content = b"fake image 1 data" )
541+ )
542+ respx .get ("https://example.com/output2.jpg" ).mock (
543+ return_value = httpx .Response (200 , content = b"fake image 2 data" )
544+ )
545+
517546 # Call use with "acme/hotdog-detector"
518547 hotdog_detector = replicate .use ("acme/hotdog-detector" )
519548
520549 # Call function with prompt="hello world"
521550 output = hotdog_detector (prompt = "hello world" )
522551
523- # Assert that output is returned as a list of URLs
524- assert output == [
525- "https://example.com/output1.jpg" ,
526- "https://example.com/output2.jpg" ,
527- ]
552+ # Assert that output is returned as a list of Path objects
553+ from pathlib import Path
554+
555+ assert isinstance (output , list )
556+ assert len (output ) == 2
557+ assert all (isinstance (path , Path ) for path in output )
558+ assert all (path .exists () for path in output )
559+ assert output [0 ].read_bytes () == b"fake image 1 data"
560+ assert output [1 ].read_bytes () == b"fake image 2 data"
528561
529562
530563@pytest .mark .asyncio
@@ -600,3 +633,123 @@ async def test_use_function_logs_method_polling(use_async_client):
600633 # Call logs method again to get updated logs (simulates polling)
601634 updated_logs = run .logs ()
602635 assert updated_logs == "Starting prediction...\n Processing input..."
636+
637+
638+ @pytest .mark .asyncio
639+ @pytest .mark .parametrize ("use_async_client" , [False ])
640+ @respx .mock
641+ async def test_use_object_output_with_file_properties (use_async_client ):
642+ mock_model_endpoints (
643+ version_overrides = {
644+ "openapi_schema" : {
645+ "components" : {
646+ "schemas" : {
647+ "Output" : {
648+ "type" : "object" ,
649+ "properties" : {
650+ "text" : {"type" : "string" , "title" : "Text" },
651+ "image" : {
652+ "type" : "string" ,
653+ "format" : "uri" ,
654+ "title" : "Image" ,
655+ },
656+ "count" : {"type" : "integer" , "title" : "Count" },
657+ },
658+ "title" : "Output" ,
659+ }
660+ }
661+ }
662+ }
663+ }
664+ )
665+ mock_prediction_endpoints (
666+ output_data = {
667+ "text" : "Generated text" ,
668+ "image" : "https://example.com/generated.png" ,
669+ "count" : 42 ,
670+ }
671+ )
672+
673+ # Mock the file download
674+ respx .get ("https://example.com/generated.png" ).mock (
675+ return_value = httpx .Response (200 , content = b"fake png data" )
676+ )
677+
678+ # Call use with "acme/hotdog-detector"
679+ hotdog_detector = replicate .use ("acme/hotdog-detector" )
680+
681+ # Call function with prompt="hello world"
682+ output = hotdog_detector (prompt = "hello world" )
683+
684+ # Assert that output is returned as an object with file downloaded
685+ from pathlib import Path
686+
687+ assert isinstance (output , dict )
688+ assert output ["text" ] == "Generated text"
689+ assert output ["count" ] == 42
690+ assert isinstance (output ["image" ], Path )
691+ assert output ["image" ].exists ()
692+ assert output ["image" ].read_bytes () == b"fake png data"
693+
694+
695+ @pytest .mark .asyncio
696+ @pytest .mark .parametrize ("use_async_client" , [False ])
697+ @respx .mock
698+ async def test_use_object_output_with_file_list_property (use_async_client ):
699+ mock_model_endpoints (
700+ version_overrides = {
701+ "openapi_schema" : {
702+ "components" : {
703+ "schemas" : {
704+ "Output" : {
705+ "type" : "object" ,
706+ "properties" : {
707+ "text" : {"type" : "string" , "title" : "Text" },
708+ "images" : {
709+ "type" : "array" ,
710+ "items" : {"type" : "string" , "format" : "uri" },
711+ "title" : "Images" ,
712+ },
713+ },
714+ "title" : "Output" ,
715+ }
716+ }
717+ }
718+ }
719+ }
720+ )
721+ mock_prediction_endpoints (
722+ output_data = {
723+ "text" : "Generated text" ,
724+ "images" : [
725+ "https://example.com/image1.png" ,
726+ "https://example.com/image2.png" ,
727+ ],
728+ }
729+ )
730+
731+ # Mock the file downloads
732+ respx .get ("https://example.com/image1.png" ).mock (
733+ return_value = httpx .Response (200 , content = b"fake png 1 data" )
734+ )
735+ respx .get ("https://example.com/image2.png" ).mock (
736+ return_value = httpx .Response (200 , content = b"fake png 2 data" )
737+ )
738+
739+ # Call use with "acme/hotdog-detector"
740+ hotdog_detector = replicate .use ("acme/hotdog-detector" )
741+
742+ # Call function with prompt="hello world"
743+ output = hotdog_detector (prompt = "hello world" )
744+
745+ # Assert that output is returned as an object with files downloaded
746+ from pathlib import Path
747+
748+ assert isinstance (output , dict )
749+ assert output ["text" ] == "Generated text"
750+ assert isinstance (output ["images" ], list )
751+ assert len (output ["images" ]) == 2
752+ assert all (isinstance (path , Path ) for path in output ["images" ])
753+ assert all (path .exists () for path in output ["images" ])
754+ assert output ["images" ][0 ].read_bytes () == b"fake png 1 data"
755+ assert output ["images" ][1 ].read_bytes () == b"fake png 2 data"
0 commit comments