Skip to content

Commit 0dc74a8

Browse files
committed
refac
1 parent 90a057f commit 0dc74a8

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,22 +2551,21 @@ def convert_content_blocks_to_output(content_blocks):
25512551
See: https://openresponses.org/specification
25522552
"""
25532553
output_items = []
2554-
item_counter = 0
25552554

25562555
def next_id(prefix):
2557-
nonlocal item_counter
2558-
item_counter += 1
2559-
return f"{prefix}_{item_counter}"
2556+
return f"{prefix}_{uuid4().hex[:24]}"
25602557

25612558
for block in content_blocks:
25622559
block_type = block.get("type", "")
2560+
# Use backend-provided ID if available, fallback to generated
2561+
block_id = block.get("id")
25632562

25642563
if block_type == "text":
25652564
text_content = block.get("content", "").strip()
25662565
if text_content:
25672566
output_items.append({
25682567
"type": "message",
2569-
"id": next_id("msg"),
2568+
"id": block_id or next_id("msg"),
25702569
"status": "completed",
25712570
"role": "assistant",
25722571
"content": [{"type": "output_text", "text": text_content}],
@@ -2582,7 +2581,7 @@ def next_id(prefix):
25822581
func = tool_call.get("function", {})
25832582
output_items.append({
25842583
"type": "function_call",
2585-
"id": next_id("fc"),
2584+
"id": call_id or next_id("fc"), # Use call_id as item id if available
25862585
"call_id": call_id,
25872586
"name": func.get("name", ""),
25882587
"arguments": func.get("arguments", "{}"),
@@ -2593,7 +2592,7 @@ def next_id(prefix):
25932592
for result in results:
25942593
output_items.append({
25952594
"type": "function_call_output",
2596-
"id": next_id("fco"),
2595+
"id": result.get("id") or next_id("fco"),
25972596
"call_id": result.get("tool_call_id", ""),
25982597
"output": [{"type": "input_text", "text": result.get("content", "")}],
25992598
"status": "completed",
@@ -2606,7 +2605,7 @@ def next_id(prefix):
26062605
duration = block.get("duration")
26072606
output_items.append({
26082607
"type": "reasoning",
2609-
"id": next_id("r"),
2608+
"id": block_id or next_id("r"),
26102609
"status": "completed" if duration is not None else "in_progress",
26112610
"content": [{"type": "output_text", "text": reasoning_content}] if reasoning_content else None,
26122611
"summary": None,
@@ -2618,7 +2617,7 @@ def next_id(prefix):
26182617
attrs = block.get("attributes", {})
26192618
output_items.append({
26202619
"type": "open_webui:code_interpreter",
2621-
"id": next_id("ci"),
2620+
"id": block_id or next_id("ci"),
26222621
"status": "completed" if output_val is not None else "in_progress",
26232622
"lang": attrs.get("lang", ""),
26242623
"code": code,

0 commit comments

Comments
 (0)