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: report a cut off /v1/responses turn as incomplete (#10225)

Nilay2c11a2752c48
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 34ccb599d7a..6050ae8eab0 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3106,7 +3106,7 @@ class ResponsesOutputMessage(BaseModel):
type: Literal["message"] = "message"
id: str = Field(default_factory = lambda: f"msg_{uuid.uuid4().hex[:12]}")
- status: Literal["completed", "in_progress"] = "completed"
+ status: Literal["completed", "in_progress", "incomplete"] = "completed"
role: Literal["assistant"] = "assistant"
content: list[ResponsesOutputTextContent] = Field(default_factory = list)
@@ -3175,7 +3175,7 @@ class ResponsesResponse(BaseModel):
id: str = Field(default_factory = lambda: f"resp_{uuid.uuid4().hex[:12]}")
object: Literal["response"] = "response"
created_at: int = Field(default_factory = lambda: int(time.time()))
- status: Literal["completed", "in_progress", "failed"] = "completed"
+ status: Literal["completed", "in_progress", "incomplete", "failed"] = "completed"
model: str = "default"
output: list[ResponsesOutputItem] = Field(default_factory = list)
usage: ResponsesUsage = Field(default_factory = ResponsesUsage)
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 553439cf7ac..afb81f923c5 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -26245,9 +26245,13 @@ def _responses_should_parse_think_markers(
return chat_req.enable_thinking is None and chat_req.reasoning_effort not in (None, "none")
-def _responses_reasoning_output_item(reasoning_text: str, item_id: Optional[str] = None) -> dict:
+def _responses_reasoning_output_item(
+ reasoning_text: str,
+ item_id: Optional[str] = None,
+ status: Literal["completed", "incomplete"] = "completed",
+) -> dict:
kwargs: dict[str, Any] = {
- "status": "completed",
+ "status": status,
"summary": [],
"content": [ResponsesOutputReasoningContent(text = reasoning_text)],
}
@@ -26532,7 +26536,9 @@ def _responses_custom_tool_input(arguments: Any) -> str:
def _chat_tool_calls_to_responses_output(
- tool_calls: list[dict], custom_tool_names: Optional[set[str]] = None
+ tool_calls: list[dict],
+ custom_tool_names: Optional[set[str]] = None,
+ status: Literal["completed", "incomplete"] = "completed",
) -> list[dict]:
"""Map local function calls back into their Responses output item types.
@@ -26554,7 +26560,7 @@ def _chat_tool_calls_to_responses_output(
call_id = tc.get("id", ""),
name = name,
input = _responses_custom_tool_input(arguments),
- status = "completed",
+ status = status,
).model_dump()
)
continue
@@ -26563,12 +26569,22 @@ def _chat_tool_calls_to_responses_output(
call_id = tc.get("id", ""),
name = name,
arguments = arguments,
- status = "completed",
+ status = status,
).model_dump()
)
return items
+def _responses_incomplete_reason(
+ finish_reason: Any,
+) -> Optional[Literal["max_output_tokens", "content_filter"]]:
+ if finish_reason == "length":
+ return "max_output_tokens"
+ if finish_reason == "content_filter":
+ return "content_filter"
+ return None
+
+
async def _responses_non_streaming(
payload: ResponsesRequest,
messages: list[ChatMessage],
@@ -26625,6 +26641,10 @@ async def _responses_non_streaming(
text = ""
reasoning_text = ""
tool_calls: list[dict] = []
+ incomplete_reason = (
+ _responses_incomplete_reason(choices[0].get("finish_reason")) if choices else None
+ )
+ truncated = incomplete_reason is not None
if choices:
msg = choices[0].get("message", {}) or {}
raw_content = msg.get("content", "") or ""
@@ -26648,28 +26668,34 @@ async def _responses_non_streaming(
# the model produced content, so clients expecting a pure tool-call turn
# (finish_reason="tool_calls") don't see a spurious empty message item.
output_items: list[dict] = []
+ item_status = "incomplete" if truncated else "completed"
if reasoning_text:
- output_items.append(_responses_reasoning_output_item(reasoning_text))
+ output_items.append(
+ _responses_reasoning_output_item(reasoning_text, status = item_status)
+ )
if text:
msg_id = f"msg_{uuid.uuid4().hex[:12]}"
output_items.append(
ResponsesOutputMessage(
id = msg_id,
- status = "completed",
+ status = item_status,
role = "assistant",
content = [ResponsesOutputTextContent(text = text)],
).model_dump()
)
output_items.extend(
_chat_tool_calls_to_responses_output(
- tool_calls, _responses_custom_tool_names(payload.tools)
+ tool_calls,
+ _responses_custom_tool_names(payload.tools),
+ status = item_status,
)
)
response = ResponsesResponse(
id = resp_id,
created_at = int(time.time()),
- status = "completed",
+ status = "incomplete" if truncated else "completed",
+ incomplete_details = ({"reason": incomplete_reason} if truncated else None),
model = body.get("model", payload.model),
output = output_items,
usage = ResponsesUsage(
@@ -27136,8 +27162,8 @@ async def _responses_stream(
out.extend(_tool_call_delta_events(tc))
return out
- def _snapshot_output() -> list[dict]:
- """Snapshot of all completed output items for response.completed."""
+ def _snapshot_output(active_item_status: str = "completed") -> list[dict]:
+ """Snapshot output items using the response's terminal state."""
indexed_items: list[tuple[int, dict]] = []
if reasoning_state["opened"]:
indexed_items.append(
@@ -27146,7 +27172,7 @@ async def _responses_stream(
{
"type": "reasoning",
"id": reasoning_state["item_id"],
- "status": "completed",
+ "status": active_item_status,
"summary": [],
"content": [{"type": "reasoning_text", "text": full_reasoning}],
},
@@ -27163,7 +27189,9 @@ async def _responses_stream(
{
"type": "message",
"id": msg_st["item_id"],
- "status": "completed",
+ "status": (
+ "completed" if msg_st is not message_state else active_item_status
+ ),
"role": "assistant",
"content": [
{
@@ -27179,7 +27207,7 @@ async def _responses_stream(
if st["name"] in custom_tool_names:
item = {
"type": "custom_tool_call",
- "status": "completed",
+ "status": active_item_status,
"call_id": st["call_id"],
"name": st["name"],
"input": _responses_custom_tool_input(st["arguments"]),
@@ -27188,7 +27216,7 @@ async def _responses_stream(
item = {
"type": "function_call",
"id": st["item_id"],
- "status": "completed",
+ "status": active_item_status,
"call_id": st["call_id"],
"name": st["name"],
"arguments": st["arguments"],
@@ -27540,6 +27568,15 @@ async def _responses_stream(
},
)
+ # Healing changes a natural stop into tool_calls, but it must not erase
+ # an authoritative truncation or failure reason from the upstream model.
+ if healer is not None and healer.healed and stream_finish_reason in (None, "stop"):
+ stream_finish_reason = "tool_calls"
+
+ incomplete_reason = _responses_incomplete_reason(stream_finish_reason)
+ truncated = incomplete_reason is not None
+ active_item_status = "incomplete" if truncated else "completed"
+
close_items: list[tuple[int, str, dict[str, Any]]] = []
if reasoning_state["opened"]:
close_items.append((reasoning_state["output_index"], "reasoning", reasoning_state))
@@ -27577,7 +27614,7 @@ async def _responses_stream(
"item": {
"type": "reasoning",
"id": st["item_id"],
- "status": "completed",
+ "status": active_item_status,
"summary": [],
"content": [{"type": "reasoning_text", "text": full_reasoning}],
},
@@ -27618,7 +27655,7 @@ async def _responses_stream(
"item": {
"type": "message",
"id": st["item_id"],
- "status": "completed",
+ "status": active_item_status,
"role": "assistant",
"content": [
{"type": "output_text", "text": _msg_text, "annotations": []}
@@ -27637,7 +27674,7 @@ async def _responses_stream(
"output_index": st["output_index"],
"item": {
"type": "custom_tool_call",
- "status": "completed",
+ "status": active_item_status,
"call_id": st["call_id"],
"name": st["name"],
"input": custom_input,
@@ -27692,7 +27729,7 @@ async def _responses_stream(
"item": {
"type": "function_call",
"id": st["item_id"],
- "status": "completed",
+ "status": active_item_status,
"call_id": st["call_id"],
"name": st["name"],
"arguments": st["arguments"],
@@ -27701,17 +27738,18 @@ async def _responses_stream(
api_monitor.append_reply(monitor_id, _monitor_call_text(st["name"], st["arguments"]))
yield _sse("response.output_item.done", item_done)
- # response.completed
total_tokens = input_tokens + output_tokens
- completed_response = {
- "type": "response.completed",
+ terminal_event = "response.incomplete" if truncated else "response.completed"
+ terminal_response = {
+ "type": terminal_event,
"response": {
"id": resp_id,
"object": "response",
"created_at": created_at,
- "status": "completed",
+ "status": "incomplete" if truncated else "completed",
+ "incomplete_details": ({"reason": incomplete_reason} if truncated else None),
"model": _clean_model,
- "output": _snapshot_output(),
+ "output": _snapshot_output(active_item_status),
"usage": {
"input_tokens": input_tokens,
"output_tokens": output_tokens,
@@ -27719,15 +27757,10 @@ async def _responses_stream(
},
},
}
- # A healed call reaches the client as a function_call item while the upstream chunk
- # still says "stop", so report what this adapter emitted -- same rule the chat
- # stream's synthetic finish line applies.
- if healer is not None and healer.healed:
- stream_finish_reason = "tool_calls"
if stream_finish_reason:
api_monitor.set_perf(monitor_id, stop_reason = stream_finish_reason)
api_monitor.finish(monitor_id)
- yield _sse("response.completed", completed_response)
+ yield _sse(terminal_event, terminal_response)
async def admitted_event_generator():
# Register for the body's whole lifetime, admission wait included: the run holds a decode
diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.py
index 05b517b3ff3..45c1dfc53ea 100644
--- a/studio/backend/tests/test_responses_tool_passthrough.py
+++ b/studio/backend/tests/test_responses_tool_passthrough.py
@@ -1028,6 +1028,7 @@ class TestResponsesNonStreamingAdapter:
message,
payload = None,
llama_backend = None,
+ finish_reason = None,
):
import routes.inference as inf_mod
@@ -1039,7 +1040,7 @@ class TestResponsesNonStreamingAdapter:
return JSONResponse(
content = {
"model": "test-model",
- "choices": [{"message": message}],
+ "choices": [{"message": message, "finish_reason": finish_reason}],
"usage": {"prompt_tokens": 2, "completion_tokens": 3},
}
)
@@ -1058,6 +1059,52 @@ class TestResponsesNonStreamingAdapter:
return asyncio.run(run())
+ def test_a_truncated_turn_is_reported_as_incomplete(self, monkeypatch):
+ body = self._run_with_message(
+ monkeypatch,
+ {
+ "content": "half an ans",
+ "reasoning_content": "partial plan",
+ "tool_calls": [
+ {
+ "id": "call_partial",
+ "type": "function",
+ "function": {"name": "lookup", "arguments": '{"q":"x"'},
+ }
+ ],
+ },
+ finish_reason = "length",
+ )
+
+ assert body["status"] == "incomplete"
+ assert body["incomplete_details"] == {"reason": "max_output_tokens"}
+ assert [item["status"] for item in body["output"]] == [
+ "incomplete",
+ "incomplete",
+ "incomplete",
+ ]
+
+ def test_a_natural_stop_is_still_completed(self, monkeypatch):
+ body = self._run_with_message(
+ monkeypatch,
+ {"content": "a whole answer"},
+ finish_reason = "stop",
+ )
+
+ assert body["status"] == "completed"
+ assert body["incomplete_details"] is None
+
+ def test_a_content_filtered_turn_is_reported_as_incomplete(self, monkeypatch):
+ body = self._run_with_message(
+ monkeypatch,
+ {"content": "filtered partial output"},
+ finish_reason = "content_filter",
+ )
+
+ assert body["status"] == "incomplete"
+ assert body["incomplete_details"] == {"reason": "content_filter"}
+ assert [item["status"] for item in body["output"]] == ["incomplete"]
+
def test_think_block_becomes_reasoning_item_before_message(self, monkeypatch):
payload = ResponsesRequest(input = "hi", reasoning = {"effort": "high"})
body = self._run_with_message(
@@ -3031,3 +3078,93 @@ def test_unhealed_responses_stream_keeps_the_upstream_stop(monkeypatch):
row = next(r for r in api_monitor.snapshot() if r["id"] == monitor_id)
assert row["stop_reason"] == "stop"
+
+
+ ("finish_reason", "incomplete_reason"),
+ [("length", "max_output_tokens"), ("content_filter", "content_filter")],
+)
+def test_a_truncated_responses_stream_ends_on_response_incomplete(
+ monkeypatch, finish_reason, incomplete_reason
+):
+ TestResponsesStreamAdapter._install_stream_mock(
+ monkeypatch,
+ [{"choices": [{"delta": {"content": "half an ans"}, "finish_reason": finish_reason}]}],
+ )
+ payload = ResponsesRequest(input = "hi", stream = True)
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ async def run():
+ response = await _responses_stream(payload, messages, TestResponsesStreamAdapter._Request())
+ return await TestResponsesStreamAdapter._collect(response)
+
+ lines = asyncio.run(run())
+
+ assert TestResponsesStreamAdapter._payloads(lines, "response.completed") == []
+ incomplete = TestResponsesStreamAdapter._payloads(lines, "response.incomplete")[0]
+ assert incomplete["response"]["status"] == "incomplete"
+ assert incomplete["response"]["incomplete_details"] == {"reason": incomplete_reason}
+ assert [item["status"] for item in incomplete["response"]["output"]] == ["incomplete"]
+ item_done = TestResponsesStreamAdapter._payloads(lines, "response.output_item.done")[0]
+ assert item_done["item"]["status"] == "incomplete"
+
+
+ ("finish_reason", "incomplete_reason"),
+ [("length", "max_output_tokens"), ("content_filter", "content_filter")],
+)
+def test_a_healed_truncated_tool_call_remains_incomplete(
+ monkeypatch, finish_reason, incomplete_reason
+):
+ from core.inference.api_monitor import api_monitor
+
+ xml = TestResponsesStreamHealing._XML
+ tool = TestResponsesStreamHealing._TOOL
+ TestResponsesStreamAdapter._install_stream_mock(
+ monkeypatch,
+ [{"choices": [{"delta": {"content": xml}, "finish_reason": finish_reason}]}],
+ )
+ payload = ResponsesRequest(input = "hi", stream = True, tools = [tool])
+ messages = [ChatMessage(role = "user", content = "hi")]
+ monitor_id = api_monitor.start(
+ endpoint = "/v1/responses", method = "POST", model = "org/M-GGUF", prompt = "hi"
+ )
+
+ async def run():
+ response = await _responses_stream(
+ payload, messages, TestResponsesStreamAdapter._Request(), monitor_id
+ )
+ return await TestResponsesStreamAdapter._collect(response)
+
+ lines = asyncio.run(run())
+
+ assert TestResponsesStreamAdapter._payloads(lines, "response.completed") == []
+ incomplete = TestResponsesStreamAdapter._payloads(lines, "response.incomplete")[0]
+ assert incomplete["response"]["status"] == "incomplete"
+ assert incomplete["response"]["incomplete_details"] == {"reason": incomplete_reason}
+ assert [item["status"] for item in incomplete["response"]["output"]] == ["incomplete"]
+ row = next(r for r in api_monitor.snapshot() if r["id"] == monitor_id)
+ assert row["stop_reason"] == finish_reason
+ item_done = TestResponsesStreamAdapter._payloads(lines, "response.output_item.done")[0]
+ assert item_done["item"]["status"] == "incomplete"
+
+
+def test_a_complete_responses_stream_still_ends_on_response_completed(monkeypatch):
+ TestResponsesStreamAdapter._install_stream_mock(
+ monkeypatch,
+ [{"choices": [{"delta": {"content": "a whole answer"}, "finish_reason": "stop"}]}],
+ )
+ payload = ResponsesRequest(input = "hi", stream = True)
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ async def run():
+ response = await _responses_stream(payload, messages, TestResponsesStreamAdapter._Request())
+ return await TestResponsesStreamAdapter._collect(response)
+
+ lines = asyncio.run(run())
+
+ assert TestResponsesStreamAdapter._payloads(lines, "response.incomplete") == []
+ completed = TestResponsesStreamAdapter._payloads(lines, "response.completed")[0]
+ assert completed["response"]["status"] == "completed"
+ assert completed["response"]["incomplete_details"] is None
+ assert [item["status"] for item in completed["response"]["output"]] == ["completed"]