From adcbba34f8bbfbab3e4041269a084f2b71c076d9 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 21 Mar 2026 20:03:02 -0500 Subject: [PATCH] refac --- backend/open_webui/routers/openai.py | 32 ++++++++++++++++++++++++++ backend/open_webui/utils/middleware.py | 17 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index 89fbf4852c..fa4626ee24 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -799,6 +799,38 @@ def convert_to_responses_payload(payload: dict) -> dict: system_content = '\n'.join(p.get('text', '') for p in content if p.get('type') == 'text') continue + # Handle assistant messages with tool_calls (from convert_output_to_messages) + if role == 'assistant' and msg.get('tool_calls'): + # Add text content as message if present + if content: + text = content if isinstance(content, str) else '\n'.join( + p.get('text', '') for p in content if p.get('type') == 'text' + ) + if text.strip(): + input_items.append({ + 'type': 'message', 'role': 'assistant', + 'content': [{'type': 'output_text', 'text': text}], + }) + # Convert each tool_call to a function_call input item + for tool_call in msg['tool_calls']: + func = tool_call.get('function', {}) + input_items.append({ + 'type': 'function_call', + 'call_id': tool_call.get('id', ''), + 'name': func.get('name', ''), + 'arguments': func.get('arguments', '{}'), + }) + continue + + # Handle tool result messages + if role == 'tool': + input_items.append({ + 'type': 'function_call_output', + 'call_id': msg.get('tool_call_id', ''), + 'output': msg.get('content', ''), + }) + continue + # Convert content format text_type = 'output_text' if role == 'assistant' else 'input_text' diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 94521d6ce0..c7a7576501 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -3887,6 +3887,23 @@ async def streaming_chat_response_handler(response, ctx): if response_tool_calls: tool_calls.append(_split_tool_calls(response_tool_calls)) + # Responses API path: extract function_call items from output + if not response_tool_calls and output: + responses_api_tool_calls = [] + for item in output: + if item.get('type') == 'function_call' and item.get('status') != 'completed': + arguments = item.get('arguments', '{}') + responses_api_tool_calls.append({ + 'id': item.get('call_id', ''), + 'index': len(responses_api_tool_calls), + 'function': { + 'name': item.get('name', ''), + 'arguments': arguments if isinstance(arguments, str) else json.dumps(arguments), + }, + }) + if responses_api_tool_calls: + tool_calls.append(_split_tool_calls(responses_api_tool_calls)) + if response.background: await response.background()