@@ -113,10 +113,25 @@ def _create_messages(self, input_items: SyncCursorPage[ResponseItem]) -> Iterabl
113113
114114 Converts OpenAI Responses API input items to chat completion message format.
115115 Handles different types of response items including messages and tool calls.
116+ Groups parallel tool calls under a single assistant message.
117+ Since we iterate backwards and reverse at the end, tool call outputs should
118+ be added before the assistant message with tool calls.
116119 """
117120 messages : list [Message ] = []
121+ current_tool_calls : list [ChatCompletionMessageFunctionToolCall ] = []
122+ tool_call_outputs : list [Message ] = []
123+
118124 for item in input_items :
119125 if item .type == "message" :
126+ # If we have accumulated tool calls, create an assistant message with them
127+ if current_tool_calls :
128+ # Add tool call outputs first (since we reverse at the end)
129+ messages .extend (tool_call_outputs )
130+ tool_call_outputs = []
131+ # Then add the assistant message with tool calls
132+ messages .append (Message (role = "assistant" , tool_calls = current_tool_calls ))
133+ current_tool_calls = []
134+
120135 # This is a message item (input or output)
121136 content = item .content
122137 for content_item in content :
@@ -127,12 +142,21 @@ def _create_messages(self, input_items: SyncCursorPage[ResponseItem]) -> Iterabl
127142 else :
128143 raise NotImplementedError (f"Unsupported content type: { content_item .type } " )
129144 elif item .type == "function_call_output" :
130- messages .append (Message (role = "tool" , content = item .output , tool_call_id = item .call_id ))
145+ # Collect tool call outputs to add before assistant message
146+ tool_call_outputs .append (Message (role = "tool" , content = item .output , tool_call_id = item .call_id ))
131147 elif item .type == "function_call" :
132148 tool_call = ChatCompletionMessageFunctionToolCall (
133149 id = item .call_id , type = "function" , function = Function (name = item .name , arguments = item .arguments )
134150 )
135- messages .append (Message ( role = "assistant" , tool_calls = [ tool_call ]) )
151+ current_tool_calls .append (tool_call )
136152 else :
137153 raise NotImplementedError (f"Unsupported item type: { item .type } " )
154+
155+ # If we have remaining tool calls, create an assistant message with them
156+ if current_tool_calls :
157+ # Add tool call outputs first (since we reverse at the end)
158+ messages .extend (tool_call_outputs )
159+ # Then add the assistant message with tool calls
160+ messages .append (Message (role = "assistant" , tool_calls = current_tool_calls ))
161+
138162 return reversed (messages )
0 commit comments