@@ -296,9 +296,13 @@ def merge_tool_calls(messages: List[ContextMessage]) -> List[Dict[str, Any]]:
296296 tc ["function" ]["arguments" ] = json .dumps (tc ["function" ]["arguments" ])
297297 msg_dict ["tool_calls" ] = tool_calls
298298
299- # Add tool_call_id if present (for tool role messages)
300- if current_messages and current_messages [0 ].tool_call_id :
301- msg_dict ["tool_call_id" ] = current_messages [0 ].tool_call_id
299+ # Add tool_call_id for tool role messages (required by OpenAI API)
300+ if role_str == "tool" :
301+ if current_messages and current_messages [0 ].tool_call_id :
302+ msg_dict ["tool_call_id" ] = current_messages [0 ].tool_call_id
303+ else :
304+ # If not provided, use empty string to satisfy schema
305+ msg_dict ["tool_call_id" ] = ""
302306
303307 messages .append (msg_dict )
304308 current_messages = [msg ]
@@ -322,9 +326,13 @@ def merge_tool_calls(messages: List[ContextMessage]) -> List[Dict[str, Any]]:
322326 tc ["function" ]["arguments" ] = json .dumps (tc ["function" ]["arguments" ])
323327 msg_dict ["tool_calls" ] = tool_calls
324328
325- # Add tool_call_id if present (for tool role messages)
326- if current_messages and current_messages [0 ].tool_call_id :
327- msg_dict ["tool_call_id" ] = current_messages [0 ].tool_call_id
329+ # Add tool_call_id for tool role messages (required by OpenAI API)
330+ if role_str == "tool" :
331+ if current_messages and current_messages [0 ].tool_call_id :
332+ msg_dict ["tool_call_id" ] = current_messages [0 ].tool_call_id
333+ else :
334+ # If not provided, use empty string to satisfy schema
335+ msg_dict ["tool_call_id" ] = ""
328336
329337 messages .append (msg_dict )
330338
0 commit comments