@@ -56,7 +56,9 @@ def parse_tool_calls(tool_calls_data):
5656 Returns:
5757 The parsed tool calls.
5858 """
59- if ChatCompletionMessageToolCall is not None and isinstance (tool_calls_data , ChatCompletionMessageToolCall ):
59+ if ChatCompletionMessageToolCall is not None and isinstance (
60+ tool_calls_data , ChatCompletionMessageToolCall
61+ ):
6062 validate_type (tool_calls_data .id , str , "id" )
6163 validate_type (tool_calls_data .type , str , "type" )
6264 parse_function_call (tool_calls_data .function )
@@ -217,10 +219,58 @@ def default_json_serializer(o: Any) -> Any:
217219 raise TypeError (f"Object of type { o .__class__ .__name__ } is not JSON serializable" )
218220
219221
222+ def is_openai_response_structure (data : Any ) -> bool :
223+ """
224+ Check if data matches the general top-level shape of an OpenAI Responses API result.
225+
226+ The OpenAI Responses API structure includes:
227+ - id: string identifier
228+ - object: string (must be "response")
229+ - created_at: integer timestamp
230+ - status: string (e.g., "completed", "in_progress")
231+ - output: list of output items
232+ - usage: dict with token usage
233+
234+ Args:
235+ data: The dictionary to check.
236+
237+ Returns:
238+ True if the data matches the OpenAI Responses API structure, False otherwise.
239+ """
240+ if not isinstance (data , dict ):
241+ return False
242+
243+ # Check for required OpenAI Responses API fields
244+ required_fields = {
245+ "id" : str ,
246+ "object" : str ,
247+ "created_at" : (int , float ),
248+ "status" : str ,
249+ "output" : list ,
250+ "usage" : dict ,
251+ }
252+
253+ for field , expected_type in required_fields .items ():
254+ if field not in data :
255+ return False
256+
257+ value = data [field ]
258+ if not isinstance (value , expected_type ):
259+ return False
260+
261+ # Verify that object is specifically "response"
262+ if data .get ("object" ) != "response" :
263+ return False
264+
265+ return True
266+
267+
220268def parse_result (data : Any ) -> Dict [str , Any ]:
221269 """
222270 Parse result from a dictionary.
223271
272+ Supports both OpenAI Chat Completion API and OpenAI Responses API result structures.
273+
224274 Args:
225275 data: The dictionary to parse.
226276
@@ -229,6 +279,14 @@ def parse_result(data: Any) -> Dict[str, Any]:
229279 """
230280 if not isinstance (data , dict ):
231281 raise ValueError ("Text completion is not supported." )
282+
283+ # Check if this is an OpenAI Responses API result structure
284+ if is_openai_response_structure (data ):
285+ # For Responses API results, return as-is without deep validation
286+ # Only the general top-level shape is validated by is_openai_response_structure
287+ return data
288+
289+ # Otherwise, process as Chat Completion API result (existing behavior)
232290 validate_type (data .get ("id" ), str , "id" )
233291 validate_optional_type (data .get ("object" ), str , "object" )
234292 validate_type (data .get ("created" ), int , "created" )
0 commit comments