Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: show the OpenAI thinking summary once (#16841)

Nilaya21a6d69c62f
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index 4363b33970d..f22be214eb7 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -6301,6 +6301,9 @@ class ExternalProviderClient:
done_emitted = False
reasoning_open = False
reasoning_emitted = False
+ # .done / part.done repeat the deltas' text: show each part once, from whichever arrives first.
+ reasoning_shown_parts: set = set()
+ last_reasoning_part = None
# Give each function call its own delta.tool_calls[].index so parallel calls do not collide.
saw_function_call = False
function_call_index = 0
@@ -6694,7 +6697,16 @@ class ExternalProviderClient:
last_openai_reasoning_replay_item = (
_record_openai_reasoning_replay_item(item)
)
- summary_text = _extract_reasoning_text(item.get("summary"))
+ summary = item.get("summary")
+ summary_text = "\n\n".join(
+ filter(
+ None,
+ map(
+ _extract_reasoning_text,
+ summary if isinstance(summary, list) else [summary],
+ ),
+ )
+ )
if summary_text and not reasoning_emitted:
if not reasoning_open:
summary_text = f"<think>{summary_text}"
@@ -6887,8 +6899,21 @@ class ExternalProviderClient:
recorded_reasoning = _record_openai_reasoning_replay_item(event)
if recorded_reasoning:
last_openai_reasoning_replay_item = recorded_reasoning
- reasoning_delta = _extract_reasoning_text(event)
+ reasoning_part = (
+ event.get("item_id"),
+ event.get("summary_index", event.get("content_index")),
+ )
+ is_delta = event_type.endswith(".delta")
+ if not is_delta and reasoning_part in reasoning_shown_parts:
+ continue
+ reasoning_delta = _extract_reasoning_text(
+ event if is_delta else (event.get("part") or event)
+ )
if reasoning_delta:
+ reasoning_shown_parts.add(reasoning_part)
+ if reasoning_open and reasoning_part != last_reasoning_part:
+ reasoning_delta = f"\n\n{reasoning_delta}"
+ last_reasoning_part = reasoning_part
if not reasoning_open:
reasoning_delta = f"<think>{reasoning_delta}"
reasoning_open = True
diff --git a/studio/backend/tests/test_openai_responses_translation.py b/studio/backend/tests/test_openai_responses_translation.py
index a84d68a6cec..fed64396cf9 100644
--- a/studio/backend/tests/test_openai_responses_translation.py
+++ b/studio/backend/tests/test_openai_responses_translation.py
@@ -781,7 +781,10 @@ def test_responses_reasoning_summary_wrapped_in_think_tags(monkeypatch):
"type": "response.output_item.done",
"item": {
"type": "reasoning",
- "summary": [{"type": "summary_text", "text": "plan"}],
+ "summary": [
+ {"type": "summary_text", "text": "plan"},
+ {"type": "summary_text", "text": "act"},
+ ],
},
},
{"type": "response.output_text.delta", "delta": "answer"},
@@ -807,7 +810,79 @@ def test_responses_reasoning_summary_wrapped_in_think_tags(monkeypatch):
for payload in payloads
if payload["choices"][0]["delta"]
)
- assert "<think>plan</think>answer" in combined
+ assert "<think>plan\n\nact</think>answer" in combined
+
+
[email protected]("with_deltas", (True, False))
+def test_responses_streamed_reasoning_summary_shown_once(monkeypatch, with_deltas):
+ def summary_part(index: int, deltas: list[str]) -> list[dict]:
+ base = {"item_id": "rs_1", "output_index": 0, "summary_index": index}
+ return [
+ {
+ "type": "response.reasoning_summary_part.added",
+ **base,
+ "part": {"type": "summary_text", "text": ""},
+ },
+ *(
+ {"type": "response.reasoning_summary_text.delta", **base, "delta": d}
+ for d in (deltas if with_deltas else ())
+ ),
+ {
+ "type": "response.reasoning_summary_text.done",
+ **base,
+ "text": "".join(deltas),
+ },
+ {
+ "type": "response.reasoning_summary_part.done",
+ **base,
+ "part": {"type": "summary_text", "text": "".join(deltas)},
+ },
+ ]
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ events = [
+ {
+ "type": "response.output_item.added",
+ "output_index": 0,
+ "item": {"type": "reasoning", "id": "rs_1", "summary": []},
+ },
+ *summary_part(0, ["Thinking ", "about it."]),
+ *summary_part(1, ["Then ", "answering."]),
+ {
+ "type": "response.output_item.done",
+ "output_index": 0,
+ "item": {
+ "type": "reasoning",
+ "id": "rs_1",
+ "summary": [
+ {"type": "summary_text", "text": "Thinking about it."},
+ {"type": "summary_text", "text": "Then answering."},
+ ],
+ },
+ },
+ {"type": "response.output_text.delta", "delta": "Hello"},
+ {"type": "response.completed", "response": {}},
+ ]
+ return httpx.Response(
+ 200,
+ content = _responses_sse(events),
+ headers = {"content-type": "text/event-stream"},
+ )
+
+ _mock_http_client(monkeypatch, handler)
+
+ lines = _drive(run())
+ payloads = [
+ json.loads(line[len("data:") :].strip())
+ for line in lines
+ if line.startswith("data:") and line[len("data:") :].strip() not in ("", "[DONE]")
+ ]
+ combined = "".join(
+ payload["choices"][0]["delta"].get("content", "")
+ for payload in payloads
+ if payload["choices"][0]["delta"]
+ )
+ assert combined == "<think>Thinking about it.\n\nThen answering.</think>Hello"
@pytest.mark.parametrize(