@@ -77,15 +77,31 @@ def generate_image(
7777
7878 # Handle Flux Kontext Pro output format
7979 image_data : Optional [bytes ] = None
80+ generated_image_url : str = ""
81+
8082 try :
81- # Flux Kontext Pro returns an object that can be used directly as image data
82- # and also has a .url() method for the URL
83- generated_image_url = output .url ()
84- print (f"[TOOL] Extracted URL using output.url(): { generated_image_url } " )
85-
86- # Get the image data directly from the output object
87- image_data = output .read ()
88- print (f"[TOOL] Got image data directly from output, size: { len (image_data ) if image_data else 0 } bytes" )
83+ # Check if output is a string (direct URL) or an object with methods
84+ if isinstance (output , str ):
85+ # Output is a direct URL string
86+ generated_image_url = output
87+ print (f"[TOOL] Output is direct URL string: { generated_image_url } " )
88+
89+ # Download the image from the URL
90+ import requests
91+
92+ response = requests .get (generated_image_url )
93+ response .raise_for_status ()
94+ image_data = response .content
95+ print (f"[TOOL] Downloaded image data from URL, size: { len (image_data )} bytes" )
96+
97+ else :
98+ # Output is an object with .url() and .read() methods
99+ generated_image_url = output .url ()
100+ print (f"[TOOL] Extracted URL using output.url(): { generated_image_url } " )
101+
102+ # Get the image data directly from the output object
103+ image_data = output .read ()
104+ print (f"[TOOL] Got image data directly from output, size: { len (image_data ) if image_data else 0 } bytes" )
89105
90106 except AttributeError as e :
91107 print (f"[TOOL] Error accessing output methods: { e } " )
0 commit comments