33from collections .abc import AsyncIterable , Awaitable , Callable , Mapping , MutableMapping , MutableSequence , Sequence
44from datetime import datetime , timezone
55from itertools import chain
6- from typing import Any , TypeVar
6+ from typing import Any , TypeVar , cast
77
88from openai import AsyncOpenAI , BadRequestError
99from openai .types .responses .file_search_tool_param import FileSearchToolParam
@@ -199,7 +199,7 @@ def _prepare_text_config(
199199 return response_format , prepared_text
200200
201201 if isinstance (response_format , Mapping ):
202- format_config = self ._convert_response_format (response_format )
202+ format_config = self ._convert_response_format (cast ( "Mapping[str, Any]" , response_format ) )
203203 if prepared_text is None :
204204 prepared_text = {}
205205 elif "format" in prepared_text and prepared_text ["format" ] != format_config :
@@ -212,20 +212,21 @@ def _prepare_text_config(
212212 def _convert_response_format (self , response_format : Mapping [str , Any ]) -> dict [str , Any ]:
213213 """Convert Chat style response_format into Responses text format config."""
214214 if "format" in response_format and isinstance (response_format ["format" ], Mapping ):
215- return dict (response_format ["format" ])
215+ return dict (cast ( "Mapping[str, Any]" , response_format ["format" ]) )
216216
217217 format_type = response_format .get ("type" )
218218 if format_type == "json_schema" :
219219 schema_section = response_format .get ("json_schema" , response_format )
220220 if not isinstance (schema_section , Mapping ):
221221 raise ServiceInvalidRequestError ("json_schema response_format must be a mapping." )
222- schema = schema_section .get ("schema" )
222+ schema_section_typed = cast ("Mapping[str, Any]" , schema_section )
223+ schema : Any = schema_section_typed .get ("schema" )
223224 if schema is None :
224225 raise ServiceInvalidRequestError ("json_schema response_format requires a schema." )
225- name = (
226- schema_section .get ("name" )
227- or schema_section .get ("title" )
228- or (schema .get ("title" ) if isinstance (schema , Mapping ) else None )
226+ name : str = str (
227+ schema_section_typed .get ("name" )
228+ or schema_section_typed .get ("title" )
229+ or (cast ( "Mapping[str, Any]" , schema ) .get ("title" ) if isinstance (schema , Mapping ) else None )
229230 or "response"
230231 )
231232 format_config : dict [str , Any ] = {
@@ -532,12 +533,13 @@ def _openai_content_parser(
532533 "text" : content .text ,
533534 },
534535 }
535- if content .additional_properties is not None :
536- if status := content .additional_properties .get ("status" ):
536+ props : dict [str , Any ] | None = getattr (content , "additional_properties" , None )
537+ if props :
538+ if status := props .get ("status" ):
537539 ret ["status" ] = status
538- if reasoning_text := content . additional_properties .get ("reasoning_text" ):
540+ if reasoning_text := props .get ("reasoning_text" ):
539541 ret ["content" ] = {"type" : "reasoning_text" , "text" : reasoning_text }
540- if encrypted_content := content . additional_properties .get ("encrypted_content" ):
542+ if encrypted_content := props .get ("encrypted_content" ):
541543 ret ["encrypted_content" ] = encrypted_content
542544 return ret
543545 case DataContent () | UriContent ():
@@ -824,7 +826,7 @@ def _create_response_content(
824826 "raw_representation" : response ,
825827 }
826828
827- conversation_id = self .get_conversation_id (response , chat_options .store )
829+ conversation_id = self .get_conversation_id (response , chat_options .store ) # type: ignore[reportArgumentType]
828830
829831 if conversation_id :
830832 args ["conversation_id" ] = conversation_id
@@ -911,6 +913,8 @@ def _create_streaming_response_content(
911913 metadata .update (self ._get_metadata_from_response (event_part ))
912914 case "refusal" :
913915 contents .append (TextContent (text = event_part .refusal , raw_representation = event ))
916+ case _:
917+ pass
914918 case "response.output_text.delta" :
915919 contents .append (TextContent (text = event .delta , raw_representation = event ))
916920 metadata .update (self ._get_metadata_from_response (event ))
@@ -1032,6 +1036,60 @@ def _create_streaming_response_content(
10321036 raw_representation = event ,
10331037 )
10341038 )
1039+ case "response.output_text.annotation.added" :
1040+ # Handle streaming text annotations (file citations, file paths, etc.)
1041+ annotation : Any = event .annotation
1042+
1043+ def _get_ann_value (key : str ) -> Any :
1044+ """Extract value from annotation (dict or object)."""
1045+ if isinstance (annotation , dict ):
1046+ return cast ("dict[str, Any]" , annotation ).get (key )
1047+ return getattr (annotation , key , None )
1048+
1049+ ann_type = _get_ann_value ("type" )
1050+ ann_file_id = _get_ann_value ("file_id" )
1051+ if ann_type == "file_path" :
1052+ if ann_file_id :
1053+ contents .append (
1054+ HostedFileContent (
1055+ file_id = str (ann_file_id ),
1056+ additional_properties = {
1057+ "annotation_index" : event .annotation_index ,
1058+ "index" : _get_ann_value ("index" ),
1059+ },
1060+ raw_representation = event ,
1061+ )
1062+ )
1063+ elif ann_type == "file_citation" :
1064+ if ann_file_id :
1065+ contents .append (
1066+ HostedFileContent (
1067+ file_id = str (ann_file_id ),
1068+ additional_properties = {
1069+ "annotation_index" : event .annotation_index ,
1070+ "filename" : _get_ann_value ("filename" ),
1071+ "index" : _get_ann_value ("index" ),
1072+ },
1073+ raw_representation = event ,
1074+ )
1075+ )
1076+ elif ann_type == "container_file_citation" :
1077+ if ann_file_id :
1078+ contents .append (
1079+ HostedFileContent (
1080+ file_id = str (ann_file_id ),
1081+ additional_properties = {
1082+ "annotation_index" : event .annotation_index ,
1083+ "container_id" : _get_ann_value ("container_id" ),
1084+ "filename" : _get_ann_value ("filename" ),
1085+ "start_index" : _get_ann_value ("start_index" ),
1086+ "end_index" : _get_ann_value ("end_index" ),
1087+ },
1088+ raw_representation = event ,
1089+ )
1090+ )
1091+ else :
1092+ logger .debug ("Unparsed annotation type in streaming: %s" , ann_type )
10351093 case _:
10361094 logger .debug ("Unparsed event of type: %s: %s" , event .type , event )
10371095
0 commit comments