Skip to content

Commit adcbba3

Browse files
committed
refac
1 parent 218bd7a commit adcbba3

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

backend/open_webui/routers/openai.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,38 @@ def convert_to_responses_payload(payload: dict) -> dict:
799799
system_content = '\n'.join(p.get('text', '') for p in content if p.get('type') == 'text')
800800
continue
801801

802+
# Handle assistant messages with tool_calls (from convert_output_to_messages)
803+
if role == 'assistant' and msg.get('tool_calls'):
804+
# Add text content as message if present
805+
if content:
806+
text = content if isinstance(content, str) else '\n'.join(
807+
p.get('text', '') for p in content if p.get('type') == 'text'
808+
)
809+
if text.strip():
810+
input_items.append({
811+
'type': 'message', 'role': 'assistant',
812+
'content': [{'type': 'output_text', 'text': text}],
813+
})
814+
# Convert each tool_call to a function_call input item
815+
for tool_call in msg['tool_calls']:
816+
func = tool_call.get('function', {})
817+
input_items.append({
818+
'type': 'function_call',
819+
'call_id': tool_call.get('id', ''),
820+
'name': func.get('name', ''),
821+
'arguments': func.get('arguments', '{}'),
822+
})
823+
continue
824+
825+
# Handle tool result messages
826+
if role == 'tool':
827+
input_items.append({
828+
'type': 'function_call_output',
829+
'call_id': msg.get('tool_call_id', ''),
830+
'output': msg.get('content', ''),
831+
})
832+
continue
833+
802834
# Convert content format
803835
text_type = 'output_text' if role == 'assistant' else 'input_text'
804836

backend/open_webui/utils/middleware.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,6 +3887,23 @@ async def flush_pending_delta_data(threshold: int = 0):
38873887
if response_tool_calls:
38883888
tool_calls.append(_split_tool_calls(response_tool_calls))
38893889

3890+
# Responses API path: extract function_call items from output
3891+
if not response_tool_calls and output:
3892+
responses_api_tool_calls = []
3893+
for item in output:
3894+
if item.get('type') == 'function_call' and item.get('status') != 'completed':
3895+
arguments = item.get('arguments', '{}')
3896+
responses_api_tool_calls.append({
3897+
'id': item.get('call_id', ''),
3898+
'index': len(responses_api_tool_calls),
3899+
'function': {
3900+
'name': item.get('name', ''),
3901+
'arguments': arguments if isinstance(arguments, str) else json.dumps(arguments),
3902+
},
3903+
})
3904+
if responses_api_tool_calls:
3905+
tool_calls.append(_split_tool_calls(responses_api_tool_calls))
3906+
38903907
if response.background:
38913908
await response.background()
38923909

0 commit comments

Comments
 (0)