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.

Honor the Responses API text.format structured output (#8846)

Nilayc8648e149b92
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index a338161499d..cd4730fb7a0 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -17831,6 +17831,24 @@ def _normalise_responses_input(payload: ResponsesRequest) -> list[ChatMessage]:
return _with_system(messages)
+def _responses_text_format(text: Any) -> Optional[dict]:
+ """Responses ``text.format`` -> Chat Completions ``response_format``.
+
+ ``{"type": "text"}`` and a verbosity-only ``text`` carry no constraint -> None.
+ """
+ fmt = text.get("format") if isinstance(text, dict) else None
+ if not isinstance(fmt, dict):
+ return None
+ if fmt.get("type") == "json_object":
+ return {"type": "json_object"}
+ if fmt.get("type") != "json_schema" or not isinstance(fmt.get("schema"), dict):
+ return None
+ json_schema = {"name": str(fmt.get("name") or "response"), "schema": fmt["schema"]}
+ if fmt.get("strict") is not None:
+ json_schema["strict"] = bool(fmt["strict"])
+ return {"type": "json_schema", "json_schema": json_schema}
+
+
def _build_chat_request(
payload: ResponsesRequest, messages: list[ChatMessage], stream: bool
) -> ChatCompletionRequest:
@@ -17903,6 +17921,11 @@ def _build_chat_request(
elif effort != "none":
chat_kwargs["reasoning_effort"] = effort
+ response_format = _responses_text_format(payload.text)
+ if response_format is not None:
+ # Lands in model_extra, where _extract_response_format reads it.
+ chat_kwargs["response_format"] = response_format
+
return ChatCompletionRequest(**chat_kwargs)
diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.py
index aff1198e23c..ea434e98551 100644
--- a/studio/backend/tests/test_responses_tool_passthrough.py
+++ b/studio/backend/tests/test_responses_tool_passthrough.py
@@ -63,6 +63,7 @@ from routes.inference import (
_SameTaskStreamingResponse,
_build_chat_request,
_chat_tool_calls_to_responses_output,
+ _extract_response_format,
_extract_responses_reasoning,
_normalise_responses_input,
_responses_tool_output_content,
@@ -292,6 +293,62 @@ class TestBuildChatRequest:
assert chat_req.parallel_tool_calls is False
+ def test_text_format_json_schema_becomes_response_format(self):
+ schema = {"type": "object", "properties": {"name": {"type": "string"}}}
+ payload = ResponsesRequest(
+ input = "hi",
+ text = {
+ "format": {
+ "type": "json_schema",
+ "name": "Person",
+ "schema": schema,
+ "strict": True,
+ }
+ },
+ )
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ chat_req = _build_chat_request(payload, messages, stream = False)
+
+ assert _extract_response_format(chat_req) == {
+ "type": "json_schema",
+ "json_schema": {"name": "Person", "schema": schema, "strict": True},
+ }
+
+ def test_text_format_json_object_becomes_response_format(self):
+ payload = ResponsesRequest(input = "hi", text = {"format": {"type": "json_object"}})
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ chat_req = _build_chat_request(payload, messages, stream = False)
+
+ assert _extract_response_format(chat_req) == {"type": "json_object"}
+
+ def test_text_format_text_carries_no_response_format(self):
+ # Codex and the Agents SDK send this on ordinary requests; constraining
+ # them would route every call onto the schema path.
+ payload = ResponsesRequest(input = "hi", text = {"format": {"type": "text"}})
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ chat_req = _build_chat_request(payload, messages, stream = False)
+
+ assert _extract_response_format(chat_req) is None
+
+ def test_text_verbosity_only_carries_no_response_format(self):
+ payload = ResponsesRequest(input = "hi", text = {"verbosity": "low"})
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ chat_req = _build_chat_request(payload, messages, stream = False)
+
+ assert _extract_response_format(chat_req) is None
+
+ def test_text_format_json_schema_without_schema_is_ignored(self):
+ payload = ResponsesRequest(input = "hi", text = {"format": {"type": "json_schema"}})
+ messages = [ChatMessage(role = "user", content = "hi")]
+
+ chat_req = _build_chat_request(payload, messages, stream = False)
+
+ assert _extract_response_format(chat_req) is None
+
def test_chat_template_kwargs_enable_thinking_true_is_lifted(self):
payload = ResponsesRequest(
input = "hi",