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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: Keep Codex reasoning between tool calls on /v1/responses (#11726)
Nilay346df2beed71
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex 4473b6c5f04..56319b1daa5 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -3117,8 +3117,8 @@ class ResponsesCustomToolCallOutputInputItem(BaseModel):class ResponsesUnknownInputItem(BaseModel):"""Catch-all for unmodelled Responses input item types.- Covers ``reasoning`` items and future types. Dropped during normalisation- (GGUFs can't consume them), but kept in the union so unrelated turns don't 422.+ Covers ``reasoning`` items and future types, so unrelated turns don't 422.+ Normalisation replays reasoning text and drops every other unknown item."""type: strdiff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 2f24fec742e..f9d0b4d4c48 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -31432,13 +31432,17 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:- ``ResponsesInputMessage`` -- regular chat messages (text or multimodal).- ``ResponsesFunctionCallInputItem`` -- a prior assistant tool call- replayed on a follow-up turn. Becomes an assistant message carrying a- Chat Completions ``tool_calls`` entry keyed by ``call_id``.+ replayed on a follow-up turn. Becomes a Chat Completions ``tool_calls``+ entry keyed by ``call_id``.- ``ResponsesFunctionCallOutputInputItem`` -- a tool result the client isreturning. Becomes a ``role="tool"`` message with ``tool_call_id`` set tothe originating ``call_id`` so llama-server can reconcile call with result.- ``ResponsesCustomToolCallInputItem`` and its output counterpart -- thefreeform call is wrapped in the local ``input`` function argument.+ - ``reasoning`` items -- their text becomes ``reasoning_content``.++ Each contiguous run of reasoning, assistant message and call items folds+ into one assistant message, as the model produced it.System / developer content is collected from ``instructions`` *and* any``role="system"`` / ``role="developer"`` entries in ``input``, then merged@@ -31474,24 +31478,43 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:if isinstance(item, ResponsesCustomToolCallInputItem) and item.name in custom_tool_names}- for item in payload.input:- if isinstance(item, ResponsesFunctionCallInputItem):+ turn_reasoning: list[str] = []+ turn_text: list[str] = []+ turn_calls: list[dict] = []++ def _flush_assistant_turn() -> None:+ if turn_text or turn_calls:messages.append(ChatMessage(role = "assistant",- content = None,- tool_calls = [- {- "id": item.call_id,- "type": "function",- "function": {- "name": item.name,- "arguments": item.arguments,- },- }- ],+ content = "\n\n".join(turn_text) or None,+ reasoning_content = "\n\n".join(turn_reasoning) or None,+ tool_calls = list(turn_calls) or None,))+ turn_reasoning.clear()+ turn_text.clear()+ turn_calls.clear()++ for item in payload.input:+ if not (+ isinstance(item, (ResponsesFunctionCallInputItem, ResponsesCustomToolCallInputItem))+ or getattr(item, "role", None) == "assistant"+ or getattr(item, "type", None) == "reasoning"+ ):+ _flush_assistant_turn()++ if isinstance(item, ResponsesFunctionCallInputItem):+ turn_calls.append(+ {+ "id": item.call_id,+ "type": "function",+ "function": {+ "name": item.name,+ "arguments": item.arguments,+ },+ }+ )continueif isinstance(item, ResponsesFunctionCallOutputInputItem):@@ -31510,21 +31533,15 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:if isinstance(item, ResponsesCustomToolCallInputItem):if item.call_id not in custom_tool_call_ids:continue- messages.append(- ChatMessage(- role = "assistant",- content = None,- tool_calls = [- {- "id": item.call_id,- "type": "function",- "function": {- "name": item.name,- "arguments": json.dumps({"input": item.input}, ensure_ascii = False),- },- }- ],- )+ turn_calls.append(+ {+ "id": item.call_id,+ "type": "function",+ "function": {+ "name": item.name,+ "arguments": json.dumps({"input": item.input}, ensure_ascii = False),+ },+ })continue@@ -31541,10 +31558,12 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:continueif isinstance(item, ResponsesUnknownInputItem):- # Reasoning items and other unmodelled top-level Responses item- # types are silently dropped -- llama-server-backed GGUFs can't- # consume them; lenient validation lets them in so unrelated turns- # don't 422.+ if item.type == "reasoning":+ replayed = _coerce_responses_reasoning_text(+ getattr(item, "content", None)+ ) or _coerce_responses_reasoning_text(getattr(item, "summary", None))+ if replayed.strip():+ turn_reasoning.append(replayed)continue# ResponsesInputMessage. Before the role branches: each returns via `continue`, so a@@ -31558,18 +31577,18 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:system_parts.append(hoisted)continue- if isinstance(item.content, str):- messages.append(ChatMessage(role = item.role, content = item.content))- continue-# Assistant-replay turns come back as content = [output_text, ...].# Chat Completions' assistant role expects a plain string, not a# multimodal array, so flatten output_text (and any stray input_text /# unknown text) to a single string.if item.role == "assistant":- text = _responses_message_text(item.content)- if text:- messages.append(ChatMessage(role = "assistant", content = text))+ message_text = _responses_message_text(item.content)+ if message_text:+ turn_text.append(message_text)+ continue++ if isinstance(item.content, str):+ messages.append(ChatMessage(role = item.role, content = item.content))continue# User (and any other remaining roles). Attachments were refused above, so a part@@ -31595,6 +31614,7 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:else:messages.append(ChatMessage(role = item.role, content = parts))+ _flush_assistant_turn()return _with_system(messages)diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.pyindex 5698479114d..38f478e17fe 100644--- a/studio/backend/tests/test_responses_tool_passthrough.py+++ b/studio/backend/tests/test_responses_tool_passthrough.py@@ -3060,14 +3060,14 @@ class TestCodexStyleRequestShapes:assert len(req.input) == 3assert isinstance(req.input[1], ResponsesUnknownInputItem)- def test_emitted_reasoning_item_replay_is_dropped_for_local_chat(self):+ def test_emitted_reasoning_item_replays_as_reasoning_content(self):payload = ResponsesRequest(input = [{"role": "user", "content": "Hi"},{"type": "reasoning","id": "rs_1",- "summary": [],+ "summary": [{"type": "summary_text", "text": "summary"}],"content": [{"type": "reasoning_text", "text": "plan"}],},{"role": "assistant", "content": "33"},@@ -3078,7 +3078,90 @@ class TestCodexStyleRequestShapes:msgs = _normalise_responses_input(payload)assert [m.role for m in msgs] == ["user", "assistant", "user"]- assert all("plan" not in (m.content or "") for m in msgs if isinstance(m.content, str))+ assert msgs[1].content == "33"+ assert msgs[1].reasoning_content == "plan"++ def test_codex_parallel_calls_replay_as_one_turn_with_reasoning(self):+ payload = ResponsesRequest(+ store = False,+ input = [+ {"type": "message", "role": "user", "content": "list files then read README"},+ {+ "type": "reasoning",+ "id": "rs_1",+ "summary": [],+ "content": [{"type": "reasoning_text", "text": "PLAN: ls and cat"}],+ "encrypted_content": None,+ },+ {+ "type": "function_call",+ "call_id": "c1",+ "name": "shell",+ "arguments": '{"cmd":"ls"}',+ },+ {+ "type": "function_call",+ "call_id": "c2",+ "name": "shell",+ "arguments": '{"cmd":"cat"}',+ },+ {"type": "function_call_output", "call_id": "c1", "output": "README"},+ {"type": "function_call_output", "call_id": "c2", "output": "hello"},+ ],+ tools = [{"type": "function", "name": "shell", "parameters": {"type": "object"}}],+ )++ msgs = _normalise_responses_input(payload)++ assert [m.role for m in msgs] == ["user", "assistant", "tool", "tool"]+ assert msgs[1].content is None+ assert msgs[1].reasoning_content == "PLAN: ls and cat"+ assert [c["id"] for c in msgs[1].tool_calls] == ["c1", "c2"]+ body = _build_openai_passthrough_body(+ _build_chat_request(payload, msgs, stream = True), backend_ctx = 4096+ )+ assert [m.get("reasoning_content") for m in body["messages"]] == [+ None,+ "PLAN: ls and cat",+ None,+ None,+ ]++ def test_each_turn_keeps_its_own_text_reasoning_and_calls(self):+ payload = ResponsesRequest(+ input = [+ {"role": "user", "content": "fix it"},+ {+ "type": "reasoning",+ "summary": [{"type": "summary_text", "text": "read first"}],+ },+ {+ "type": "message",+ "role": "assistant",+ "content": [{"type": "output_text", "text": "Reading."}],+ },+ {"type": "function_call", "call_id": "c1", "name": "shell", "arguments": "{}"},+ {"type": "function_call_output", "call_id": "c1", "output": "code"},+ {"type": "reasoning", "summary": [], "encrypted_content": "opaque"},+ {"type": "custom_tool_call", "call_id": "c2", "name": "apply_patch", "input": "p"},+ {"type": "custom_tool_call_output", "call_id": "c2", "output": "Done!"},+ {+ "type": "reasoning",+ "summary": [],+ "content": [{"type": "reasoning_text", "text": "ok"}],+ },+ {"role": "user", "content": "thanks"},+ ],+ tools = [_codex_apply_patch_tool()],+ )++ msgs = _normalise_responses_input(payload)++ assert [m.role for m in msgs] == ["user", "assistant", "tool", "assistant", "tool", "user"]+ assert (msgs[1].content, msgs[1].reasoning_content) == ("Reading.", "read first")+ assert [c["id"] for c in msgs[1].tool_calls] == ["c1"]+ assert (msgs[3].content, msgs[3].reasoning_content) == (None, None)+ assert [c["id"] for c in msgs[3].tool_calls] == ["c2"]def test_unknown_content_part_type_accepted(self):"""Unknown content-part types (e.g. future input_audio) validate as