@@ -400,10 +400,7 @@ def handle_non_streaming_create(
400400 processed_messages = extract_chat_completion_messages (kwargs ["messages" ])
401401
402402 # Check if response contains audio (to sanitize raw_output)
403- has_audio = (
404- hasattr (response .choices [0 ].message , "audio" )
405- and response .choices [0 ].message .audio is not None
406- )
403+ has_audio = bool (getattr (response .choices [0 ].message , "audio" , None ))
407404
408405 # Sanitize raw_output to remove heavy base64 data already uploaded as attachments
409406 raw_output = response .model_dump ()
@@ -481,11 +478,9 @@ def extract_chat_completion_messages(
481478
482479 # If content is a list, process each item
483480 if isinstance (content , list ):
484- normalized_content : List [ContentItem ] = []
485- for item in content :
486- content_item = _normalize_content_item (item )
487- normalized_content .append (content_item )
488-
481+ normalized_content : List [ContentItem ] = [
482+ _normalize_content_item (item ) for item in content
483+ ]
489484 processed_msg = {"role" : role , "content" : normalized_content }
490485 if name :
491486 processed_msg ["name" ] = name
@@ -594,7 +589,8 @@ def _normalize_content_item(item: Dict[str, Any]) -> ContentItem:
594589
595590 else :
596591 # Unknown type, return as TextContent with string representation
597- logger .debug ("Unknown content item type: %s" , item_type )
592+ # Log as warning so we can learn about new OpenAI content types
593+ logger .warning ("Unknown content item type '%s', preserving as text" , item_type )
598594 return TextContent (text = str (item ))
599595
600596
@@ -874,9 +870,7 @@ def handle_responses_non_streaming_create(
874870 if hasattr (response , "model_dump" ):
875871 raw_output = response .model_dump ()
876872 if has_generated_images :
877- raw_output = _sanitize_raw_output (
878- raw_output , has_generated_images = True
879- )
873+ raw_output = _sanitize_raw_output (raw_output , has_generated_images = True )
880874 else :
881875 raw_output = str (response )
882876
@@ -1114,6 +1108,10 @@ def parse_responses_output_data(
11141108 )
11151109 content_items .append (ImageContent (attachment = attachment ))
11161110
1111+ else :
1112+ # Unknown output type - log for future support
1113+ logger .debug ("Unknown Responses API output type: %s" , output_type )
1114+
11171115 # Return appropriate format based on content
11181116 if len (content_items ) > 1 :
11191117 # Multimodal output - return list of ContentItems
@@ -1127,7 +1125,7 @@ def parse_responses_output_data(
11271125 return content_items
11281126
11291127 except Exception as e :
1130- logger .debug ("Could not parse Responses API output data: %s" , e )
1128+ logger .warning ("Could not parse Responses API output data: %s" , e )
11311129
11321130 return None
11331131
@@ -1230,30 +1228,46 @@ def _sanitize_raw_output(
12301228
12311229 sanitized = copy .deepcopy (raw_output )
12321230
1233- # Clear audio data from Chat Completions response
12341231 if has_audio :
1235- try :
1236- for choice in sanitized .get ("choices" , []):
1237- message = choice .get ("message" , {})
1238- if message and "audio" in message and message ["audio" ]:
1239- if "data" in message ["audio" ]:
1240- message ["audio" ]["data" ] = "[UPLOADED_TO_STORAGE]"
1241- except Exception as e :
1242- logger .debug ("Could not sanitize audio data from raw_output: %s" , e )
1232+ _sanitize_audio_data (sanitized )
12431233
1244- # Clear image data from Responses API
12451234 if has_generated_images :
1246- try :
1247- for output_item in sanitized .get ("output" , []):
1248- if output_item .get ("type" ) == "image_generation_call" :
1249- if "result" in output_item :
1250- output_item ["result" ] = "[UPLOADED_TO_STORAGE]"
1251- except Exception as e :
1252- logger .debug ("Could not sanitize image data from raw_output: %s" , e )
1235+ _sanitize_image_data (sanitized )
12531236
12541237 return sanitized
12551238
12561239
1240+ def _sanitize_audio_data (sanitized : Dict [str , Any ]) -> None :
1241+ """Remove audio base64 data from Chat Completions response.
1242+
1243+ Args:
1244+ sanitized: The raw output dict to sanitize (modified in place)
1245+ """
1246+ try :
1247+ for choice in sanitized .get ("choices" , []):
1248+ message = choice .get ("message" , {})
1249+ if message and "audio" in message and message ["audio" ]:
1250+ if "data" in message ["audio" ]:
1251+ message ["audio" ]["data" ] = "[UPLOADED_TO_STORAGE]"
1252+ except Exception as e :
1253+ logger .debug ("Could not sanitize audio data from raw_output: %s" , e )
1254+
1255+
1256+ def _sanitize_image_data (sanitized : Dict [str , Any ]) -> None :
1257+ """Remove generated image base64 data from Responses API output.
1258+
1259+ Args:
1260+ sanitized: The raw output dict to sanitize (modified in place)
1261+ """
1262+ try :
1263+ for output_item in sanitized .get ("output" , []):
1264+ if output_item .get ("type" ) == "image_generation_call" :
1265+ if "result" in output_item :
1266+ output_item ["result" ] = "[UPLOADED_TO_STORAGE]"
1267+ except Exception as e :
1268+ logger .debug ("Could not sanitize image data from raw_output: %s" , e )
1269+
1270+
12571271def parse_non_streaming_output_data (
12581272 response : "openai.types.chat.chat_completion.ChatCompletion" ,
12591273) -> Union [str , List [ContentItem ], Dict [str , Any ], None ]:
0 commit comments