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: add responses api selection for custom providers (#11354)

Maheswar Kumar27c1f07ba934
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index 5eaea65e7ac..7a078ba3688 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -26,6 +26,7 @@ from core.inference.openai_responses_shared import (
normalize_function_schema,
responses_function_call,
responses_function_output,
+ responses_usage_to_chat,
response_event_type,
)
from core.inference.sse_control_frames import sanitize_provider_sse_line
@@ -137,12 +138,17 @@ def _append_provider_path(base_url: str, endpoint: str) -> str:
return urlunsplit((parts.scheme, parts.netloc, path, parts.query, parts.fragment))
+def _is_azure_openai_host(host: str) -> bool:
+ return host.endswith((".openai.azure.com", ".services.ai.azure.com"))
+
+
def _is_openai_family_cloud(base_url: Optional[str]) -> bool:
"""True iff ``base_url`` points at OpenAI cloud or Azure OpenAI Foundry. Anchored to the URL
host so a path/subdomain like ``https://api.openai.com.attacker.com/v1`` cannot bypass it
(CodeQL py/incomplete-url-substring-sanitization). Scopes cloud-only Responses-API extensions
that 400 on non-cloud OAI-compat servers. Azure Foundry resources live at
- ``<resource>.openai.azure.com``; the leading dot on `endswith` stops the apex from matching."""
+ ``<resource>.openai.azure.com`` and ``<resource>.services.ai.azure.com``; the leading
+ dots on `endswith` stop the apexes from matching."""
if not base_url:
return False
try:
@@ -151,7 +157,7 @@ def _is_openai_family_cloud(base_url: Optional[str]) -> bool:
return False
if not host:
return False
- return host == "api.openai.com" or host.endswith(".openai.azure.com")
+ return host == "api.openai.com" or _is_azure_openai_host(host)
# Claude Opus 4.7 and every Claude 5 family removed temperature/top_p/top_k, as did Mythos Preview; the API 400s with
@@ -164,6 +170,20 @@ _ANTHROPIC_MODEL_VERSION = re.compile(
re.IGNORECASE,
)
_OPENAI_REASONING_SUMMARY_UNSUPPORTED = re.compile(r"^o3(?:[-.]|$)")
+_OPENAI_FIXED_SAMPLING_MODEL = re.compile(
+ r"^(?:gpt-5(?:[.-]|$)|gpt-4\.5(?:[.-]|$)|o\d+(?:[.-]|$)|codex-mini(?:[.-]|$)|gpt-6-astra(?:[.-]|$))"
+)
+_OPENAI_NON_REASONING_CHAT_ALIAS = re.compile(r"-chat(?:-latest)?$")
+
+
+def _openai_fixed_sampling_model(model: str) -> bool:
+ normalized = model.strip().lower()
+ return bool(
+ _OPENAI_FIXED_SAMPLING_MODEL.match(normalized)
+ and not _OPENAI_NON_REASONING_CHAT_ALIAS.search(normalized)
+ )
+
+
# Gemini 3.x, dotted minor optional: gemini-3-, gemini-3.1-, gemini-3.6- ...
_GEMINI3_FAMILY = re.compile(r"^gemini-(?:[3-9]|\d{2,})(?:\.\d+)?-")
_GEMINI3_PRO = re.compile(r"^gemini-(?:[3-9]|\d{2,})(?:\.\d+)?-pro")
@@ -224,6 +244,15 @@ def _openai_response_error_message(event: Any) -> str:
return f"OpenAI response failed without error details{suffix}."
+def _openai_response_finish_reason(response: Any, *, has_tool_calls: bool = False) -> str:
+ """Map a completed Responses object to the closest Chat Completions finish reason."""
+ if isinstance(response, dict) and response.get("status") == "incomplete":
+ details = response.get("incomplete_details")
+ reason = details.get("reason") if isinstance(details, dict) else None
+ return "content_filter" if reason == "content_filter" else "length"
+ return "tool_calls" if has_tool_calls else "stop"
+
+
def _openai_image_replay_requires_reasoning(model: str) -> bool:
normalized = model.strip().lower()
return normalized.startswith("gpt-5") or normalized.startswith("o")
@@ -371,6 +400,47 @@ def _split_pending_citation_tail(text: str) -> tuple[str, str]:
return text[:last_open], text[last_open:]
+def _record_openai_url_citation(
+ url_citations: list[dict[str, Any]], payload: dict[str, Any]
+) -> None:
+ """Normalize and append one Responses ``url_citation`` annotation.
+
+ API revisions have used ``source_id``, ``id``, ``locator``, and
+ ``source_ids`` for the marker aliases. Citations sharing a URL retain one
+ display index while accumulating every alias that can reference it.
+ """
+ if payload.get("type") != "url_citation":
+ return
+ url = payload.get("url")
+ if not isinstance(url, str) or not url:
+ return
+ aliases: list[str] = []
+ source_id = payload.get("source_id") or payload.get("id") or payload.get("locator")
+ if isinstance(source_id, str) and source_id:
+ aliases.append(source_id)
+ source_ids = payload.get("source_ids")
+ if isinstance(source_ids, list):
+ aliases.extend(alias for alias in source_ids if isinstance(alias, str) and alias)
+
+ for citation in url_citations:
+ if citation["url"] != url:
+ continue
+ existing_aliases = citation.setdefault("source_ids", [])
+ for alias in aliases:
+ if alias not in existing_aliases:
+ existing_aliases.append(alias)
+ return
+
+ url_citations.append(
+ {
+ "url": url,
+ "title": payload.get("title") or url,
+ "snippet": payload.get("snippet") or payload.get("quote") or "",
+ "source_ids": aliases,
+ }
+ )
+
+
def _extract_web_search_action(item: dict[str, Any]) -> dict[str, Any]:
"""Normalize an OpenAI web_search_call action into card arguments. gpt-5.x agentic search emits
three action types discriminated by `action.type`: `search` carries queries, `open_page` a
@@ -1287,8 +1357,11 @@ class ExternalProviderClient:
base_url: str,
api_key: str,
timeout: float = 120.0,
+ *,
+ api_type: str = "chat_completions",
):
self.provider_type = provider_type
+ self.api_type = api_type if provider_type == "custom" else "chat_completions"
# Single choke point for every outbound provider request (chat, models, responses, messages, containers): the
# URL is caller-controlled, so it is validated here even when a route already checked it. Routes turn the
# ValueError into a 400; reaching it here means a caller bypassed them.
@@ -1328,9 +1401,27 @@ class ExternalProviderClient:
auth_prefix = "Bearer "
headers = {"Content-Type": "application/json"}
- # Skip auth header when api_key is empty (optional for local providers); httpx rejects an empty `Bearer `
- # value as "Illegal header value".
- if self.api_key:
+ # Azure OpenAI accepts a resource key in `api-key` or an Entra access token as a Bearer token.
+ # A saved custom provider has only one credential field, so preserve raw JWT bearer credentials and let
+ # `Bearer <token>` explicitly select bearer auth for opaque Entra tokens. Never send both credentials.
+ azure_custom_responses = (
+ self.provider_type == "custom"
+ and self.api_type == "responses"
+ and _is_azure_openai_host((urlparse(self.base_url).hostname or "").lower())
+ )
+ if azure_custom_responses and self.api_key:
+ if self.api_key[:7].lower() == "bearer ":
+ bearer_token = self.api_key[7:].strip()
+ elif re.fullmatch(r"eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+", self.api_key):
+ bearer_token = self.api_key
+ else:
+ bearer_token = None
+ if bearer_token:
+ headers["Authorization"] = f"Bearer {bearer_token}"
+ elif bearer_token is None:
+ headers["api-key"] = self.api_key
+ # Skip auth when the key is empty (optional for local providers); httpx rejects an empty `Bearer `.
+ elif self.api_key:
headers[auth_header] = f"{auth_prefix}{self.api_key}"
# Merge provider-specific extra headers (anthropic-version, OpenRouter attribution).
headers.update(provider_info.get("extra_headers", {}))
@@ -1353,7 +1444,7 @@ class ExternalProviderClient:
self,
messages: list[dict[str, Any]],
model: str,
- temperature: float = 0.7,
+ temperature: Optional[float] = 0.7,
top_p: Optional[float] = 0.95,
max_tokens: Optional[int] = None,
presence_penalty: float = 0.0,
@@ -1387,6 +1478,28 @@ class ExternalProviderClient:
isinstance(tool_choice, str) and tool_choice.strip().lower() == "none"
)
+ # Managed OpenAI Responses hosts do not apply a local chat template; preserve literal delimiter text there.
+ # Custom gateways and local endpoints still need their template-control-token protection.
+ managed_custom_responses = (
+ self.provider_type == "custom"
+ and self.api_type == "responses"
+ and _is_openai_family_cloud(self.base_url)
+ )
+ if self.provider_type in _TEMPLATE_APPLYING_PROVIDERS and not managed_custom_responses:
+ from core.inference.chat_template_helpers import (
+ neutralize_control_markup_in_messages,
+ neutralize_tool_descriptions,
+ reconciled_tool_choice,
+ )
+ messages = neutralize_control_markup_in_messages(messages)
+ if tools:
+ safe_tools = neutralize_tool_descriptions(tools)
+ # reconcile a forced tool choice when sanitization removes its function from a mixed catalog.
+ tool_choice = reconciled_tool_choice(tool_choice, tools, safe_tools)
+ if not safe_tools:
+ tool_choice = None
+ tools = safe_tools
+
if not self._is_openai_compatible():
# Gemini speaks its own native REST shape (contents/parts); `_stream_gemini` translates request/response
# into the OpenAI Chat Completions chunk format the rest of Unsloth expects.
@@ -1432,7 +1545,7 @@ class ExternalProviderClient:
# OpenAI moved flagship models (gpt-5.x) off /v1/chat/completions -- those endpoints return 404 "This is not a
# chat model" for the new families. Route all OpenAI traffic through /v1/responses instead and translate the
# Responses SSE back into Chat Completions chunks so the frontend stays endpoint-agnostic.
- if self.provider_type == "openai":
+ if self.provider_type == "openai" or self.api_type == "responses":
async for line in self._stream_openai_responses(
messages,
model,
@@ -1448,6 +1561,7 @@ class ExternalProviderClient:
tools,
tool_choice,
response_format,
+ stream = stream if self.provider_type == "custom" else True,
):
yield line
return
@@ -1475,25 +1589,6 @@ class ExternalProviderClient:
yield line
return
- # A self-hosted server templates client text just like the in-process paths, so the same "</think>" or turn
- # marker forges a turn (#7066). Hosted APIs are left alone: their prompt assembly is not ours to rewrite.
- if self.provider_type in _TEMPLATE_APPLYING_PROVIDERS:
- from core.inference.chat_template_helpers import (
- neutralize_control_markup_in_messages,
- neutralize_tool_descriptions,
- reconciled_tool_choice,
- )
- messages = neutralize_control_markup_in_messages(messages)
- if tools:
- safe_tools = neutralize_tool_descriptions(tools)
- # A mixed catalog keeps safe_tools non-empty while dropping the one tool the client forced, so an
- # empty check is not enough: without the passthrough builder's per-name reconciliation the body names
- # an unadvertised function.
- tool_choice = reconciled_tool_choice(tool_choice, tools, safe_tools)
- if not safe_tools:
- tool_choice = None
- tools = safe_tools
-
# Both are set because a server rejects continuing while a generation prompt is still asked for. Sent only to
# the two documenting the pair: "custom" is any user-supplied base_url and a strict endpoint 400s on an
# unknown field, so it keeps the trailing assistant turn on its own as before.
@@ -4986,8 +5081,8 @@ class ExternalProviderClient:
self,
messages: list[dict[str, Any]],
model: str,
- temperature: float,
- top_p: float,
+ temperature: Optional[float],
+ top_p: Optional[float],
max_tokens: Optional[int],
enable_thinking: Optional[bool],
reasoning_effort: Optional[str],
@@ -4998,6 +5093,7 @@ class ExternalProviderClient:
tools: Optional[list[dict[str, Any]]] = None,
tool_choice: Optional[Any] = None,
response_format: Optional[dict[str, Any]] = None,
+ stream: bool = True,
) -> AsyncGenerator[str, None]:
"""Call OpenAI's /v1/responses endpoint and translate its SSE stream back into OpenAI Chat
Completions chunk format. The Responses API uses a different request shape (``input`` not
@@ -5007,8 +5103,23 @@ class ExternalProviderClient:
import json as _json
is_openai_cloud = _is_openai_family_cloud(self.base_url)
+ _responses_tool_choice_none = (
+ isinstance(tool_choice, str) and tool_choice.strip().lower() == "none"
+ )
+ _responses_tool_choice_forced_function = (
+ isinstance(tool_choice, dict)
+ and tool_choice.get("type") == "function"
+ and isinstance(tool_choice.get("function"), dict)
+ and bool(tool_choice["function"].get("name"))
+ )
+ _responses_hosted_builtins_allowed = (
+ not _responses_tool_choice_none and not _responses_tool_choice_forced_function
+ )
image_generation_requested = bool(
- enabled_tools and "image_generation" in enabled_tools and is_openai_cloud
+ _responses_hosted_builtins_allowed
+ and enabled_tools
+ and "image_generation" in enabled_tools
+ and is_openai_cloud
)
# Split system messages into a single `instructions` string and translate user/assistant messages into the
@@ -5250,12 +5361,13 @@ class ExternalProviderClient:
filtered_replay_items.append(item)
openai_replay_items = filtered_replay_items
if dropped_image_replay_without_reasoning:
- yield _error_sse_line(
+ error_line = _error_sse_line(
400,
"OpenAI image edit reference is missing paired reasoning state. "
"Regenerate the image, then retry the edit.",
self.provider_type,
)
+ yield error_line if stream else error_line.removeprefix("data: ")
return
image_generation_has_reference = bool(
previous_response_id
@@ -5272,16 +5384,22 @@ class ExternalProviderClient:
break
input_items[insert_at:insert_at] = openai_replay_items
- # Reasoning families reject temperature/top_p, and the UI hides both sliders for the rest
- # (provider-capabilities.ts), so the only values arriving here are ChatCompletionRequest's 0.6/0.95 defaults,
- # which would override OpenAI's own with a number the user never chose.
- del temperature, top_p # accepted for API symmetry, not forwarded.
-
body: dict[str, Any] = {
"model": model,
"input": input_items,
- "stream": True,
+ "stream": stream,
}
+ # Azure model ids are deployment names, which need not reveal a fixed-sampling model behind them.
+ # Omit both controls for Azure; on direct OpenAI cloud, use the model family. Gateways still receive them.
+ is_azure_openai = _is_azure_openai_host((urlparse(self.base_url).hostname or "").lower())
+ forward_custom_sampling = self.provider_type == "custom" and not (
+ is_azure_openai or (is_openai_cloud and _openai_fixed_sampling_model(model))
+ )
+ if forward_custom_sampling:
+ if temperature is not None:
+ body["temperature"] = temperature
+ if top_p is not None:
+ body["top_p"] = top_p
if previous_response_id:
body["previous_response_id"] = previous_response_id
# `summary: "auto"` is what makes /v1/responses emit reasoning summary events; without it the reasoning panel
@@ -5381,6 +5499,8 @@ class ExternalProviderClient:
_entry["description"] = _fn["description"]
if isinstance(_fn.get("parameters"), dict):
_entry["parameters"] = normalize_function_schema(_fn["parameters"])
+ if _fn.get("strict") is not None:
+ _entry["strict"] = bool(_fn["strict"])
responses_user_function_tools.append(_entry)
# Translate tool_choice into the Responses shape.
@@ -5404,18 +5524,26 @@ class ExternalProviderClient:
if isinstance(_name, str) and _name:
responses_tool_choice = {"type": "function", "name": _name}
- _responses_tool_choice_none = _responses_tc_string == "none"
# A pinned user function suppresses hosted builtins (privacy + billing), matching the Gemini / Anthropic /
# OpenRouter gates.
- _responses_tool_choice_forced_function = (
- isinstance(tool_choice, dict)
- and tool_choice.get("type") == "function"
- and isinstance(tool_choice.get("function"), dict)
- and bool(tool_choice["function"].get("name"))
- )
- _responses_hosted_builtins_allowed = (
- not _responses_tool_choice_none and not _responses_tool_choice_forced_function
+ _responses_image_generation_enabled = (
+ _responses_hosted_builtins_allowed and image_generation_enabled_openai
)
+ if _responses_image_generation_enabled and not stream:
+ yield _json.dumps(
+ {
+ "error": {
+ "message": (
+ "image_generation is not supported for non-streaming Responses "
+ "requests; set stream=true."
+ ),
+ "type": "invalid_request_error",
+ "code": "400",
+ "provider": self.provider_type,
+ }
+ }
+ )
+ return
if (enabled_tools or responses_user_function_tools) and not _responses_tool_choice_none:
tools_array: list[dict[str, Any]] = list(responses_user_function_tools)
@@ -5437,14 +5565,14 @@ class ExternalProviderClient:
else:
shell_env = {"type": "container_auto"}
tools_array.append({"type": "shell", "environment": shell_env})
- if _responses_hosted_builtins_allowed and image_generation_enabled_openai:
+ if _responses_image_generation_enabled:
tools_array.append(_openai_image_generation_tool())
if tools_array:
body["tools"] = tools_array
if responses_tool_choice is not None:
body["tool_choice"] = responses_tool_choice
- url = f"{self.base_url}/responses"
+ url = _append_provider_path(self.base_url, "/responses")
completion_id = f"chatcmpl-openai-{model.replace('/', '-')}"
logger.info("Proxying OpenAI Responses API to %s (model=%s)", url, model)
@@ -5471,7 +5599,7 @@ class ExternalProviderClient:
else:
env_attempt = {"type": "container_auto"}
tools_array_attempt.append({"type": "shell", "environment": env_attempt})
- if _responses_hosted_builtins_allowed and image_generation_enabled_openai:
+ if _responses_image_generation_enabled:
tools_array_attempt.append(_openai_image_generation_tool())
if tools_array_attempt:
attempt_body["tools"] = tools_array_attempt
@@ -5520,19 +5648,142 @@ class ExternalProviderClient:
and _is_openai_container_expired_error(error_text)
)
if expired_container_4xx and not retried:
- yield (
- f"data: "
- f"{_json.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'choices': [{'index': 0, 'delta': {}, 'finish_reason': None}], '_toolEvent': {'type': 'container_invalidated'}})}"
- )
+ if stream:
+ yield (
+ f"data: "
+ f"{_json.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'choices': [{'index': 0, 'delta': {}, 'finish_reason': None}], '_toolEvent': {'type': 'container_invalidated'}})}"
+ )
retried = True
attempt_container_id = None
continue
- yield _error_sse_line(
+ error_line = _error_sse_line(
response.status_code,
error_text,
self.provider_type,
response.headers.get("Retry-After"),
)
+ yield error_line if stream else error_line.removeprefix("data: ")
+ return
+
+ if not stream:
+ response_payload = _json.loads(await response.aread())
+ status = response_payload.get("status")
+ if status == "failed" or response_payload.get("error"):
+ yield _json.dumps(
+ {
+ "error": {
+ "message": _openai_response_error_message(response_payload),
+ "type": "server_error",
+ "provider": self.provider_type,
+ }
+ }
+ )
+ return
+ text_parts: list[str] = []
+ reasoning_summary_parts: list[str] = []
+ refusal_parts: list[str] = []
+ tool_calls: list[dict[str, Any]] = []
+ reasoning_replay_items: list[dict[str, Any]] = []
+ url_citations: list[dict[str, Any]] = []
+ for item in response_payload.get("output") or []:
+ if not isinstance(item, dict):
+ continue
+ if item.get("type") == "reasoning":
+ summary = item.get("summary")
+ if isinstance(summary, list):
+ for part in summary:
+ if not isinstance(part, dict):
+ continue
+ if part.get("type") != "summary_text":
+ continue
+ summary_text = part.get("text")
+ if isinstance(summary_text, str) and summary_text:
+ reasoning_summary_parts.append(summary_text)
+ reasoning_item = _sanitize_openai_reasoning_replay_item(item)
+ if reasoning_item:
+ reasoning_replay_items.append(reasoning_item)
+ elif item.get("type") == "message":
+ content = item.get("content") or []
+ if isinstance(content, str):
+ text_parts.append(content)
+ continue
+ for part in content:
+ if not isinstance(part, dict):
+ continue
+ if part.get("type") in ("output_text", "text"):
+ text = part.get("text")
+ if isinstance(text, str):
+ text_parts.append(text)
+ for annotation in part.get("annotations") or []:
+ if isinstance(annotation, dict):
+ _record_openai_url_citation(
+ url_citations,
+ annotation,
+ )
+ elif part.get("type") == "refusal":
+ refusal = part.get("refusal")
+ if isinstance(refusal, str):
+ refusal_parts.append(refusal)
+ elif item.get("type") == "function_call":
+ name = item.get("name")
+ if not isinstance(name, str) or not name:
+ continue
+ arguments = item.get("arguments", "")
+ if not isinstance(arguments, str):
+ arguments = _json.dumps(arguments)
+ tool_calls.append(
+ {
+ "id": item.get("call_id") or item.get("id") or "call_0",
+ "type": "function",
+ "function": {"name": name, "arguments": arguments},
+ }
+ )
+ if not text_parts and isinstance(response_payload.get("output_text"), str):
+ text_parts.append(response_payload["output_text"])
+
+ visible_text = "".join(text_parts)
+ if reasoning_summary_parts:
+ visible_text = (
+ f"<think>{''.join(reasoning_summary_parts)}</think>{visible_text}"
+ )
+ visible_text = _replace_openai_citation_markers(
+ visible_text,
+ url_citations,
+ )
+
+ message: dict[str, Any] = {
+ "role": "assistant",
+ "content": visible_text or None,
+ }
+ if refusal_parts:
+ message["refusal"] = "".join(refusal_parts)
+ if tool_calls:
+ message["tool_calls"] = tool_calls
+ if reasoning_replay_items:
+ message["extra_content"] = {
+ "openai_responses_reasoning": reasoning_replay_items
+ }
+
+ completion: dict[str, Any] = {
+ "id": response_payload.get("id") or completion_id,
+ "object": "chat.completion",
+ "created": int(response_payload.get("created_at") or time.time()),
+ "model": response_payload.get("model") or model,
+ "choices": [
+ {
+ "index": 0,
+ "message": message,
+ "finish_reason": _openai_response_finish_reason(
+ response_payload,
+ has_tool_calls = bool(tool_calls),
+ ),
+ }
+ ],
+ }
+ usage = responses_usage_to_chat(response_payload.get("usage"))
+ if usage is not None:
+ completion["usage"] = usage
+ yield _json.dumps(completion)
return
# Same manual __anext__ loop as stream_chat_completion -- see there for the GeneratorExit / aclose
@@ -5677,40 +5928,7 @@ class ExternalProviderClient:
return "\n--- next command ---\n".join(parts) if parts else "(no output)"
def _record_url_citation(payload: dict[str, Any]) -> None:
- """Append a url_citation, deduped by URL: collect every source_id alias onto
- the entry's ``source_ids`` so the rewriter can resolve any alias. The id
- lives under source_id/id/locator across API revisions."""
- if payload.get("type") != "url_citation":
- return
- url = payload.get("url", "")
- if not url:
- return
- source_id = (
- payload.get("source_id")
- or payload.get("id")
- or payload.get("locator")
- or ""
- )
- # Single pass: either backfill aliases onto an existing URL entry (and return) or fall through
- # to append a fresh one.
- for c in all_url_citations:
- if c["url"] != url:
- continue
- if source_id:
- aliases = c.setdefault("source_ids", [])
- if source_id not in aliases:
- aliases.append(source_id)
- return
- title = payload.get("title") or url
- snippet = payload.get("snippet") or payload.get("quote") or ""
- all_url_citations.append(
- {
- "url": url,
- "title": title,
- "snippet": snippet,
- "source_ids": [source_id] if source_id else [],
- }
- )
+ _record_openai_url_citation(all_url_citations, payload)
def _record_openai_reasoning_replay_item(
payload: Any,
@@ -5920,6 +6138,14 @@ class ExternalProviderClient:
elif head_rewritten:
yield _chunk_with_text(head_rewritten)
+ elif event_type == "response.refusal.delta":
+ refusal_delta = event.get("delta", "")
+ if isinstance(refusal_delta, str) and refusal_delta:
+ if reasoning_open:
+ yield _chunk_with_text("</think>")
+ reasoning_open = False
+ yield _chunk_with_text(refusal_delta)
+
elif event_type == "response.output_text.annotation.added":
ann = event.get("annotation")
if isinstance(ann, dict):
@@ -6306,7 +6532,9 @@ class ExternalProviderClient:
yield usage_line
elif event_type == "response.incomplete":
- incomplete_usage = (event.get("response") or {}).get("usage")
+ incomplete_response = dict(event.get("response") or {})
+ incomplete_response.setdefault("status", "incomplete")
+ incomplete_usage = incomplete_response.get("usage")
if isinstance(incomplete_usage, dict):
last_usage = incomplete_usage
# Same flush as response.completed -- truncated streams can leave a half-marker in the
@@ -6369,7 +6597,9 @@ class ExternalProviderClient:
{
"index": 0,
"delta": {},
- "finish_reason": "length",
+ "finish_reason": _openai_response_finish_reason(
+ incomplete_response
+ ),
}
],
}
@@ -6448,25 +6678,28 @@ class ExternalProviderClient:
except httpx.ConnectError as exc:
logger.error("Connection error to %s: %s", self.provider_type, exc)
- yield _error_sse_line(
+ error_line = _error_sse_line(
502,
f"Failed to connect to {self.provider_type}: {exc}",
self.provider_type,
)
+ yield error_line if stream else error_line.removeprefix("data: ")
except httpx.ReadTimeout as exc:
logger.error("Read timeout from %s: %s", self.provider_type, exc)
- yield _error_sse_line(
+ error_line = _error_sse_line(
504,
f"Timeout waiting for {self.provider_type} response",
self.provider_type,
)
+ yield error_line if stream else error_line.removeprefix("data: ")
except httpx.HTTPError as exc:
logger.error("HTTP error from %s: %s", self.provider_type, exc)
- yield _error_sse_line(
+ error_line = _error_sse_line(
502,
f"Error communicating with {self.provider_type}: {exc}",
self.provider_type,
)
+ yield error_line if stream else error_line.removeprefix("data: ")
async def chat_completion(
self,
@@ -6960,26 +7193,25 @@ def _build_usage_chunk(
usage_block["speed"] = speed
else:
prompt_tokens = last_usage.get("input_tokens") or 0
- cached = 0
details = last_usage.get("input_tokens_details")
- if isinstance(details, dict):
- cached = details.get("cached_tokens") or 0
+ prompt_details = dict(details) if isinstance(details, dict) else {}
+ cached = prompt_details.get("cached_tokens") or 0
+ prompt_details.setdefault("cached_tokens", cached)
if not (prompt_tokens or completion_tokens or cached):
return None
usage_block = {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": prompt_tokens + completion_tokens,
- "prompt_tokens_details": {"cached_tokens": cached},
+ "prompt_tokens_details": prompt_details,
}
# Surface OpenAI Responses / Gemini reasoning-token detail. The caller pre-populates
# last_usage["output_tokens_details"] with at least {"reasoning_tokens": ...}; mirror it into the OAI
# `completion_tokens_details` shape so SDKs can render the hidden-thoughts slice.
out_details = last_usage.get("output_tokens_details")
if isinstance(out_details, dict) and out_details:
- usage_block["completion_tokens_details"] = {
- "reasoning_tokens": out_details.get("reasoning_tokens") or 0,
- }
+ usage_block["completion_tokens_details"] = dict(out_details)
+ usage_block["completion_tokens_details"].setdefault("reasoning_tokens", 0)
usage_block["output_tokens_details"] = out_details
chunk = {
diff --git a/studio/backend/core/research_runs.py b/studio/backend/core/research_runs.py
index c53c6765e20..97645da309d 100644
--- a/studio/backend/core/research_runs.py
+++ b/studio/backend/core/research_runs.py
@@ -78,6 +78,11 @@ _URL_BLOCK = re.compile(
r"Title:\s*(?P<title>[^\n]*)\nURL:\s*(?P<url>https?://[^\s]+)\nSnippet:\s*(?P<snippet>.*?)(?=\n\n---|\Z)",
re.DOTALL,
)
+_OPENAI_RESPONSES_FIXED_SAMPLING_MODEL = re.compile(
+ r"^(?:gpt-6-astra(?:[-.]|$)|gpt-5(?:[-.]|$)|gpt-4\.5(?:[-.]|$)|"
+ r"o\d+(?:[-.]|$)|codex-mini(?:[-.]|$))"
+)
+_OPENAI_NON_REASONING_CHAT_ALIAS = re.compile(r"-chat(?:-latest)?$")
_WALL_CLOCK_TIMEOUT_CANCEL_MESSAGE = "research-wall-clock-timeout"
_MAX_ERROR_CHARS = 500
_MAX_CONTEXT_CHARS = 12_000
@@ -537,6 +542,30 @@ def _saved_connection_cap(provider_id: object) -> int | None | object:
return _CAP_UNREADABLE
+def _custom_responses_rejects_sampling(inference: dict[str, Any]) -> bool:
+ """Whether this research hop targets fixed-sampling OpenAI Responses semantics."""
+ if inference.get("providerType") != "custom":
+ return False
+ provider_id = inference.get("providerId")
+ if not isinstance(provider_id, str) or not provider_id:
+ return False
+ try:
+ provider = providers_db.get_provider(provider_id) or {}
+ except Exception:
+ logger.debug("research.provider_api_type_probe_failed", exc_info = True)
+ return False
+ if provider.get("provider_type") != "custom" or provider.get("api_type") != "responses":
+ return False
+
+ model = str(inference.get("externalModel") or inference.get("model") or "").strip().lower()
+ if _OPENAI_NON_REASONING_CHAT_ALIAS.search(model):
+ return False
+ # Sampling support is an upstream model contract, independent of the UI's reasoning
+ # toggle metadata. In particular, codex-mini and gpt-4.5 reject these fields without
+ # necessarily being marked reasoning-capable by the client that created a durable run.
+ return _OPENAI_RESPONSES_FIXED_SAMPLING_MODEL.match(model) is not None
+
+
def _normalize_completion_usage(raw: Any) -> dict[str, int] | None:
if not isinstance(raw, dict):
return None
@@ -1631,6 +1660,9 @@ class ResearchSupervisor:
)
config = run["config"]
inference = config.get("inferenceRequest") or {}
+ omit_sampling = inference.get("providerType") == "custom" and await asyncio.to_thread(
+ _custom_responses_rejects_sampling, inference
+ )
payload: dict[str, Any] = {
"model": inference.get("model") or config.get("model") or "",
"messages": messages,
@@ -1643,8 +1675,9 @@ class ResearchSupervisor:
# enabled_tools resolves to every built-in, python and terminal included.
"tool_choice": "none",
"enabled_tools": [],
- "temperature": inference.get("temperature", 0.2),
}
+ if not omit_sampling:
+ payload["temperature"] = inference.get("temperature", 0.2)
# The route's _sanitize_config already refused anything but an enabled saved connection of a studio-
# tools-capable provider type.
@@ -1656,7 +1689,7 @@ class ResearchSupervisor:
"external_model": inference["externalModel"],
}
)
- if inference.get("topP") is not None:
+ if not omit_sampling and inference.get("topP") is not None:
payload["top_p"] = inference["topP"]
if inference.get("providerType") in ("deepseek", "huggingface", "qwen", "mistral"):
# These providers forward reasoning fields verbatim, and a strict upstream rejects one the model lacks.
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index de408536e13..11f8307f9c9 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -2440,6 +2440,7 @@ class ChatCompletionRequest(BaseModel):
None,
description = "[x-unsloth] Saved provider config ID. Its stored key is used when encrypted_api_key is omitted.",
)
+ provider_api_type: Literal["chat_completions", "responses"] = "chat_completions"
provider_type: Optional[str] = Field(
None,
description = "[x-unsloth] Provider type (e.g. 'openai', 'mistral'). Used if provider_id is not set.",
@@ -2512,7 +2513,7 @@ class ChatCompletionRequest(BaseModel):
"header. The upstream floor is 50k; `_stream_anthropic` clamps "
"lower values up.\n"
" - OpenAI cloud (api.openai.com) and Azure OpenAI Foundry "
- "(*.openai.azure.com): attaches "
+ "(*.openai.azure.com, *.services.ai.azure.com): attaches "
"`context_management:[{type:'compaction', compact_threshold:N}]` "
"to /v1/responses. Effective floor is around 200k (OpenAI's "
"canonical example); values below it surface "
diff --git a/studio/backend/models/providers.py b/studio/backend/models/providers.py
index 446560cb4b2..2e6b6c47951 100644
--- a/studio/backend/models/providers.py
+++ b/studio/backend/models/providers.py
@@ -7,6 +7,8 @@ from typing import Literal, Optional
from pydantic import BaseModel, Field
+ProviderApiType = Literal["chat_completions", "responses"]
+
MAX_JSON_SAFE_INTEGER = 9_007_199_254_740_991
@@ -51,6 +53,8 @@ class ProviderRegistryEntry(BaseModel):
class ProviderCreate(BaseModel):
"""Request to create a saved provider configuration."""
+ api_type: ProviderApiType = "chat_completions"
+
provider_type: str = Field(..., description = "Provider type from the registry")
display_name: str = Field(..., description = "User-chosen label (e.g. 'My OpenAI Key')")
base_url: Optional[str] = Field(
@@ -82,6 +86,8 @@ class ProviderCreate(BaseModel):
class ProviderUpdate(BaseModel):
"""Request to update a saved provider configuration."""
+ api_type: Optional[ProviderApiType] = None
+
display_name: Optional[str] = Field(None, description = "New display name")
base_url: Optional[str] = Field(None, description = "New base URL")
is_enabled: Optional[bool] = Field(None, description = "Enable or disable this provider")
@@ -121,6 +127,8 @@ class ProviderCredentialMigration(BaseModel):
class ProviderResponse(BaseModel):
"""A saved provider configuration (returned by list/get endpoints)."""
+ api_type: ProviderApiType = "chat_completions"
+
id: str = Field(..., description = "Unique provider config ID")
provider_type: str = Field(..., description = "Provider type (e.g. 'openai')")
display_name: str = Field(..., description = "User-chosen label")
@@ -179,6 +187,8 @@ class ModelCatalogResponse(BaseModel):
class ProviderModelsRequest(BaseModel):
"""Request to list models from an external provider."""
+ api_type: ProviderApiType = "chat_completions"
+
provider_id: Optional[str] = Field(
None, description = "Saved provider config whose stored key may be used"
)
@@ -196,6 +206,8 @@ class ProviderModelsRequest(BaseModel):
class ProviderTestRequest(BaseModel):
"""Request to test connectivity to an external provider."""
+ api_type: ProviderApiType = "chat_completions"
+
provider_id: Optional[str] = Field(
None, description = "Saved provider config whose stored key may be used"
)
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index f088df8297f..68a74868e01 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -3639,7 +3639,7 @@ from core.inference.providers import (
provider_runs_local_tools,
validate_provider_base_url,
)
-from core.inference.external_provider import ExternalProviderClient
+from core.inference.external_provider import ExternalProviderClient, _is_openai_family_cloud
from core.inference.external_tool_transport import OAICompatTransport
from core.inference.sse_control_frames import (
is_ui_control_sse_line,
@@ -22575,6 +22575,7 @@ def _build_external_messages(
supports_vision: bool,
provider_type: Optional[str] = None,
base_url: Optional[str] = None,
+ api_type: Optional[str] = None,
promoted_out: "Optional[list]" = None,
promote_mcp_images: "Optional[bool]" = None,
) -> list[dict]:
@@ -22589,23 +22590,26 @@ def _build_external_messages(
that flag, or a stricter MCP answer would also strip what the caller
attached.
- `input_document`: preserved ONLY when the provider's stream helper has
- explicit translation logic (Anthropic + OpenAI today, see
- ``_INPUT_DOCUMENT_PROVIDERS``). Stripped for every other provider so the
- unknown type doesn't reach generic /chat/completions and 400.
- - `reasoning`: OpenAI-only Responses reasoning item paired with a prior
- tool output. Forwarded ONLY when provider_type=="openai" so follow-up
- image edits can replay the required reasoning item.
- - `image_generation_call`: OpenAI-only Responses image reference. Forwarded
- ONLY when provider_type=="openai" so follow-up image edits can reference
- prior generated images.
+ explicit translation logic (Anthropic, OpenAI, and custom Responses).
+ Stripped for every other provider so the unknown type doesn't reach
+ generic /chat/completions and 400.
+ - `reasoning`: Responses reasoning item paired with a prior tool output.
+ Forwarded for OpenAI and custom Responses so follow-up image edits can
+ replay the required reasoning item.
+ - `image_generation_call`: Responses image reference. Forwarded for OpenAI
+ and custom Responses so follow-up image edits can reference prior images.
- `compaction`: Anthropic-only synthetic part (round-trips server-side
compaction state). Forwarded ONLY when provider_type=="anthropic";
stripped elsewhere so the unknown part doesn't reach generic
/chat/completions and 400 (DeepSeek, Mistral, Gemini, Kimi, OpenRouter).
"""
- document_provider = provider_type in _INPUT_DOCUMENT_PROVIDERS
+ document_provider = provider_type in _INPUT_DOCUMENT_PROVIDERS or (
+ provider_type == "custom" and api_type == "responses"
+ )
anthropic = provider_type == "anthropic"
- openai = provider_type == "openai"
+ responses_native_parts = provider_type == "openai" or (
+ provider_type == "custom" and api_type == "responses"
+ )
# `extra_content` carries the assistant's text-part `thoughtSignature`
# round-trip on Gemini's native streamGenerateContent endpoint. Custom
# Gemini OpenAI-compat gateways (LiteLLM etc.) route through
@@ -22620,6 +22624,9 @@ def _build_external_messages(
except Exception:
_native_gemini = False
emit_extra_content = _native_gemini or provider_type == "openai_codex"
+ emit_message_extra_content = emit_extra_content or (
+ provider_type == "custom" and api_type == "responses"
+ )
_SERVER_BUILTIN_TOOL_NAMES = frozenset(
{"web_search", "web_fetch", "code_execution", "image_generation"}
@@ -22755,7 +22762,7 @@ def _build_external_messages(
out["tool_call_id"] = msg.tool_call_id
if msg.name:
out["name"] = msg.name
- if emit_extra_content and msg.role == "assistant" and msg.extra_content:
+ if emit_message_extra_content and msg.role == "assistant" and msg.extra_content:
out["extra_content"] = msg.extra_content
result.append(out)
continue
@@ -22773,7 +22780,7 @@ def _build_external_messages(
"content": "",
"tool_calls": _filtered_tcs,
}
- if emit_extra_content and msg.extra_content:
+ if emit_message_extra_content and msg.extra_content:
_assistant_only["extra_content"] = msg.extra_content
result.append(_assistant_only)
continue
@@ -22791,7 +22798,7 @@ def _build_external_messages(
}
)
elif (
- openai
+ responses_native_parts
and msg.role == "assistant"
and (_rp := _openai_responses_part(part)) is not None
):
@@ -22836,7 +22843,7 @@ def _build_external_messages(
entry["tool_call_id"] = msg.tool_call_id
if msg.name:
entry["name"] = msg.name
- if emit_extra_content and msg.role == "assistant" and msg.extra_content:
+ if emit_message_extra_content and msg.role == "assistant" and msg.extra_content:
entry["extra_content"] = msg.extra_content
result.append(entry)
else:
@@ -22849,7 +22856,7 @@ def _build_external_messages(
if p.type == "text":
preserved.append({"type": "text", "text": p.text})
elif (
- openai
+ responses_native_parts
and msg.role == "assistant"
and (_rp := _openai_responses_part(p)) is not None
):
@@ -22882,7 +22889,7 @@ def _build_external_messages(
entry["tool_call_id"] = msg.tool_call_id
if msg.name:
entry["name"] = msg.name
- if emit_extra_content and msg.role == "assistant" and msg.extra_content:
+ if emit_message_extra_content and msg.role == "assistant" and msg.extra_content:
entry["extra_content"] = msg.extra_content
result.append(entry)
originals = {
@@ -22964,20 +22971,60 @@ async def _build_external_messages_async(messages, supports_vision, **kwargs) ->
return _build_external_messages(messages, supports_vision, **kwargs)
+def _safe_retry_after_header(value: Any) -> Optional[str]:
+ """Return an RFC-compatible Retry-After value safe for an HTTP header.
+
+ The field permits either decimal delay-seconds or an HTTP date. Reject
+ control bytes, non-ASCII text, oversized values, malformed dates, and
+ non-GMT dates before copying provider-controlled data into a response
+ header.
+ """
+ if not isinstance(value, str):
+ return None
+ if (
+ len(value) > 128
+ or not value.isascii()
+ or any(ord(char) < 0x20 or ord(char) > 0x7E for char in value)
+ ):
+ return None
+ candidate = value.strip()
+ if not candidate:
+ return None
+ if candidate.isdecimal():
+ return candidate
+
+ from datetime import timedelta
+ from email.utils import parsedate_to_datetime
+
+ try:
+ parsed = parsedate_to_datetime(candidate)
+ except (TypeError, ValueError, OverflowError):
+ return None
+ if (
+ parsed is None
+ or parsed.tzinfo is None
+ or parsed.utcoffset() != timedelta(0)
+ or not candidate.endswith(" GMT")
+ ):
+ return None
+ return candidate
+
+
async def _proxy_to_external_provider(
payload: ChatCompletionRequest,
request: Request,
current_subject: Optional[str] = None,
-) -> StreamingResponse:
+) -> Response:
"""
Proxy a chat completion request to an external LLM provider.
- Resolves provider config (DB or registry), decrypts the API key, and
- streams the response back in OpenAI SSE format.
+ Resolves provider config (DB or registry), decrypts the API key, and returns
+ either one Chat Completion JSON object or an OpenAI-compatible SSE stream.
"""
# Resolve provider type and base URL
provider_type = payload.provider_type
base_url = payload.provider_base_url
+ api_type = payload.provider_api_type
saved_provider_snapshot: Optional[dict] = None
if payload.provider_id and not payload.encrypted_api_key:
@@ -22998,6 +23045,7 @@ async def _proxy_to_external_provider(
saved_provider_snapshot = config
provider_type = config["provider_type"]
base_url = config["base_url"]
+ api_type = config.get("api_type", "chat_completions")
if not provider_type:
raise HTTPException(
@@ -23538,7 +23586,7 @@ async def _proxy_to_external_provider(
if saved_provider_snapshot is not None:
async with provider_config_guard(payload.provider_id):
current = await asyncio.to_thread(providers_db.get_provider, payload.provider_id)
- routing_fields = ("provider_type", "base_url", "is_enabled")
+ routing_fields = ("provider_type", "base_url", "api_type", "is_enabled")
if current is None or any(
current.get(field) != saved_provider_snapshot.get(field) for field in routing_fields
):
@@ -23594,6 +23642,7 @@ async def _proxy_to_external_provider(
_supports_vision,
provider_type = provider_type,
base_url = base_url,
+ api_type = api_type,
promoted_out = _external_promoted_parts,
promote_mcp_images = _external_takes_mcp_images(
provider_type, _supports_vision, model, _pinfo
@@ -23615,6 +23664,10 @@ async def _proxy_to_external_provider(
provider_type = provider_type,
base_url = base_url,
api_key = api_key,
+ api_type = api_type,
+ )
+ _non_stream_custom_responses = (
+ provider_type == "custom" and api_type == "responses" and payload.stream is False
)
# Schema defaults are non-None (20, 0.01, 1.0) for the local path, so only
@@ -23679,7 +23732,13 @@ async def _proxy_to_external_provider(
async def _stream():
_provider_kwargs = dict(
- temperature = payload.temperature,
+ temperature = (
+ None
+ if provider_type == "custom"
+ and api_type == "responses"
+ and "temperature" not in payload.model_fields_set
+ else payload.temperature
+ ),
top_p = _top_p_explicit,
# Honor max_completion_tokens when max_tokens is absent, so a
# provider-routed request capped only by the newer field still gets
@@ -23708,7 +23767,18 @@ async def _proxy_to_external_provider(
# Hosted-only tools still ride along: Images and Fetch have their own
# toggles and no local stand-in, so dropping them would turn a lit
# pill into a tool the model never sees.
- loop_hosted_tools = hosted_only_tools(provider_type, payload.enabled_tools)
+ # A custom Responses connection to OpenAI or Azure uses the same
+ # hosted tool envelope as the native OpenAI provider. Keep the
+ # endpoint check aligned with the Responses translator so an
+ # arbitrary compatible gateway is not offered cloud-only tools.
+ hosted_provider_type = (
+ "openai"
+ if provider_type == "custom"
+ and api_type == "responses"
+ and _is_openai_family_cloud(base_url)
+ else provider_type
+ )
+ loop_hosted_tools = hosted_only_tools(hosted_provider_type, payload.enabled_tools)
gen = stream_with_studio_tools(
OAICompatTransport(
client,
@@ -23782,6 +23852,9 @@ async def _proxy_to_external_provider(
)
except Exception:
pass
+ if _non_stream_custom_responses:
+ yield line
+ continue
if monitor_event == "error":
stream_failed = True
# The monitor has read it by now. Providers are asked for usage on the
@@ -23807,6 +23880,8 @@ async def _proxy_to_external_provider(
# trusting it would append a second [DONE] after the provider's.
if _is_openai_sse_done(line):
sent_done = True
+ if _non_stream_custom_responses:
+ return
# The loop can end without opening the turn a withheld call promised, and the
# reason removed with that call was this stream's last one. Before [DONE], where
# the GGUF passthrough places its own synthetic finish.
@@ -23824,6 +23899,11 @@ async def _proxy_to_external_provider(
except Exception as exc:
logger.error("external_provider.stream_error", error = str(exc))
api_monitor.fail(monitor_id, _friendly_error(exc))
+ if _non_stream_custom_responses:
+ yield json.dumps(
+ {"error": {"message": _friendly_error(exc), "type": "server_error"}}
+ )
+ return
# Surface the failure: a bare EOF (e.g. after a read timeout) is treated
# by the chat client as success, saving a partial answer with no error.
yield (
@@ -23863,6 +23943,50 @@ async def _proxy_to_external_provider(
return _wrapped()
+ if _non_stream_custom_responses:
+ body = "".join([chunk async for chunk in _stream()])
+ try:
+ content = json.loads(body)
+ except json.JSONDecodeError:
+ logger.error("external_provider.non_stream_invalid_json")
+ api_monitor.fail(
+ monitor_id,
+ "External provider returned an invalid non-streaming response.",
+ )
+ return JSONResponse(
+ status_code = 502,
+ content = openai_error_body(
+ "External provider returned an invalid non-streaming response.",
+ status = 502,
+ ),
+ )
+ error_message = (
+ _monitor_openai_error_message(content) if isinstance(content, dict) else None
+ )
+ retry_after_header = None
+ if error_message:
+ api_monitor.fail(monitor_id, error_message)
+ error = content.get("error") if isinstance(content, dict) else None
+ raw_status = error.get("code") if isinstance(error, dict) else None
+ retry_after_header = _safe_retry_after_header(
+ error.get("retry_after") if isinstance(error, dict) else None
+ )
+ try:
+ upstream_status = int(raw_status)
+ except (TypeError, ValueError):
+ upstream_status = 0
+ status_code = upstream_status if 400 <= upstream_status <= 599 else 502
+ else:
+ api_monitor.finish(monitor_id)
+ status_code = 200
+ return JSONResponse(
+ status_code = status_code,
+ content = content,
+ headers = (
+ {"Retry-After": retry_after_header} if retry_after_header is not None else None
+ ),
+ )
+
return StreamingResponse(
_tracked_stream(),
media_type = "text/event-stream",
diff --git a/studio/backend/routes/providers.py b/studio/backend/routes/providers.py
index aa7e4114dab..4f949bfb25b 100644
--- a/studio/backend/routes/providers.py
+++ b/studio/backend/routes/providers.py
@@ -6,6 +6,7 @@ configurations and their API keys, the RSA public key used to encrypt those keys
listing.
"""
+import asyncio
import json
import time
import uuid
@@ -69,6 +70,9 @@ logger = structlog.get_logger(__name__)
router = APIRouter(dependencies = [Depends(get_current_subject)])
+_MAX_RESPONSES_CONNECTIVITY_MODELS = 5
+_PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS = 15.0
+
def _provider_response(row: dict) -> ProviderResponse:
return ProviderResponse(
@@ -76,6 +80,7 @@ def _provider_response(row: dict) -> ProviderResponse:
provider_type = row["provider_type"],
display_name = row["display_name"],
base_url = row["base_url"],
+ api_type = row.get("api_type", "chat_completions"),
is_enabled = bool(row["is_enabled"]),
has_api_key = credential_secrets.has_secret(
credential_secrets.PROVIDER_API_KEY_KIND,
@@ -261,6 +266,7 @@ async def create_provider_config(
models = payload.models,
available_models = payload.available_models,
max_output_tokens = payload.max_output_tokens,
+ api_type = payload.api_type,
)
try:
if api_key:
@@ -351,6 +357,7 @@ async def update_provider_config(
"models",
"available_models",
"max_output_tokens",
+ "api_type",
}
metadata_requested = bool(payload.model_fields_set & metadata_fields)
@@ -381,6 +388,7 @@ async def update_provider_config(
is_enabled = payload.is_enabled,
models = payload.models,
available_models = payload.available_models,
+ api_type = payload.api_type,
)
if max_output_tokens_requested:
metadata_updates["max_output_tokens"] = payload.max_output_tokens
@@ -393,6 +401,7 @@ async def update_provider_config(
models = existing.get("models") or [],
available_models = existing.get("available_models") or [],
max_output_tokens = existing.get("max_output_tokens"),
+ api_type = existing.get("api_type", "chat_completions"),
)
def _current_matches(current: dict, field: str, written) -> bool:
@@ -588,27 +597,63 @@ def _bind_saved_provider_target(payload):
update = {
"provider_type": config["provider_type"],
"base_url": config["base_url"],
+ "api_type": config.get("api_type", "chat_completions"),
}
)
-async def _test_custom_provider_connectivity(client, model_id: str) -> ProviderTestResult:
+async def _test_custom_provider_connectivity(
+ client,
+ model_id: str,
+ api_type: str = "chat_completions",
+) -> ProviderTestResult:
"""Probe a custom OpenAI-compatible endpoint without assuming /chat/completions. TTS-only gateways such as
Kokoro expose ``/models`` and ``/audio/speech`` but not ``/chat/completions``. Try those first, then fall
back to a chat probe."""
model_id = (model_id or "").strip()
models_error: Exception | None = None
+ models = None
try:
models = await client.list_models()
+ except Exception as exc:
+ models_error = exc
+
+ if models is not None and api_type != "responses":
return ProviderTestResult(
success = True,
message = f"Connected successfully. Found {len(models)} model(s).",
models_count = len(models),
)
- except Exception as exc:
- models_error = exc
+
+ responses_model_ids: list[str] = []
+ if model_id and api_type == "responses":
+ responses_model_ids = [model_id]
+ elif api_type == "responses" and models is not None:
+ for model in models:
+ candidate = model.get("id") if isinstance(model, dict) else None
+ candidate = candidate.strip() if isinstance(candidate, str) else ""
+ if candidate and candidate not in responses_model_ids:
+ responses_model_ids.append(candidate)
+ if len(responses_model_ids) >= _MAX_RESPONSES_CONNECTIVITY_MODELS:
+ break
+ if not responses_model_ids:
+ return ProviderTestResult(
+ success = False,
+ message = (
+ "Connection failed: /models responded, but no model ID was available "
+ "to test the Responses endpoint."
+ ),
+ models_count = len(models),
+ )
+ model_id = responses_model_ids[0]
if not model_id:
+ if models is not None:
+ return ProviderTestResult(
+ success = True,
+ message = f"Connected successfully. Found {len(models)} model(s).",
+ models_count = len(models),
+ )
return ProviderTestResult(
success = False,
message = (
@@ -618,6 +663,56 @@ async def _test_custom_provider_connectivity(client, model_id: str) -> ProviderT
models_count = None,
)
+ if api_type == "responses":
+ last_failure = "Responses endpoint returned no completion."
+ for response_model_id in responses_model_ids:
+ try:
+ received_response = False
+ model_failure: str | None = None
+ response_stream = client.stream_chat_completion(
+ messages = [{"role": "user", "content": "ping"}],
+ model = response_model_id,
+ temperature = None,
+ top_p = None,
+ max_tokens = 16,
+ )
+
+ async def consume_response_stream():
+ nonlocal received_response, model_failure
+ async for line in response_stream:
+ if line.startswith("data: ") and line[6:].strip() != "[DONE]":
+ event = json.loads(line[6:])
+ received_response = received_response or bool(event.get("choices"))
+ if event.get("error"):
+ model_failure = event["error"].get(
+ "message", "Responses request failed"
+ )
+
+ try:
+ await asyncio.wait_for(
+ consume_response_stream(),
+ timeout = _PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS,
+ )
+ finally:
+ await response_stream.aclose()
+ if received_response and model_failure is None:
+ return ProviderTestResult(
+ success = True,
+ message = "Connected successfully. Responses endpoint responded.",
+ )
+ last_failure = model_failure or "Responses endpoint returned no completion."
+ except asyncio.TimeoutError:
+ last_failure = (
+ "Responses endpoint timed out after "
+ f"{_PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS:g} seconds."
+ )
+ except Exception as exc:
+ last_failure = safe_curated_detail(exc)
+ return ProviderTestResult(
+ success = False,
+ message = f"Connection failed: {last_failure}",
+ )
+
try:
await client.create_speech(
text = ".",
@@ -704,12 +799,15 @@ async def test_provider(
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
- timeout = 15.0,
+ api_type = payload.api_type,
+ timeout = _PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS,
)
try:
if payload.provider_type == "custom":
- return await _test_custom_provider_connectivity(client, payload.model_id or "")
+ return await _test_custom_provider_connectivity(
+ client, payload.model_id or "", payload.api_type
+ )
if info.get("model_list_mode") == "curated":
await client.verify_models_endpoint_lightweight()
return ProviderTestResult(
@@ -843,6 +941,7 @@ async def list_provider_model_capabilities(
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
+ api_type = payload.api_type,
timeout = 15.0,
)
try:
@@ -910,6 +1009,7 @@ async def list_provider_models(
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
+ api_type = payload.api_type,
timeout = 15.0,
)
diff --git a/studio/backend/storage/providers_db.py b/studio/backend/storage/providers_db.py
index b93f156f1cd..6ff5776a8f0 100644
--- a/studio/backend/storage/providers_db.py
+++ b/studio/backend/storage/providers_db.py
@@ -20,6 +20,7 @@ from collections.abc import Iterator
from contextlib import contextmanager
from datetime import datetime, timezone
from typing import Optional
+from urllib.parse import urlparse
logger = logging.getLogger(__name__)
@@ -29,6 +30,50 @@ _schema_lock = threading.Lock()
_schema_ready: set[Path] = set()
_UNSET = object()
+_LEGACY_CUSTOM_PRESET_TYPES = {
+ "custom": "custom",
+ "llama.cpp": "llama_cpp",
+ "vllm": "vllm",
+ "ollama": "ollama",
+}
+
+
+def _migrate_legacy_custom_provider_types(conn: sqlite3.Connection) -> None:
+ """Align rows created before Custom had its own backend provider types.
+
+ Older Studio builds saved every OpenAI-compatible connection as ``openai``. A
+ non-OpenAI URL may still be an OpenAI reverse proxy, so only an exact built-in
+ custom/preset label is sufficient evidence to change the stored provider type.
+ """
+ updates: list[tuple[str, str]] = []
+ rows = conn.execute(
+ "SELECT id, display_name, base_url FROM llm_providers WHERE provider_type = 'openai'"
+ ).fetchall()
+ for provider_id, display_name, base_url in rows:
+ label = str(display_name or "").strip().lower()
+ url = str(base_url or "").strip()
+ try:
+ parsed = urlparse(url)
+ host = (parsed.hostname or "").lower()
+ except ValueError:
+ host = ""
+
+ # A display name is user-editable metadata. Never let it turn an OpenAI-managed
+ # endpoint into a custom connection, even if it happens to equal a preset label.
+ if host == "api.openai.com" or host.endswith(
+ (".openai.azure.com", ".services.ai.azure.com")
+ ):
+ continue
+
+ migrated_type = _LEGACY_CUSTOM_PRESET_TYPES.get(label)
+ if migrated_type is not None:
+ updates.append((migrated_type, str(provider_id)))
+
+ conn.executemany(
+ "UPDATE llm_providers SET provider_type = ? WHERE id = ?",
+ updates,
+ )
+
def _encode_models_json(models: Optional[list[str]]) -> str:
if not models:
@@ -79,8 +124,16 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
conn.execute(
"ALTER TABLE llm_providers ADD COLUMN available_models_json TEXT NOT NULL DEFAULT '[]'"
)
+ added_api_type = "api_type" not in existing_cols
+ if added_api_type:
+ conn.execute(
+ "ALTER TABLE llm_providers ADD COLUMN api_type TEXT NOT NULL DEFAULT 'chat_completions'"
+ )
+ _migrate_legacy_custom_provider_types(conn)
if "max_output_tokens" not in existing_cols:
conn.execute("ALTER TABLE llm_providers ADD COLUMN max_output_tokens INTEGER")
+ # ALTER TABLE persists independently in SQLite, but its one-time data migration does not.
+ conn.commit()
def reset_schema_state_for_tests() -> None:
@@ -139,6 +192,7 @@ def create_provider(
models: Optional[list[str]] = None,
available_models: Optional[list[str]] = None,
max_output_tokens: Optional[int] = None,
+ api_type: str = "chat_completions",
) -> None:
"""Insert a new provider configuration."""
now = datetime.now(timezone.utc).isoformat()
@@ -148,10 +202,10 @@ def create_provider(
"""
INSERT INTO llm_providers (
id, provider_type, display_name, base_url,
- models_json, available_models_json, max_output_tokens,
+ models_json, available_models_json, max_output_tokens, api_type,
created_at, updated_at
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
id,
@@ -161,6 +215,7 @@ def create_provider(
_encode_models_json(models),
_encode_models_json(available_models),
max_output_tokens,
+ api_type,
now,
now,
),
@@ -180,6 +235,7 @@ def update_provider(
max_output_tokens: int | None | object = _UNSET,
*,
connection: sqlite3.Connection | None = None,
+ api_type: str | None = None,
) -> bool:
"""Update fields on an existing provider. Returns True if a row was updated."""
updates = []
@@ -190,6 +246,9 @@ def update_provider(
if base_url is not None:
updates.append("base_url = ?")
params.append(base_url)
+ if api_type is not None:
+ updates.append("api_type = ?")
+ params.append(api_type)
if is_enabled is not None:
updates.append("is_enabled = ?")
params.append(1 if is_enabled else 0)
diff --git a/studio/backend/tests/test_control_markup_neutralize_7066.py b/studio/backend/tests/test_control_markup_neutralize_7066.py
index 490f70c37e9..c0af25c0a22 100644
--- a/studio/backend/tests/test_control_markup_neutralize_7066.py
+++ b/studio/backend/tests/test_control_markup_neutralize_7066.py
@@ -2401,7 +2401,7 @@ def test_self_hosted_openai_compatible_providers_are_swept():
_REPO_ROOT / "studio" / "backend" / "core" / "inference" / "external_provider.py"
).read_text(encoding = "utf-8")
# The sweep has to sit before the body is built, so both messages and tools are covered.
- sweep = source.index("_TEMPLATE_APPLYING_PROVIDERS:\n")
+ sweep = source.index("_TEMPLATE_APPLYING_PROVIDERS and not managed_custom_responses:\n")
body = source.index('body: dict[str, Any] = {\n "model": model,')
assert sweep < body
for call in (
diff --git a/studio/backend/tests/test_custom_provider_api_type.py b/studio/backend/tests/test_custom_provider_api_type.py
new file mode 100644
index 00000000000..daf04b65ef5
--- /dev/null
+++ b/studio/backend/tests/test_custom_provider_api_type.py
@@ -0,0 +1,240 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The persisted API choice reaches the right wire protocol and response shape."""
+
+import asyncio
+import json
+from types import SimpleNamespace
+
+import httpx
+import pytest
+from pydantic import ValidationError
+
+from core.inference import external_provider as ep
+from core.inference.external_provider import ExternalProviderClient
+from models.inference import ChatCompletionRequest, ChatMessage
+from models.providers import ProviderCreate, ProviderUpdate
+from storage import providers_db
+
+
[email protected]("api_type", ["chat_completions", "responses"])
+def test_api_type_persists_through_edit_and_reload(tmp_path, monkeypatch, api_type):
+ monkeypatch.setattr(providers_db, "studio_db_path", lambda: tmp_path / "studio.db")
+ providers_db.reset_schema_state_for_tests()
+ payload = ProviderCreate(
+ provider_type = "custom",
+ display_name = "Gateway",
+ base_url = "https://gateway.example/v1",
+ api_type = api_type,
+ )
+ providers_db.create_provider(id = "gateway", **payload.model_dump(exclude = {"encrypted_api_key"}))
+ providers_db.update_provider("gateway", display_name = "Renamed")
+ providers_db.reset_schema_state_for_tests()
+ assert providers_db.get_provider("gateway")["api_type"] == api_type
+ changed = "responses" if api_type == "chat_completions" else "chat_completions"
+ providers_db.update_provider(
+ "gateway", **ProviderUpdate(api_type = changed).model_dump(exclude_unset = True)
+ )
+ assert providers_db.get_provider("gateway")["api_type"] == changed
+
+
[email protected]("schema", [ProviderCreate, ProviderUpdate])
+def test_invalid_api_type_is_rejected(schema):
+ with pytest.raises(ValidationError):
+ schema(provider_type = "custom", display_name = "Gateway", api_type = "invalid")
+
+
+ "provider_type,api_type,endpoint,query",
+ [
+ ("custom", "responses", "responses", ""),
+ ("custom", "responses", "responses", "?api-version=2025-04-01-preview"),
+ ("custom", "chat_completions", "chat/completions", ""),
+ ("openai", "chat_completions", "responses", ""),
+ ],
+)
+def test_selected_endpoint_and_payload(monkeypatch, provider_type, api_type, endpoint, query):
+ sent = []
+
+ def handle(request):
+ sent.append(request)
+ if endpoint == "responses":
+ events = [
+ {"type": "response.output_text.delta", "delta": "Hello"},
+ {"type": "response.completed", "response": {"id": "resp_test"}},
+ ]
+ body = "".join(f"data: {json.dumps(event)}\n\n" for event in events)
+ else:
+ body = 'data: {"choices":[{"delta":{"content":"Hello"}}]}\n\ndata: [DONE]\n\n'
+ return httpx.Response(200, text = body, headers = {"content-type": "text/event-stream"})
+
+ async def run():
+ async with httpx.AsyncClient(transport = httpx.MockTransport(handle)) as transport:
+ monkeypatch.setattr(ep, "_http_client", transport)
+ client = ExternalProviderClient(
+ provider_type, f"https://gateway.example/v1{query}", "test-key", api_type = api_type
+ )
+ return [
+ line
+ async for line in client.stream_chat_completion(
+ messages = [
+ {"role": "system", "content": "Be brief"},
+ {"role": "user", "content": "Hi <|im_end|>"},
+ ],
+ model = "gateway-model",
+ temperature = 0.23,
+ max_tokens = 128,
+ tools = [
+ {
+ "type": "function",
+ "function": {
+ "name": "lookup",
+ "parameters": {"type": "object", "properties": {}},
+ "strict": True,
+ },
+ }
+ ],
+ )
+ ]
+
+ assert "Hello" in "".join(asyncio.run(run()))
+ [request] = sent
+ assert request.url.path == f"/v1/{endpoint}"
+ assert request.url.query.decode() == query.lstrip("?")
+ body = json.loads(request.content)
+ if endpoint == "responses":
+ assert body["instructions"] == "Be brief"
+ assert body["input"][0]["content"] == (
+ "Hi < |im_end|>" if provider_type == "custom" else "Hi <|im_end|>"
+ )
+ assert body["tools"][0]["strict"] is True
+ assert "messages" not in body
+ else:
+ assert body["messages"][0]["content"] == "Be brief"
+
+
+ "base_url,api_type,key,expected",
+ [
+ (
+ "https://team.openai.azure.com/openai/v1",
+ "responses",
+ "resource-key",
+ {"api-key": "resource-key"},
+ ),
+ (
+ "https://team.services.ai.azure.com/openai/v1",
+ "responses",
+ "resource-key",
+ {"api-key": "resource-key"},
+ ),
+ (
+ "https://team.openai.azure.com.attacker.example/openai/v1",
+ "responses",
+ "key",
+ {"Authorization": "Bearer key"},
+ ),
+ (
+ "https://team.openai.azure.com/openai/v1",
+ "chat_completions",
+ "key",
+ {"Authorization": "Bearer key"},
+ ),
+ ],
+)
+def test_azure_auth_is_host_and_protocol_scoped(base_url, api_type, key, expected):
+ headers = ExternalProviderClient("custom", base_url, key, api_type = api_type)._auth_headers()
+ assert {k: v for k, v in headers.items() if k in ("api-key", "Authorization")} == expected
+
+
[email protected]("upstream_status", [200, 429])
+def test_non_stream_route_returns_json_or_upstream_error(monkeypatch, upstream_status):
+ from core.inference.api_monitor import ApiMonitor
+ from routes import inference
+
+ def handle(request):
+ assert request.url.path == "/v1/responses"
+ assert json.loads(request.content)["stream"] is False
+ if upstream_status != 200:
+ return httpx.Response(429, json = {"error": {"message": "rate limited"}})
+ return httpx.Response(
+ 200,
+ json = {
+ "id": "resp_route",
+ "status": "completed",
+ "output": [
+ {"type": "message", "content": [{"type": "output_text", "text": "Hello"}]}
+ ],
+ },
+ )
+
+ async def run():
+ async with httpx.AsyncClient(transport = httpx.MockTransport(handle)) as transport:
+ monkeypatch.setattr(ep, "_http_client", transport)
+ monitor = ApiMonitor(max_entries = 3)
+ monkeypatch.setattr(inference, "api_monitor", monitor)
+ payload = ChatCompletionRequest(
+ messages = [{"role": "user", "content": "Hi"}],
+ stream = False,
+ provider_type = "custom",
+ provider_base_url = "https://gateway.example/v1",
+ provider_api_type = "responses",
+ external_model = "gateway-model",
+ )
+
+ async def disconnected():
+ return False
+
+ request = SimpleNamespace(
+ headers = {},
+ state = SimpleNamespace(skip_api_monitor = False),
+ url = SimpleNamespace(path = "/v1/chat/completions"),
+ method = "POST",
+ is_disconnected = disconnected,
+ )
+ return await inference._proxy_to_external_provider(payload, request), monitor
+
+ response, monitor = asyncio.run(run())
+ assert response.status_code == upstream_status
+ body = json.loads(response.body)
+ if upstream_status == 200:
+ assert body["choices"][0]["message"]["content"] == "Hello"
+ assert monitor.snapshot()[0]["status"] == "completed"
+ else:
+ assert "rate limited" in body["error"]["message"]
+ assert body["error"]["code"] == "429"
+ assert monitor.active_count() == 0
+
+
+def test_responses_follow_up_preserves_reasoning_metadata():
+ from routes.inference import _build_external_messages
+
+ reasoning = {
+ "openai_responses_reasoning": [
+ {
+ "type": "reasoning",
+ "id": "rs_1",
+ "encrypted_content": "enc_blob",
+ }
+ ]
+ }
+ messages = [
+ ChatMessage(
+ role = "assistant",
+ content = None,
+ tool_calls = [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {"name": "lookup", "arguments": "{}"},
+ }
+ ],
+ extra_content = reasoning,
+ ),
+ ChatMessage(role = "tool", tool_call_id = "call_1", content = "result"),
+ ]
+ built = _build_external_messages(
+ messages, supports_vision = False, provider_type = "custom", api_type = "responses"
+ )
+ assert built[0]["extra_content"] == reasoning
diff --git a/studio/backend/tests/test_external_hosted_tool_selection.py b/studio/backend/tests/test_external_hosted_tool_selection.py
index aa8dd4be2a9..3c483c178fd 100644
--- a/studio/backend/tests/test_external_hosted_tool_selection.py
+++ b/studio/backend/tests/test_external_hosted_tool_selection.py
@@ -14,6 +14,8 @@ the loop is up, so forwarding them too would run both sides of one tool and bill
the provider for its half.
"""
+from __future__ import annotations
+
import asyncio
from types import SimpleNamespace
@@ -59,7 +61,13 @@ def _request():
)
-def _install(monkeypatch, provider_type: str):
+def _install(
+ monkeypatch,
+ provider_type: str,
+ *,
+ base_url = None,
+ api_type = None,
+):
from core.inference.providers import get_base_url
from routes import inference as inf
@@ -69,7 +77,8 @@ def _install(monkeypatch, provider_type: str):
lambda _pid: {
"id": _pid,
"provider_type": provider_type,
- "base_url": get_base_url(provider_type) or "http://127.0.0.1:8080/v1",
+ "base_url": base_url or get_base_url(provider_type) or "http://127.0.0.1:8080/v1",
+ "api_type": api_type or "chat_completions",
"display_name": "Saved connection",
"is_enabled": True,
},
@@ -78,6 +87,9 @@ def _install(monkeypatch, provider_type: str):
monkeypatch.setattr(inf, "ExternalProviderClient", _FakeExternalClient)
def _loop_raiser(transport, **_kwargs):
+ transport._selected_local_tool_names = [
+ tool["function"]["name"] for tool in _kwargs["policy"].tools
+ ]
raise _LoopEntered(transport)
monkeypatch.setattr(inf, "stream_with_studio_tools", _loop_raiser)
@@ -98,9 +110,17 @@ def _payload(**overrides):
return ChatCompletionRequest(**base)
-def _loop_transport(monkeypatch, provider_type: str, selection: list[str], **overrides):
+def _loop_transport(
+ monkeypatch,
+ provider_type: str,
+ selection: list[str],
+ *,
+ base_url: str | None = None,
+ api_type: str | None = None,
+ **overrides,
+):
"""Run the route and return the transport the loop was handed."""
- inf = _install(monkeypatch, provider_type)
+ inf = _install(monkeypatch, provider_type, base_url = base_url, api_type = api_type)
async def go():
resp = await inf._proxy_to_external_provider(
@@ -204,3 +224,29 @@ def test_the_loop_keeps_its_own_search(monkeypatch):
def test_a_self_hosted_loop_is_still_sent_no_tool_flags(monkeypatch):
transport = _loop_transport(monkeypatch, "llama_cpp", ["web_search", "python"])
assert transport._request_kwargs["enabled_tools"] is None
+
+
+ "base_url,expected_hosted_tools",
+ [
+ ("https://api.openai.com/v1", ["code_execution", "image_generation"]),
+ (
+ "https://resource.services.ai.azure.com/openai/v1",
+ ["code_execution", "image_generation"],
+ ),
+ ("https://gateway.example/v1", None),
+ ("https://api.openai.com.attacker.example/v1", None),
+ ],
+)
+def test_custom_mixed_tools_keep_local_search_and_scope_hosted_tools(
+ monkeypatch, base_url, expected_hosted_tools
+):
+ transport = _loop_transport(
+ monkeypatch,
+ "custom",
+ ["web_search", "code_execution", "image_generation"],
+ base_url = base_url,
+ api_type = "responses",
+ )
+ assert "web_search" in transport._selected_local_tool_names
+ assert transport._request_kwargs["enabled_tools"] == expected_hosted_tools
diff --git a/studio/backend/tests/test_external_provider_usage_chunk.py b/studio/backend/tests/test_external_provider_usage_chunk.py
index 3ab2a09fd74..84bf77e693a 100644
--- a/studio/backend/tests/test_external_provider_usage_chunk.py
+++ b/studio/backend/tests/test_external_provider_usage_chunk.py
@@ -62,7 +62,11 @@ def test_build_usage_chunk_openai_shape():
{
"input_tokens": 5507,
"output_tokens": 252,
- "input_tokens_details": {"cached_tokens": 4736},
+ "input_tokens_details": {"cached_tokens": 4736, "cache_write_tokens": 64},
+ "output_tokens_details": {
+ "reasoning_tokens": 12,
+ "accepted_prediction_tokens": 9,
+ },
},
)
assert line is not None
@@ -72,6 +76,11 @@ def test_build_usage_chunk_openai_shape():
assert usage["completion_tokens"] == 252
assert usage["total_tokens"] == 5759
assert usage["prompt_tokens_details"]["cached_tokens"] == 4736
+ assert usage["prompt_tokens_details"]["cache_write_tokens"] == 64
+ assert usage["completion_tokens_details"] == {
+ "reasoning_tokens": 12,
+ "accepted_prediction_tokens": 9,
+ }
# Anthropic-only keys must not leak onto the OpenAI shape.
assert "cache_creation_input_tokens" not in usage
assert "cache_read_input_tokens" not in usage
diff --git a/studio/backend/tests/test_legacy_custom_provider_migration.py b/studio/backend/tests/test_legacy_custom_provider_migration.py
new file mode 100644
index 00000000000..f9ad936e519
--- /dev/null
+++ b/studio/backend/tests/test_legacy_custom_provider_migration.py
@@ -0,0 +1,45 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Legacy rows migrate by trusted endpoint and label, never a lookalike host."""
+
+import sqlite3
+
+from storage import providers_db
+
+
+def test_legacy_rows_migrate_without_promoting_spoofed_hosts(tmp_path, monkeypatch):
+ db = tmp_path / "studio.db"
+ rows = [
+ ("openai", "OpenAI", "https://api.openai.com/v1", "openai"),
+ ("azure", "Custom", "https://team.services.ai.azure.com/openai/v1", "openai"),
+ (
+ "spoof",
+ "Custom",
+ "https://team.services.ai.azure.com.attacker.example/openai/v1",
+ "custom",
+ ),
+ ("gateway", "Custom", "https://gateway.example/v1", "custom"),
+ ("vllm", "vLLM", "http://localhost:8000/v1", "vllm"),
+ ]
+ with sqlite3.connect(db) as conn:
+ conn.execute(
+ "CREATE TABLE llm_providers (id TEXT PRIMARY KEY, provider_type TEXT, "
+ "display_name TEXT, base_url TEXT, is_enabled INTEGER DEFAULT 1, "
+ "created_at TEXT, updated_at TEXT)"
+ )
+ conn.executemany(
+ "INSERT INTO llm_providers "
+ "(id, provider_type, display_name, base_url, created_at, updated_at) "
+ "VALUES (?, 'openai', ?, ?, 'created', 'unchanged')",
+ [(key, label, url) for key, label, url, _ in rows],
+ )
+ monkeypatch.setattr(providers_db, "studio_db_path", lambda: db)
+ monkeypatch.setattr(providers_db, "ensure_dir", lambda _path: None)
+ providers_db.reset_schema_state_for_tests()
+ migrated = {row["id"]: row for row in providers_db.list_providers()}
+ assert {key: migrated[key]["provider_type"] for key, _, _, _ in rows} == {
+ key: expected for key, _, _, expected in rows
+ }
+ assert all(row["api_type"] == "chat_completions" for row in migrated.values())
+ assert all(row["updated_at"] == "unchanged" for row in migrated.values())
diff --git a/studio/backend/tests/test_multimodal_document.py b/studio/backend/tests/test_multimodal_document.py
index 2a3d233d378..d12b826651b 100644
--- a/studio/backend/tests/test_multimodal_document.py
+++ b/studio/backend/tests/test_multimodal_document.py
@@ -467,10 +467,8 @@ def test_chat_message_accepts_input_document_part():
assert msg.content[1].media_type == "application/pdf"
-def test_build_external_messages_passes_input_document_for_anthropic_and_openai():
- # Both providers' stream helpers translate input_document (Anthropic ->
- # {type:"document"}, OpenAI Responses -> {type:"input_file"}), so the
- # part round-trips through the builder unchanged on those routes.
+def test_build_external_messages_passes_input_document_for_native_and_custom_responses():
+ # Each route translates input_document, so the builder keeps it.
from models.inference import ChatMessage
from routes.inference import _build_external_messages
@@ -490,8 +488,10 @@ def test_build_external_messages_passes_input_document_for_anthropic_and_openai(
}
)
]
- for provider in ("anthropic", "openai"):
- out = _build_external_messages(msgs, supports_vision = True, provider_type = provider)
+ for provider, api_type in (("anthropic", None), ("openai", None), ("custom", "responses")):
+ out = _build_external_messages(
+ msgs, supports_vision = True, provider_type = provider, api_type = api_type
+ )
assert len(out) == 1, (provider, out)
parts = out[0]["content"]
assert parts[0] == {"type": "text", "text": "summarise"}, provider
@@ -527,8 +527,11 @@ def test_build_external_messages_strips_input_document_for_unmapped_providers():
}
)
]
- for provider in ("gemini", "mistral", "kimi", "openrouter", "deepseek", "qwen"):
- out = _build_external_messages(msgs, supports_vision = True, provider_type = provider)
+ for provider in ("gemini", "mistral", "kimi", "openrouter", "deepseek", "qwen", "custom"):
+ api_type = "chat_completions" if provider == "custom" else None
+ out = _build_external_messages(
+ msgs, supports_vision = True, provider_type = provider, api_type = api_type
+ )
assert len(out) == 1, (provider, out)
parts = out[0]["content"]
types = [p.get("type") for p in parts if isinstance(p, dict)]
diff --git a/studio/backend/tests/test_openai_compaction.py b/studio/backend/tests/test_openai_compaction.py
index 7099517c906..15b5fb8561f 100644
--- a/studio/backend/tests/test_openai_compaction.py
+++ b/studio/backend/tests/test_openai_compaction.py
@@ -151,7 +151,7 @@ def test_azure_openai_mixed_case_base_url_matches(monkeypatch):
def test_cloud_gate_uses_hostname_not_substring(monkeypatch):
# CodeQL py/incomplete-url-substring-sanitization: an attacker
# controlling base_url could embed `api.openai.com` or
- # `.openai.azure.com` in a path or subdomain on an arbitrary host to
+ # an Azure managed suffix in a path or subdomain on an arbitrary host to
# slip cloud-only body fields to their own server. The
# hostname-anchored helper must reject both shapes.
for evil in [
@@ -159,6 +159,9 @@ def test_cloud_gate_uses_hostname_not_substring(monkeypatch):
"https://api.openai.com.attacker.com/v1",
"https://attacker.com/.openai.azure.com/v1",
"https://my-resource.openai.azure.com.attacker.com/openai/v1",
+ "https://attacker.com/.services.ai.azure.com/openai/v1",
+ "https://my-resource.services.ai.azure.com.attacker.com/openai/v1",
+ "https://[email protected]/openai/v1",
]:
captured = _capture_at_threshold(monkeypatch, base_url = evil)
assert "context_management" not in captured["body"], evil
diff --git a/studio/backend/tests/test_openai_container_crud.py b/studio/backend/tests/test_openai_container_crud.py
index e95f36471f9..7ff4932864d 100644
--- a/studio/backend/tests/test_openai_container_crud.py
+++ b/studio/backend/tests/test_openai_container_crud.py
@@ -268,6 +268,7 @@ def test_external_chat_explicit_key_honors_edited_target(monkeypatch):
"provider_type": "custom",
"base_url": "https://new.example/v1",
"api_key": "replacement-key",
+ "api_type": "chat_completions",
}
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index f8c700e1e4b..86c069a553b 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -5779,6 +5779,7 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({
selectedExternalProvider?.isReasoningModel === true,
// Lets the resolver detect custom Gemini OAI-compat gateways.
baseUrl: selectedExternalProvider?.baseUrl ?? null,
+ apiType: selectedExternalProvider?.apiType,
},
)
: null;
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 78efb1a410f..29db7597ad7 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -144,6 +144,7 @@ import {
codexLocalToolRoundId,
codexReasoningForToolCalls,
readCodexReasoning,
+ readOpenAIResponsesReasoning,
shouldReplayAssistantReasoning,
startsNewCodexToolRound,
type CodexReasoningLedger,
@@ -1191,6 +1192,20 @@ function setAssistantCodexReasoning(
message.extra_content = { ...extra, openai_codex_reasoning: reasoning };
}
+function setAssistantOpenAIResponsesReasoning(
+ message: SerializedMessage,
+ reasoning: unknown[] | undefined,
+): void {
+ if (!reasoning) return;
+ const extra =
+ message.extra_content &&
+ typeof message.extra_content === "object" &&
+ !Array.isArray(message.extra_content)
+ ? (message.extra_content as Record<string, unknown>)
+ : {};
+ message.extra_content = { ...extra, openai_responses_reasoning: reasoning };
+}
+
function attachAssistantThoughtSignature(
messages: SerializedMessage[],
thoughtSignature: string | undefined,
@@ -1233,6 +1248,9 @@ function serializeAssistantReplayMessages(
const codexReasoning = readCodexReasoning(
(message as { metadata?: unknown }).metadata,
);
+ const openAIResponsesReasoning = readOpenAIResponsesReasoning(
+ (message as { metadata?: unknown }).metadata,
+ );
const messages: SerializedMessage[] = [];
const pendingTextParts: string[] = [];
const pendingReasoningParts: string[] = [];
@@ -1290,6 +1308,13 @@ function serializeAssistantReplayMessages(
pendingToolCalls.map((call) => call.id),
),
);
+ setAssistantOpenAIResponsesReasoning(
+ assistantMessage,
+ codexReasoningForToolCalls(
+ openAIResponsesReasoning,
+ pendingToolCalls.map((call) => call.id),
+ ),
+ );
}
messages.push(assistantMessage);
@@ -4841,6 +4866,7 @@ export function createOpenAIStreamAdapter(
externalProvider.providerType,
externalSelection.modelId,
externalProvider.baseUrl,
+ externalProvider.apiType,
),
);
// Per-model Search/Code allowances live in providerSupportsBuiltin*; this flag only signals image-mode.
@@ -4866,6 +4892,7 @@ export function createOpenAIStreamAdapter(
externalProvider.providerType,
externalSelection.modelId,
externalProvider.baseUrl,
+ externalProvider.apiType,
),
);
// Fetch is independent of Search (Anthropic bills web_fetch separately); forced off in
@@ -4889,6 +4916,8 @@ export function createOpenAIStreamAdapter(
hostedCodeExecutionForThisTurn: codeExecEnabledForThisTurn,
providerHostsCodeExecution: providerHostsCodeExecution(
externalProvider?.providerType,
+ externalProvider?.baseUrl,
+ externalProvider?.apiType,
),
});
@@ -5416,6 +5445,9 @@ export function createOpenAIStreamAdapter(
// Every streamed yield carries the repaired text: assistant-ui drops whatever is yielded after
// an abort, so on Stop the last STREAMED yield is what gets saved.
let codexReasoningLedger: CodexReasoningLedger = { byToolCall: {} };
+ let openAIResponsesReasoningLedger: CodexReasoningLedger = {
+ byToolCall: {},
+ };
let codexRoundToolCallIds: string[] = [];
let contextTruncation: OpenAIChatChunk["context_truncated"];
@@ -5428,6 +5460,7 @@ export function createOpenAIStreamAdapter(
const liveCustom = () => ({
...reasoningDurationTracker.metadata(),
openaiCodexReasoning: codexReasoningLedger,
+ openaiResponsesReasoning: openAIResponsesReasoningLedger,
contextTruncation,
// A legacy (browser-tool / attachment / incognito) run that ends because you closed the tab has no
// server-side run to resume from, so its last streamed yield is what persists. Mark it an interruption
@@ -5808,6 +5841,7 @@ export function createOpenAIStreamAdapter(
{
isReasoningProvider: externalProvider.isReasoningModel === true,
baseUrl: externalProvider.baseUrl ?? null,
+ apiType: externalProvider.apiType,
},
)
: {
@@ -6067,6 +6101,9 @@ export function createOpenAIStreamAdapter(
});
const externalCapabilities = getProviderCapabilities(
externalProvider?.providerType,
+ externalProvider?.apiType,
+ externalSelection?.modelId,
+ externalProvider?.baseUrl,
);
const buildRequestPayload = async (
forceRefreshPublicKey = false,
@@ -6352,6 +6389,7 @@ export function createOpenAIStreamAdapter(
}
: {}),
provider_base_url: externalProvider.baseUrl || null,
+ provider_api_type: externalProvider.apiType ?? "chat_completions",
...(openaiCodeExecContainerId
? {
openai_code_exec_container_id: openaiCodeExecContainerId,
@@ -7270,6 +7308,19 @@ export function createOpenAIStreamAdapter(
);
replayStateChanged = true;
}
+ const openAIResponsesReasoning =
+ extraRecord.openai_responses_reasoning;
+ if (
+ Array.isArray(openAIResponsesReasoning) &&
+ openAIResponsesReasoning.length > 0
+ ) {
+ openAIResponsesReasoningLedger = addCodexReasoning(
+ openAIResponsesReasoningLedger,
+ openAIResponsesReasoning,
+ codexRoundToolCallIds,
+ );
+ replayStateChanged = true;
+ }
}
if (chunk.choices?.[0]?.finish_reason) {
@@ -8060,6 +8111,7 @@ export function createOpenAIStreamAdapter(
// Persisted so Continue survives a reload; cleared on a normal end.
openaiCodexReasoning: codexReasoningLedger,
+ openaiResponsesReasoning: openAIResponsesReasoningLedger,
contextTruncation,
incomplete: finalIncompleteReason
? { reason: finalIncompleteReason }
diff --git a/studio/frontend/src/features/chat/api/providers-api.ts b/studio/frontend/src/features/chat/api/providers-api.ts
index 4c86b35d7e6..e273045802a 100644
--- a/studio/frontend/src/features/chat/api/providers-api.ts
+++ b/studio/frontend/src/features/chat/api/providers-api.ts
@@ -35,7 +35,10 @@ export interface ProviderRegistryEntry {
model_ids_editable?: boolean;
}
+export type ProviderApiType = "chat_completions" | "responses";
+
export interface ProviderConfig {
+ api_type?: ProviderApiType;
id: string;
provider_type: string;
display_name: string;
@@ -202,6 +205,7 @@ export async function createProviderConfig(payload: {
providerType: string;
displayName: string;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
models?: string[];
availableModels?: string[];
maxOutputTokens?: number | null;
@@ -215,6 +219,7 @@ export async function createProviderConfig(payload: {
provider_type: payload.providerType,
display_name: payload.displayName,
base_url: payload.baseUrl ?? null,
+ ...(payload.apiType === undefined ? {} : { api_type: payload.apiType }),
models: payload.models ?? [],
available_models: payload.availableModels ?? [],
...(payload.maxOutputTokens === undefined
@@ -247,6 +252,7 @@ export async function updateProviderConfig(
payload: {
displayName?: string;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
isEnabled?: boolean;
models?: string[];
availableModels?: string[];
@@ -262,6 +268,7 @@ export async function updateProviderConfig(
body: JSON.stringify({
...(payload.displayName === undefined ? {} : { display_name: payload.displayName }),
...(payload.baseUrl === undefined ? {} : { base_url: payload.baseUrl }),
+ ...(payload.apiType === undefined ? {} : { api_type: payload.apiType }),
...(payload.isEnabled === undefined ? {} : { is_enabled: payload.isEnabled }),
...(payload.models === undefined ? {} : { models: payload.models }),
...(payload.availableModels === undefined
@@ -320,6 +327,7 @@ export async function testProviderConnection(payload: {
providerId?: string | null;
apiKey: string;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
modelId?: string | null;
}): Promise<ProviderTestResult> {
return withApiKeyEncryptionRetry(payload.apiKey, async (encryptedApiKey) => {
@@ -332,6 +340,7 @@ export async function testProviderConnection(payload: {
provider_id: payload.providerId ?? null,
encrypted_api_key: encryptedApiKey,
base_url: payload.baseUrl ?? null,
+ ...(payload.apiType === undefined ? {} : { api_type: payload.apiType }),
model_id: payload.modelId ?? null,
}),
});
@@ -345,6 +354,7 @@ export async function listProviderModels(payload: {
providerId?: string | null;
apiKey: string;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
}): Promise<ProviderModelInfo[]> {
return withApiKeyEncryptionRetry(payload.apiKey, async (encryptedApiKey) => {
const response = await authFetch("/api/providers/models", {
@@ -356,6 +366,7 @@ export async function listProviderModels(payload: {
provider_id: payload.providerId ?? null,
encrypted_api_key: encryptedApiKey,
base_url: payload.baseUrl ?? null,
+ ...(payload.apiType === undefined ? {} : { api_type: payload.apiType }),
}),
});
return parseJsonOrThrow<ProviderModelInfo[]>(response);
@@ -372,6 +383,7 @@ export async function listProviderModelCapabilities(payload: {
providerId?: string | null;
apiKey: string;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
}): Promise<ProviderModelCapabilityInfo[]> {
return withApiKeyEncryptionRetry(payload.apiKey, async (encryptedApiKey) => {
const response = await authFetch("/api/providers/model-capabilities", {
@@ -382,6 +394,7 @@ export async function listProviderModelCapabilities(payload: {
provider_id: payload.providerId ?? null,
encrypted_api_key: encryptedApiKey,
base_url: payload.baseUrl ?? null,
+ ...(payload.apiType === undefined ? {} : { api_type: payload.apiType }),
}),
});
return parseJsonOrThrow<ProviderModelCapabilityInfo[]>(response);
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index e4545f83db5..445b13a6f2d 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -2483,7 +2483,12 @@ export function ChatPage({
const provider = externalProvidersForChat.find(
(p) => p.id === selection.providerId,
);
- const baseCapabilities = getProviderCapabilities(provider?.providerType);
+ const baseCapabilities = getProviderCapabilities(
+ provider?.providerType,
+ provider?.apiType,
+ selection.modelId,
+ provider?.baseUrl,
+ );
if (!baseCapabilities) return baseCapabilities;
const anthropicThinkingEnabled =
provider?.providerType === "anthropic" &&
@@ -2516,6 +2521,7 @@ export function ChatPage({
{
isReasoningProvider: provider?.isReasoningModel === true,
baseUrl: provider?.baseUrl ?? null,
+ apiType: provider?.apiType,
},
);
const state = useChatRuntimeStore.getState();
@@ -2529,6 +2535,7 @@ export function ChatPage({
const nextReasoningEffort = resolveExternalReasoningEffort({
caps: reasoningCaps,
providerType: provider?.providerType,
+ apiType: provider?.apiType,
// Same as the switch below: a checkpoint that changed without going through it leaves the
// previous model's pin in the live level, which an unpinned model must not inherit.
current:
@@ -2550,12 +2557,14 @@ export function ChatPage({
provider?.providerType,
selection.modelId,
provider?.baseUrl,
+ provider?.apiType,
);
const supportsBuiltinImageGeneration =
providerSupportsBuiltinImageGeneration(
provider?.providerType,
selection.modelId,
provider?.baseUrl,
+ provider?.apiType,
);
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
provider?.providerType,
@@ -2595,7 +2604,11 @@ export function ChatPage({
// provider that cannot use it runs nothing either way.
const canRunCode = codeToolCanRun({
hostedCodeExecutionForThisTurn: supportsBuiltinCodeExecution,
- providerHostsCodeExecution: providerHostsCodeExecution(provider?.providerType),
+ providerHostsCodeExecution: providerHostsCodeExecution(
+ provider?.providerType,
+ provider?.baseUrl,
+ provider?.apiType,
+ ),
supportsStudioTools: supportsStudioToolsHere,
});
const nextToolsEnabled = canSearch
@@ -2658,12 +2671,14 @@ export function ChatPage({
{
isReasoningProvider: provider?.isReasoningModel === true,
baseUrl: provider?.baseUrl ?? null,
+ apiType: provider?.apiType,
},
);
reconcilePinnedReasoningEffort({
checkpoint: inferenceParams.checkpoint,
caps,
providerType: provider?.providerType,
+ apiType: provider?.apiType,
});
}, [activePinnedEffort, externalProvidersForChat, inferenceParams.checkpoint]);
// A catalog that lands after selection refreshes only the stored reasoning fields (the effort shortcut reads them),
@@ -2688,6 +2703,7 @@ export function ChatPage({
{
isReasoningProvider: provider?.isReasoningModel === true,
baseUrl: provider?.baseUrl ?? null,
+ apiType: provider?.apiType,
},
);
useChatRuntimeStore.setState(
@@ -2699,6 +2715,7 @@ export function ChatPage({
checkpoint: inferenceParams.checkpoint,
caps,
providerType: provider?.providerType,
+ apiType: provider?.apiType,
});
}, [modelCatalogChange, inferenceParams.checkpoint]);
const canCompare = useMemo(() => {
@@ -3193,6 +3210,7 @@ export function ChatPage({
{
isReasoningProvider: selectedProvider?.isReasoningModel === true,
baseUrl: selectedProvider?.baseUrl ?? null,
+ apiType: selectedProvider?.apiType,
},
);
const effortLevels = reasoningCaps.reasoningEffortLevels;
@@ -3202,6 +3220,7 @@ export function ChatPage({
const nextReasoningEffort = resolveExternalReasoningEffort({
caps: reasoningCaps,
providerType: selectedProvider?.providerType,
+ apiType: selectedProvider?.apiType,
// The outgoing model's pin is what the live level holds, so an unpinned target resolves
// from the chat's own effort rather than inheriting one model's override.
current:
@@ -3230,12 +3249,14 @@ export function ChatPage({
selectedProvider?.providerType,
selectedExternal?.modelId,
selectedProvider?.baseUrl,
+ selectedProvider?.apiType,
);
const supportsBuiltinImageGeneration =
providerSupportsBuiltinImageGeneration(
selectedProvider?.providerType,
selectedExternal?.modelId,
selectedProvider?.baseUrl,
+ selectedProvider?.apiType,
);
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
selectedProvider?.providerType,
@@ -3274,6 +3295,8 @@ export function ChatPage({
hostedCodeExecutionForThisTurn: supportsBuiltinCodeExecution,
providerHostsCodeExecution: providerHostsCodeExecution(
selectedProvider?.providerType,
+ selectedProvider?.baseUrl,
+ selectedProvider?.apiType,
),
supportsStudioTools: supportsStudioToolsHere,
});
diff --git a/studio/frontend/src/features/chat/chat-providers-dialog.tsx b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
index fad636e8877..93d6c09d805 100644
--- a/studio/frontend/src/features/chat/chat-providers-dialog.tsx
+++ b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
@@ -45,6 +45,7 @@ import { OpenAICodexConnect } from "./openai-codex-connect";
import {
type CodexSubscriptionModels,
type ProviderAuthStatus,
+ type ProviderApiType,
type ProviderRegistryEntry,
createProviderConfig,
deleteProviderConfig,
@@ -149,6 +150,10 @@ function shouldAppendOpenAiVersionPath(providerType: string): boolean {
);
}
+function shouldShowProviderApiType(providerType: string): boolean {
+ return providerType === LEGACY_CUSTOM_PROVIDER_TYPE;
+}
+
function formatModelSummary(models: string[]): string {
if (models.length === 0) {
return "No models enabled";
@@ -250,6 +255,7 @@ export function ChatProvidersSettings({
const [clearApiKeyRequested, setClearApiKeyRequested] = useState(false);
const [baseUrlDraft, setBaseUrlDraft] = useState("");
+ const [apiType, setApiType] = useState<ProviderApiType>("chat_completions");
const [maxOutputTokensDraft, setMaxOutputTokensDraft] = useState("");
const [editingBackendProviderType, setEditingBackendProviderType] = useState<
string | null
@@ -393,6 +399,7 @@ export function ChatProvidersSettings({
if (seededProviderTypeRef.current === providerType) return;
seededProviderTypeRef.current = providerType;
setMaxOutputTokensDraft("");
+ setApiType("chat_completions");
setEditingBackendProviderType(null);
const entry = registryByType.get(providerType);
if (!entry) {
@@ -513,6 +520,7 @@ export function ChatProvidersSettings({
setShowApiKey(false);
setBaseUrlDraft("");
setMaxOutputTokensDraft("");
+ setApiType("chat_completions");
setEditingBackendProviderType(null);
setAvailableModels([]);
setSelectedModelIds([]);
@@ -677,6 +685,7 @@ export function ChatProvidersSettings({
providerId: editingProviderId,
apiKey: apiKey.trim(),
baseUrl,
+ apiType: providerType === LEGACY_CUSTOM_PROVIDER_TYPE ? apiType : undefined,
});
const registryDefaults = supportsRemoteModelCatalog(providerType)
? []
@@ -750,6 +759,7 @@ export function ChatProvidersSettings({
providerType: created.provider_type,
name: created.display_name,
baseUrl: created.base_url ?? "",
+ apiType: created.api_type ?? "chat_completions",
models,
availableModels: available,
hasApiKey: created.has_api_key,
@@ -851,6 +861,7 @@ export function ChatProvidersSettings({
providerType: backendProviderType,
displayName,
baseUrl,
+ apiType: providerType === LEGACY_CUSTOM_PROVIDER_TYPE ? apiType : undefined,
models: modelsToSave,
availableModels: manualOnly
? []
@@ -875,6 +886,7 @@ export function ChatProvidersSettings({
backendProviderType: created.provider_type,
name: created.display_name,
baseUrl: created.base_url ?? "",
+ apiType: created.api_type ?? "chat_completions",
models: modelsToSave,
availableModels: manualOnly
? []
@@ -999,6 +1011,7 @@ export function ChatProvidersSettings({
customProviderDisplayName(existing.providerType)
: existing.name,
baseUrl,
+ apiType: providerType === LEGACY_CUSTOM_PROVIDER_TYPE ? apiType : undefined,
models: modelsToSave,
availableModels: manualOnly
? []
@@ -1025,6 +1038,7 @@ export function ChatProvidersSettings({
backendProviderType: updated.provider_type,
name: updated.display_name,
baseUrl: updated.base_url ?? "",
+ apiType: updated.api_type ?? "chat_completions",
models: modelsToSave,
availableModels: manualOnly
? []
@@ -1134,6 +1148,7 @@ export function ChatProvidersSettings({
setClearApiKeyRequested(false);
setShowApiKey(false);
setBaseUrlDraft(provider.baseUrl);
+ setApiType(provider.apiType ?? "chat_completions");
// Seeded at the floor: parseMaxOutputTokens throws below it, so a row stored under one would
// fail every unrelated edit. The resolver already reads it as the floor.
setMaxOutputTokensDraft(
@@ -1292,6 +1307,7 @@ export function ChatProvidersSettings({
providerId: provider.id,
apiKey: savedKey,
baseUrl: provider.baseUrl || null,
+ apiType: provider.apiType,
modelId:
provider.providerType === LEGACY_CUSTOM_PROVIDER_TYPE
? (provider.models[0] ?? null)
@@ -1529,6 +1545,30 @@ export function ChatProvidersSettings({
</div>
) : null}
+ {shouldShowProviderApiType(providerType) ? (
+ <div className="grid grid-cols-[minmax(140px,0.8fr)_minmax(0,1.2fr)] items-center gap-4 px-4 py-3 @max-[520px]:grid-cols-1">
+ <div className="flex min-w-0 flex-col gap-0.5">
+ <Label htmlFor="provider-api-type" className="text-sm font-medium">
+ API type
+ </Label>
+ <p className="text-xs leading-snug text-muted-foreground">
+ Choose the endpoint your enabled models support.
+ </p>
+ </div>
+ <Select
+ value={apiType}
+ onValueChange={(value) => setApiType(value as ProviderApiType)}
+ >
+ <SelectTrigger id="provider-api-type" className="h-9 w-full text-sm">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="chat_completions">OpenAI Chat Completions</SelectItem>
+ <SelectItem value="responses">OpenAI Responses</SelectItem>
+ </SelectContent>
+ </Select>
+ </div>
+ ) : null}
{isCustomProvider &&
selectedProviderContract?.base_url_editable !== false ? (
<div className="grid grid-cols-[minmax(140px,0.8fr)_minmax(0,1.2fr)] items-center gap-4 px-4 py-3 @max-[520px]:grid-cols-1">
diff --git a/studio/frontend/src/features/chat/codex-reasoning.ts b/studio/frontend/src/features/chat/codex-reasoning.ts
index 46e626312dd..9f60efd4bca 100644
--- a/studio/frontend/src/features/chat/codex-reasoning.ts
+++ b/studio/frontend/src/features/chat/codex-reasoning.ts
@@ -57,13 +57,14 @@ export function addCodexReasoning(
};
}
-export function readCodexReasoning(
+function readReasoningLedger(
metadata: unknown,
+ key: string,
): CodexReasoningLedger | undefined {
if (!metadata || typeof metadata !== "object") return undefined;
const custom = (metadata as { custom?: unknown }).custom;
if (!custom || typeof custom !== "object") return undefined;
- const value = (custom as Record<string, unknown>).openaiCodexReasoning;
+ const value = (custom as Record<string, unknown>)[key];
if (Array.isArray(value)) {
return value.length > 0 ? { byToolCall: {}, final: value } : undefined;
}
@@ -84,6 +85,18 @@ export function readCodexReasoning(
: undefined;
}
+export function readCodexReasoning(
+ metadata: unknown,
+): CodexReasoningLedger | undefined {
+ return readReasoningLedger(metadata, "openaiCodexReasoning");
+}
+
+export function readOpenAIResponsesReasoning(
+ metadata: unknown,
+): CodexReasoningLedger | undefined {
+ return readReasoningLedger(metadata, "openaiResponsesReasoning");
+}
+
export function codexReasoningForToolCalls(
ledger: CodexReasoningLedger | undefined,
toolCallIds: string[],
diff --git a/studio/frontend/src/features/chat/external-providers.ts b/studio/frontend/src/features/chat/external-providers.ts
index 8ea2c745373..82abee9256c 100644
--- a/studio/frontend/src/features/chat/external-providers.ts
+++ b/studio/frontend/src/features/chat/external-providers.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import type {
+ ProviderApiType,
ProviderAuthKind,
ProviderAuthStatus,
} from "./api/providers-api";
@@ -15,6 +16,7 @@ export interface ExternalProviderConfig {
name: string;
/** Provider base URL (default from registry or backend-saved override). */
baseUrl: string;
+ apiType?: ProviderApiType;
/** Model ids user enabled from `/api/providers/models`. */
models: string[];
/** Cached available model ids from the provider's /models response. */
@@ -518,6 +520,7 @@ function normalizeProvider(raw: ExternalProviderConfig): ExternalProviderConfig
providerType,
name: raw.name.trim(),
baseUrl: raw.baseUrl.trim(),
+ apiType: raw.apiType === "responses" ? "responses" : "chat_completions",
models: raw.models
.map((model) => model.trim())
.filter((model) => model.length > 0),
diff --git a/studio/frontend/src/features/chat/provider-capabilities.ts b/studio/frontend/src/features/chat/provider-capabilities.ts
index 447fe9a9f29..a5f4f055426 100644
--- a/studio/frontend/src/features/chat/provider-capabilities.ts
+++ b/studio/frontend/src/features/chat/provider-capabilities.ts
@@ -114,6 +114,7 @@ export function externalReasoningTakesEffort(
export function resolveExternalReasoningEffort(opts: {
caps: ExternalReasoningCapabilities;
providerType: string | null | undefined;
+ apiType?: "chat_completions" | "responses";
/** The chat's level now: the clamp target, and what a model that cannot think keeps. */
current: ReasoningEffortLevel;
/** The level pinned on this model's picker row. Ignored when the model no longer offers it. */
@@ -121,7 +122,11 @@ export function resolveExternalReasoningEffort(opts: {
/** Restore an existing preference without choosing a new model default. */
restore?: boolean;
}): ReasoningEffortLevel {
- const { caps, providerType, current, pinned } = opts;
+ const { caps, current, pinned } = opts;
+ const providerType = effectiveExternalReasoningProviderType(
+ opts.providerType,
+ opts.apiType,
+ );
const levels = caps.reasoningEffortLevels;
// A style that sends no level has nothing to resolve, and moving the chat's level for it would
// change what every other model runs at on the strength of a setting this one never sends.
@@ -462,20 +467,53 @@ const OPENAI_CODE_EXECUTION_MODEL_PREFIXES = [
/** Strict check for OpenAI managed cloud or Azure Foundry, not a custom OpenAI-compat
* backend: shell and image-generation tools 400 elsewhere. Mirrors _is_openai_family_cloud. */
+function isAzureOpenAICloudHost(host: string): boolean {
+ return (
+ host.endsWith(".openai.azure.com") ||
+ host.endsWith(".services.ai.azure.com")
+ );
+}
+
function isOpenAICloudBaseUrl(baseUrl: string | null | undefined): boolean {
if (!baseUrl) return true; // No override → uses the default openai.com base.
try {
const host = new URL(baseUrl).hostname.toLowerCase();
- return host === "api.openai.com" || host.endsWith(".openai.azure.com");
+ return host === "api.openai.com" || isAzureOpenAICloudHost(host);
} catch {
return false;
}
}
+function isAzureOpenAICloudBaseUrl(baseUrl: string | null | undefined): boolean {
+ if (!baseUrl) return false;
+ try {
+ return isAzureOpenAICloudHost(new URL(baseUrl).hostname.toLowerCase());
+ } catch {
+ return false;
+ }
+}
+
+function usesOpenAIHostedResponses(
+ providerType: string | null | undefined,
+ baseUrl: string | null | undefined,
+ apiType: "chat_completions" | "responses" | undefined,
+): boolean {
+ if (providerType === "openai") {
+ return isOpenAICloudBaseUrl(baseUrl);
+ }
+ return (
+ providerType === "custom" &&
+ apiType === "responses" &&
+ Boolean(baseUrl?.trim()) &&
+ isOpenAICloudBaseUrl(baseUrl)
+ );
+}
+
export function providerSupportsBuiltinCodeExecution(
providerType: string | null | undefined,
modelId: string | null | undefined,
baseUrl?: string | null,
+ apiType?: "chat_completions" | "responses",
): boolean {
const normalized = modelId?.trim().toLowerCase() ?? "";
if (!normalized) return false;
@@ -484,8 +522,8 @@ export function providerSupportsBuiltinCodeExecution(
normalized.startsWith(prefix),
);
}
- if (providerType === "openai") {
- if (!isOpenAICloudBaseUrl(baseUrl)) return false;
+ // Codex runs Studio tools locally, not provider-hosted code_execution.
+ if (usesOpenAIHostedResponses(providerType, baseUrl, apiType)) {
return OPENAI_CODE_EXECUTION_MODEL_PREFIXES.some((prefix) =>
normalized.startsWith(prefix),
);
@@ -502,15 +540,20 @@ export function providerSupportsBuiltinCodeExecution(
return false;
}
-/** Whether the provider TYPE ships its own code sandbox, model aside. Mirrors backend
- * PROVIDER_REGISTRY `hosted_tools` code_execution. Coarser than the per-model check:
- * it asks whether running the code locally would be a relocation. */
+/** Whether this connection has its own code sandbox, model aside. Coarser than the
+ * per-model check: it asks whether running the code locally would be a relocation. */
const PROVIDER_TYPES_WITH_CODE_SANDBOX = new Set(["openai", "anthropic", "gemini"]);
export function providerHostsCodeExecution(
providerType: string | null | undefined,
+ baseUrl?: string | null,
+ apiType?: "chat_completions" | "responses",
): boolean {
- return PROVIDER_TYPES_WITH_CODE_SANDBOX.has(providerType ?? "");
+ return (
+ PROVIDER_TYPES_WITH_CODE_SANDBOX.has(providerType ?? "") ||
+ (providerType === "custom" &&
+ usesOpenAIHostedResponses(providerType, baseUrl, apiType))
+ );
}
/** Whether provider/model exposes OpenAI's Responses-API image_generation tool. On for
@@ -531,11 +574,11 @@ export function providerSupportsBuiltinImageGeneration(
providerType: string | null | undefined,
modelId: string | null | undefined,
baseUrl?: string | null,
+ apiType?: "chat_completions" | "responses",
): boolean {
const normalized = modelId?.trim().toLowerCase() ?? "";
if (!normalized) return false;
- if (providerType === "openai") {
- if (!isOpenAICloudBaseUrl(baseUrl)) return false;
+ if (usesOpenAIHostedResponses(providerType, baseUrl, apiType)) {
return OPENAI_IMAGE_GENERATION_MODEL_PREFIXES.some((prefix) =>
normalized.startsWith(prefix),
);
@@ -606,6 +649,12 @@ const OPENAI_COMPAT_BASE: ProviderCapabilities = {
presencePenalty: true,
};
+const CUSTOM_RESPONSES_CAPABILITIES: ProviderCapabilities = {
+ ...OPENAI_COMPAT_BASE,
+ // The Responses translator forwards temperature/top_p, but drops presence_penalty.
+ presencePenalty: false,
+};
+
const ALL_SUPPORTED: ProviderCapabilities = {
temperature: true,
topP: true,
@@ -697,12 +746,33 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
const DEFAULT_EXTERNAL_CAPABILITIES = OPENAI_COMPAT_BASE;
+const OPENAI_RESPONSES_FIXED_SAMPLING_MODEL =
+ /^(?:gpt-5(?:[.-]|$)|gpt-4\.5(?:[.-]|$)|o\d+(?:[.-]|$)|codex-mini(?:[.-]|$)|gpt-6-astra(?:[.-]|$))/;
+
/** Resolve the capability set for an external provider. Null for a local model, which
* callers treat as "every knob applies". */
export function getProviderCapabilities(
providerType: string | null | undefined,
+ apiType?: "chat_completions" | "responses",
+ modelId?: string | null,
+ baseUrl?: string | null,
): ProviderCapabilities | null {
if (!providerType) return null;
+ if (providerType === "custom" && apiType === "responses") {
+ // Azure deployment names may not reveal the underlying model. Suppress sampling
+ // there; on api.openai.com only known fixed-sampling models need that restriction.
+ // Custom gateways may accept sampling even for those same model IDs.
+ const model = modelId?.trim().toLowerCase() ?? "";
+ if (
+ usesOpenAIHostedResponses(providerType, baseUrl, apiType) &&
+ (isAzureOpenAICloudBaseUrl(baseUrl) ||
+ (!OPENAI_NON_REASONING_CHAT_ALIAS.test(model) &&
+ OPENAI_RESPONSES_FIXED_SAMPLING_MODEL.test(model)))
+ ) {
+ return PROVIDER_CAPABILITIES.openai;
+ }
+ return CUSTOM_RESPONSES_CAPABILITIES;
+ }
return PROVIDER_CAPABILITIES[providerType] ?? DEFAULT_EXTERNAL_CAPABILITIES;
}
@@ -1064,6 +1134,18 @@ export interface ExternalReasoningResolveOptions {
isReasoningProvider?: boolean;
/** Provider base URL; used to detect custom Gemini OAI-compat gateways. */
baseUrl?: string | null;
+ /** Custom providers can opt into OpenAI's Responses API and its reasoning controls. */
+ apiType?: "chat_completions" | "responses";
+}
+
+export function effectiveExternalReasoningProviderType(
+ providerType: string | null | undefined,
+ apiType?: "chat_completions" | "responses",
+): string {
+ const normalizedProvider = providerType?.trim().toLowerCase() ?? "";
+ return normalizedProvider === "custom" && apiType === "responses"
+ ? "openai"
+ : normalizedProvider;
}
// vLLM has no per-model reasoning signal on OpenAI-compat, so pin via user toggle.
@@ -1180,7 +1262,10 @@ export function getExternalReasoningCapabilities(
options?: ExternalReasoningResolveOptions,
): ExternalReasoningCapabilities {
const normalizedModel = modelId?.trim().toLowerCase() ?? "";
- const normalizedProvider = providerType?.trim().toLowerCase() ?? "";
+ const normalizedProvider = effectiveExternalReasoningProviderType(
+ providerType,
+ options?.apiType,
+ );
const connectionLevel = resolveConnectionLevelReasoning(
normalizedProvider,
options,
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index 4fed9ca375e..d914dcceaa2 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -189,10 +189,12 @@ import {
import {
clampReasoningEffortToLevels,
getExternalReasoningCapabilities,
+ providerHostsCodeExecution,
providerSupportsBuiltinCodeExecution,
providerSupportsBuiltinImageGeneration,
providerSupportsBuiltinWebFetch,
} from "./provider-capabilities";
+import { codeToolCanRun } from "./api/code-tool-placement";
import { modelCatalogVersion, subscribeModelCatalog } from "./model-catalog";
import {
type CompositionEvent,
@@ -762,6 +764,7 @@ export function SharedComposer({
isReasoningProvider:
selectedExternalProvider?.isReasoningModel === true,
baseUrl: selectedExternalProvider?.baseUrl ?? null,
+ apiType: selectedExternalProvider?.apiType,
},
)
: null;
@@ -810,18 +813,20 @@ export function SharedComposer({
const thinkingActiveLook = isEffort
? reasoningLockedOn || (effectiveReasoningVisualEnabled && !reasoningDisabled)
: reasoningLockedOn || (effectiveReasoningEnabled && !reasoningDisabled);
- // Two-pill gating. Search: supportsTools (Code/python plus local web_search) OR
- // supportsBuiltinWebSearch (OpenAI/Anthropic/OpenRouter/Kimi). Code: supportsTools OR the
- // provider's own sandbox, per providerSupportsBuiltinCodeExecution.
+ // Search can use Unsloth tools or provider web search independently of Code.
+ // Code follows the provider's sandbox placement and never falls back to local
+ // execution when a hosted model lacks code support.
const supportsBuiltinCodeExecution = providerSupportsBuiltinCodeExecution(
selectedExternalProvider?.providerType,
effectiveExternalModelId,
selectedExternalProvider?.baseUrl,
+ selectedExternalProvider?.apiType,
);
const supportsBuiltinImageGeneration = providerSupportsBuiltinImageGeneration(
selectedExternalProvider?.providerType,
effectiveExternalModelId,
selectedExternalProvider?.baseUrl,
+ selectedExternalProvider?.apiType,
);
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
selectedExternalProvider?.providerType,
@@ -844,11 +849,24 @@ export function SharedComposer({
(isGeminiImageTier
? !supportsBuiltinWebSearch
: !(supportsTools || supportsBuiltinWebSearch));
+ const externalUsesStudioTools =
+ providerModelSupportsStudioTools(
+ selectedExternalProvider?.providerType,
+ externalSelection?.modelId,
+ ) === true;
+ const canRunCode = isExternalModel
+ ? codeToolCanRun({
+ hostedCodeExecutionForThisTurn: supportsBuiltinCodeExecution,
+ providerHostsCodeExecution: providerHostsCodeExecution(
+ selectedExternalProvider?.providerType,
+ selectedExternalProvider?.baseUrl,
+ selectedExternalProvider?.apiType,
+ ),
+ supportsStudioTools: externalUsesStudioTools,
+ })
+ : supportsTools;
const codeDisabled =
- (modelLoaded &&
- (isGeminiImageTier
- ? true
- : !(supportsTools || supportsBuiltinCodeExecution))) ||
+ (modelLoaded && (isGeminiImageTier || !canRunCode)) ||
imageModeDisablesCode;
// Images pill lights only on OpenAI cloud Responses-API models and the Gemini Nano Banana
// family. No local tool runtime fallback.
@@ -856,11 +874,6 @@ export function SharedComposer({
// Fetch pill: Anthropic-only (web_fetch_20250910 / web_fetch_20260209).
const webFetchDisabled = !modelLoaded || !supportsBuiltinWebFetch;
const showWebFetchPill = supportsBuiltinWebFetch;
- const externalUsesStudioTools =
- providerModelSupportsStudioTools(
- selectedExternalProvider?.providerType,
- externalSelection?.modelId,
- ) === true;
const ragDisabled =
modelLoaded && ((!externalUsesStudioTools && isExternalModel) || !supportsTools);
const showRagPill = !isExternalModel || externalUsesStudioTools;
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index 84f31e94360..acf69bd5cc7 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -3551,6 +3551,7 @@ export function reconcilePinnedReasoningEffort(opts: {
checkpoint: string;
caps: ExternalReasoningCapabilities;
providerType: string | null | undefined;
+ apiType?: "chat_completions" | "responses";
}): void {
const state = useChatRuntimeStore.getState();
if (state.params.checkpoint !== opts.checkpoint) return;
@@ -3561,6 +3562,7 @@ export function reconcilePinnedReasoningEffort(opts: {
const next = resolveExternalReasoningEffort({
caps: opts.caps,
providerType: opts.providerType,
+ apiType: opts.apiType,
current: pinned
? state.reasoningEffort
: (takeEffortDisplacedByPin() ?? state.reasoningEffort),
diff --git a/studio/frontend/src/features/chat/sync-external-providers.ts b/studio/frontend/src/features/chat/sync-external-providers.ts
index 6e9aadbda2f..343bb7a5449 100644
--- a/studio/frontend/src/features/chat/sync-external-providers.ts
+++ b/studio/frontend/src/features/chat/sync-external-providers.ts
@@ -23,10 +23,10 @@ import {
} from "./model-catalog";
import {
CUSTOM_BACKEND_PROVIDER_TYPE,
+ CUSTOM_PROVIDER_DISPLAY_NAME,
CUSTOM_PROVIDER_PRESETS,
type ExternalProviderConfig,
getExternalProviderApiKey,
- isCustomProviderType,
isPromptCacheTtl,
LEGACY_CUSTOM_PROVIDER_TYPE,
pruneExternalProviderApiKeys,
@@ -52,45 +52,60 @@ const OPENROUTER_EXCLUDED_MODELS = new Set([
"recraft/recraft-v4-pro",
]);
-function normalizeUrl(input: string): string {
- return input.trim().replace(/\/+$/, "");
+function parseHttpEndpointHost(
+ input: string | null | undefined,
+): string | null {
+ const value = (input ?? "").trim();
+ if (!value) {
+ return null;
+ }
+ try {
+ const parsed = new URL(value);
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
+ return null;
+ }
+ return parsed.hostname.toLowerCase() || null;
+ } catch {
+ return null;
+ }
+}
+
+function isOpenAIManagedEndpoint(input: string | null | undefined): boolean {
+ const host = parseHttpEndpointHost(input);
+ return (
+ host === "api.openai.com" ||
+ host?.endsWith(".openai.azure.com") === true ||
+ host?.endsWith(".services.ai.azure.com") === true
+ );
}
export function resolveUiProviderTypeFromConfig(
configProviderType: string,
configDisplayName: string | null | undefined,
configBaseUrl: string | null | undefined,
- registryRows: ProviderRegistryEntry[],
- existingProviderType: string | undefined,
+ _registryRows: ProviderRegistryEntry[],
+ _existingProviderType: string | undefined,
): string {
- if (existingProviderType && isCustomProviderType(existingProviderType)) {
- return existingProviderType;
- }
if (configProviderType !== CUSTOM_BACKEND_PROVIDER_TYPE) {
return configProviderType;
}
+ // Names are editable labels, not provider identity. Repair stale local custom state before
+ // applying any legacy heuristic when the saved endpoint is OpenAI-managed.
+ if (isOpenAIManagedEndpoint(configBaseUrl)) {
+ return CUSTOM_BACKEND_PROVIDER_TYPE;
+ }
const displayName = (configDisplayName ?? "").trim().toLowerCase();
+ if (displayName === CUSTOM_PROVIDER_DISPLAY_NAME.toLowerCase()) {
+ return LEGACY_CUSTOM_PROVIDER_TYPE;
+ }
const matchingCustomPreset = CUSTOM_PROVIDER_PRESETS.find(
(preset) => preset.displayName.toLowerCase() === displayName,
);
if (matchingCustomPreset) {
return matchingCustomPreset.providerType;
}
- const openAiRegistry = registryRows.find(
- (entry) => entry.provider_type === CUSTOM_BACKEND_PROVIDER_TYPE,
- );
- if (!openAiRegistry) {
- return configProviderType;
- }
- const openAiDisplayName = openAiRegistry.display_name.trim().toLowerCase();
- if (displayName.length > 0 && displayName !== openAiDisplayName) {
- return LEGACY_CUSTOM_PROVIDER_TYPE;
- }
- const configUrl = normalizeUrl(configBaseUrl ?? "");
- const defaultUrl = normalizeUrl(openAiRegistry.base_url ?? "");
- if (configUrl.length > 0 && configUrl !== defaultUrl) {
- return LEGACY_CUSTOM_PROVIDER_TYPE;
- }
+ // A non-OpenAI hostname can be a corporate proxy for the real OpenAI API. Without a
+ // built-in legacy label there is no safe way to infer that the saved row was custom.
return configProviderType;
}
@@ -296,6 +311,7 @@ export async function syncExternalProvidersFromBackend(
backendProviderType: config.provider_type,
name: config.display_name,
baseUrl: config.base_url ?? "",
+ apiType: config.api_type ?? "chat_completions",
models: resolvedModels,
availableModels: resolvedAvailableModels,
maxOutputTokens: config.max_output_tokens ?? undefined,
diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.ts
index 25b26c98186..eb0a1787967 100644
--- a/studio/frontend/src/features/chat/types/api.ts
+++ b/studio/frontend/src/features/chat/types/api.ts
@@ -666,6 +666,7 @@ export interface OpenAIChatCompletionsRequest {
external_model?: string;
encrypted_api_key?: string;
provider_base_url?: string | null;
+ provider_api_type?: "chat_completions" | "responses";
/** Boolean toggle for OpenAI/Anthropic ephemeral cache_control. For Gemini the backend also accepts
* a cached-content resource name, forwarded as `generationConfig.cachedContent`. */
enable_prompt_caching?: boolean | string | null;
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-info-dialog.tsx b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-info-dialog.tsx
index 24306265d7c..e4b1e53f54d 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-info-dialog.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-info-dialog.tsx
@@ -18,6 +18,7 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { modelCatalogVersion, subscribeModelCatalog } from "@/features/chat";
+import type { ProviderApiType } from "@/features/chat/api/providers-api";
// eslint-disable-next-line no-restricted-imports -- Avoid the chat barrel's React exports.
import { resolveModelCatalogEntry } from "@/features/chat/model-catalog";
// eslint-disable-next-line no-restricted-imports -- Avoid the chat barrel's React exports.
@@ -63,6 +64,7 @@ export function ConnectedModelInfoDialog({
displayName,
providerName,
providerType,
+ apiType,
baseUrl,
isReasoningProvider,
}: {
@@ -73,6 +75,7 @@ export function ConnectedModelInfoDialog({
displayName: string;
providerName: string;
providerType: string;
+ apiType?: ProviderApiType;
baseUrl?: string | null;
/** A vLLM connection flagged as serving a reasoning model: the only signal a self-host gives. */
isReasoningProvider?: boolean;
@@ -80,11 +83,17 @@ export function ConnectedModelInfoDialog({
// Every figure below is read from the catalogue, which can land after this renders.
useSyncExternalStore(subscribeModelCatalog, modelCatalogVersion);
const entry = resolveModelCatalogEntry(providerType, modelId);
- const marks = connectedModelMarks({ providerType, modelId, baseUrl });
+ const marks = connectedModelMarks({
+ providerType,
+ modelId,
+ baseUrl,
+ apiType,
+ });
// The resolver behind the composer's Thinking chip, not the catalogue alone.
const reasoning = getExternalReasoningCapabilities(providerType, modelId, {
isReasoningProvider,
baseUrl,
+ apiType,
});
// "none" is the off switch, not a rung. Only where a level is sent at all: the default ladder
// is there even for a style that carries a bare thinking on/off, and listing it read as a
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-meta.ts b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-meta.ts
index 97bf4e9b602..e47882262ee 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-meta.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-meta.ts
@@ -5,6 +5,7 @@
// gates), never from the model's name: a mark the request cannot honour is a promise the picker
// breaks. An On Device badge may guess, since it describes a repo you have not fetched yet.
+import type { ProviderApiType } from "@/features/chat/api/providers-api";
// eslint-disable-next-line no-restricted-imports -- Avoid the chat barrel's React exports.
import { providerModelSupportsVision } from "@/features/chat/external-providers";
// eslint-disable-next-line no-restricted-imports -- Avoid the chat barrel's React exports.
@@ -21,13 +22,15 @@ export interface ConnectedModelMarks {
/** The badges one Connected row should draw.
*
* `modelId` is the provider's own id (`gemini-2.5-flash-image`), not the `external::` id the
- * picker selects by. `baseUrl` is the connection's and only image generation reads it. */
+ * picker selects by. `baseUrl` and `apiType` are the connection's and only image generation
+ * reads them. */
export function connectedModelMarks(opts: {
providerType: string | null | undefined;
modelId: string | null | undefined;
baseUrl?: string | null;
+ apiType?: ProviderApiType;
}): ConnectedModelMarks {
- const { providerType, modelId, baseUrl } = opts;
+ const { providerType, modelId, baseUrl, apiType } = opts;
// null is unknown, drawn as no badge rather than as a promise.
const vision = providerModelSupportsVision(providerType, modelId) === true;
return {
@@ -43,6 +46,7 @@ export function connectedModelMarks(opts: {
providerType,
modelId,
baseUrl,
+ apiType,
),
// Nothing we connect to serves video generation through the chat route, so the name would
// be the only evidence and a glyph resting on it promises what selecting the row cannot do.
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-settings-dialog.tsx b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-settings-dialog.tsx
index 4207d0dffc3..c0f44dac353 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/connected-model-settings-dialog.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/connected-model-settings-dialog.tsx
@@ -26,6 +26,7 @@ import {
SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
+import type { ProviderApiType } from "@/features/chat/api/providers-api";
import {
modelCatalogVersion,
reconcilePinnedReasoningEffort,
@@ -52,6 +53,7 @@ export function ConnectedModelSettingsDialog({
displayName,
modelId,
providerType,
+ apiType,
baseUrl,
isReasoningProvider,
connectionMaxOutputTokens,
@@ -64,6 +66,7 @@ export function ConnectedModelSettingsDialog({
/** The provider's own id, for the catalogue lookup. */
modelId: string;
providerType: string;
+ apiType?: ProviderApiType;
baseUrl?: string | null;
/** A vLLM connection flagged as serving a reasoning model. */
isReasoningProvider?: boolean;
@@ -98,6 +101,7 @@ export function ConnectedModelSettingsDialog({
const reasoning = getExternalReasoningCapabilities(providerType, modelId, {
isReasoningProvider,
baseUrl,
+ apiType,
});
// Offered only where a level is actually sent: the default low/medium/high ladder is present
// even for a model whose style carries a bare thinking on/off, so gating on supportsReasoning
@@ -172,6 +176,7 @@ export function ConnectedModelSettingsDialog({
checkpoint: checkpointId,
caps: reasoning,
providerType,
+ apiType,
});
}
}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
index 134d887c9e1..7af6bd125a7 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
@@ -38,6 +38,7 @@ import type {
GgufVariantDetail,
LocalModelInfo,
} from "@/features/chat";
+import type { ProviderApiType } from "@/features/chat/api/providers-api";
import {
DotTag,
type HubOption,
@@ -4431,6 +4432,13 @@ export function HubModelPicker({
),
[externalProviders],
);
+ const externalApiTypeById = useMemo(
+ () =>
+ new Map(
+ externalProviders.map((provider) => [provider.id, provider.apiType]),
+ ),
+ [externalProviders],
+ );
// A connection's own output cap, which lowers the model's documented one. The bounds the
// per-model editor offers have to be the ones every request is clamped to.
const externalMaxOutputById = useMemo(
@@ -4481,12 +4489,14 @@ export function HubModelPicker({
const [infoModel, setInfoModel] = useState<{
model: ExternalModelOption;
providerModelId: string;
+ apiType?: ProviderApiType;
baseUrl: string | null;
isReasoningProvider: boolean;
} | null>(null);
const [settingsModel, setSettingsModel] = useState<{
model: ExternalModelOption;
providerModelId: string;
+ apiType?: ProviderApiType;
baseUrl: string | null;
isReasoningProvider: boolean;
connectionMaxOutputTokens: number | null;
@@ -4844,6 +4854,7 @@ export function HubModelPicker({
providerType: model.providerType,
modelId: parseExternalModelId(model.id)?.modelId ?? model.name,
baseUrl: externalBaseUrlById.get(model.providerId) ?? null,
+ apiType: externalApiTypeById.get(model.providerId),
});
return connectedModality === "vision"
? marks.vision
@@ -4858,6 +4869,7 @@ export function HubModelPicker({
debouncedQuery,
connectedModality,
externalBaseUrlById,
+ externalApiTypeById,
catalogVersion,
]);
@@ -5454,6 +5466,7 @@ export function HubModelPicker({
providerType: model.providerType,
modelId: providerModelId,
baseUrl,
+ apiType: externalApiTypeById.get(model.providerId),
});
return (
<div
@@ -5558,6 +5571,7 @@ export function HubModelPicker({
setSettingsModel({
model,
providerModelId,
+ apiType: externalApiTypeById.get(model.providerId),
baseUrl,
isReasoningProvider:
externalReasoningFlagById.get(model.providerId) === true,
@@ -5589,6 +5603,7 @@ export function HubModelPicker({
setInfoModel({
model,
providerModelId,
+ apiType: externalApiTypeById.get(model.providerId),
baseUrl,
isReasoningProvider:
externalReasoningFlagById.get(model.providerId) === true,
@@ -7595,6 +7610,7 @@ export function HubModelPicker({
displayName={settingsModel.model.name}
modelId={settingsModel.providerModelId}
providerType={settingsModel.model.providerType}
+ apiType={settingsModel.apiType}
baseUrl={settingsModel.baseUrl}
isReasoningProvider={settingsModel.isReasoningProvider}
connectionMaxOutputTokens={settingsModel.connectionMaxOutputTokens}
@@ -7610,6 +7626,7 @@ export function HubModelPicker({
displayName={infoModel.model.name}
providerName={infoModel.model.providerName}
providerType={infoModel.model.providerType}
+ apiType={infoModel.apiType}
baseUrl={infoModel.baseUrl}
isReasoningProvider={infoModel.isReasoningProvider}
/>
diff --git a/studio/frontend/tests/code-tool-placement.test.ts b/studio/frontend/tests/code-tool-placement.test.ts
index 805062ffd9a..e3a92eeb486 100644
--- a/studio/frontend/tests/code-tool-placement.test.ts
+++ b/studio/frontend/tests/code-tool-placement.test.ts
@@ -23,10 +23,17 @@ import {
codeToolCanRun,
selectCodeToolNames,
} from "../src/features/chat/api/code-tool-placement.ts";
+import { readSrc, registerBundlerResolver } from "./helpers/kit.ts";
-import { readSrc } from "./helpers/kit.ts";
+registerBundlerResolver();
+const {
+ providerHostsCodeExecution,
+ providerSupportsBuiltinCodeExecution,
+} = await import("../src/features/chat/provider-capabilities.ts");
const SOURCE = readSrc("features/chat/api/chat-adapter.ts");
+const COMPOSER_SOURCE = readSrc("features/chat/shared-composer.tsx");
+const CHAT_PAGE_SOURCE = readSrc("features/chat/chat-page.tsx");
// ── the rule itself ────────────────────────────────────────────────
@@ -55,6 +62,19 @@ test("a provider with a sandbox its MODEL cannot use runs nothing, not local cod
);
});
+test("unsupported models on managed custom Responses never fall back to local code", () => {
+ const baseUrl = "https://api.openai.com/v1";
+ const hosted = providerSupportsBuiltinCodeExecution("custom", "gpt-4.1", baseUrl, "responses");
+ const providerHosted = providerHostsCodeExecution("custom", baseUrl, "responses");
+ assert.equal(codeToolCanRun({ hostedCodeExecutionForThisTurn: hosted,
+ providerHostsCodeExecution: providerHosted, supportsStudioTools: true }), false);
+ assert.deepEqual(selectCodeToolNames({ codeToolsEnabled: true,
+ hostedCodeExecutionForThisTurn: hosted, providerHostsCodeExecution: providerHosted }),
+ { local: [], hosted: [] });
+ assert.match(COMPOSER_SOURCE, /providerHostsCodeExecution\(\s*selectedExternalProvider\?\.providerType,\s*selectedExternalProvider\?\.baseUrl,\s*selectedExternalProvider\?\.apiType,/);
+ assert.match(CHAT_PAGE_SOURCE, /providerHostsCodeExecution\(\s*provider\?\.providerType,\s*provider\?\.baseUrl,\s*provider\?\.apiType,/);
+});
+
test("a provider with no sandbox uses Unsloth's own tools", () => {
// llama.cpp / vLLM / Ollama / custom, and the cloud providers that ship no
// code sandbox. Local execution is the only meaning the pill can have there,
diff --git a/studio/frontend/tests/connected-model-row-actions.test.ts b/studio/frontend/tests/connected-model-row-actions.test.ts
index 1758a974619..ea5bcc4c385 100644
--- a/studio/frontend/tests/connected-model-row-actions.test.ts
+++ b/studio/frontend/tests/connected-model-row-actions.test.ts
@@ -7,7 +7,6 @@
import assert from "node:assert/strict";
import test from "node:test";
-
import { readSrc } from "./helpers/kit.ts";
const pickers = readSrc(
@@ -65,7 +64,7 @@ test("capabilities are keyed by the provider's own model id", () => {
);
assert.match(
pickers,
- /connectedModelMarks\(\{\s*providerType: model\.providerType,\s*modelId: providerModelId,\s*baseUrl,/,
+ /connectedModelMarks\(\{\s*providerType: model\.providerType,\s*modelId: providerModelId,\s*baseUrl,\s*apiType: externalApiTypeById\.get\(model\.providerId\),/,
);
// A catalogue lands after first paint, so the marks have to be re-read when it does.
assert.match(
@@ -92,7 +91,7 @@ test("modality comes from the resolvers the app already has", () => {
assert.doesNotMatch(marks, /detectCapabilities/);
assert.match(
marks,
- /imageGen: providerSupportsBuiltinImageGeneration\(\s*providerType,\s*modelId,\s*baseUrl,\s*\),/,
+ /imageGen: providerSupportsBuiltinImageGeneration\(\s*providerType,\s*modelId,\s*baseUrl,\s*apiType,\s*\),/,
);
// Audio is withheld even where a catalogue publishes it as an input modality: the attachment
// adapter resolves the active model out of `models`, which carries loaded local models only, so
@@ -380,7 +379,7 @@ test("per-model prompt and cap reuse the memory Chat already keeps", () => {
// one, and the pin effect's own guard sees no change in the stored string.
assert.match(
chatPage,
- /reconcilePinnedReasoningEffort\(\{\s*checkpoint: inferenceParams\.checkpoint,\s*caps,\s*providerType: provider\?\.providerType,\s*\}\);\s*\}, \[activePinnedEffort,/,
+ /reconcilePinnedReasoningEffort\(\{\s*checkpoint: inferenceParams\.checkpoint,\s*caps,\s*providerType: provider\?\.providerType,\s*apiType: provider\?\.apiType,\s*\}\);\s*\}, \[activePinnedEffort,/,
);
assert.match(
chatPage,
@@ -726,7 +725,7 @@ test("reasoning is read through the resolver the composer uses", () => {
for (const source of [infoDialog, settingsDialog]) {
assert.match(
source,
- /getExternalReasoningCapabilities\(providerType, modelId, \{\s*isReasoningProvider,\s*baseUrl,\s*\}\)/,
+ /getExternalReasoningCapabilities\(providerType, modelId, \{\s*isReasoningProvider,\s*baseUrl,\s*apiType,\s*\}\)/,
);
// "none" is the off switch, not a level on offer.
assert.match(source, /\(level\) => level !== "none"/);
@@ -734,6 +733,10 @@ test("reasoning is read through the resolver the composer uses", () => {
assert.doesNotMatch(settingsDialog, /entry\?\.reasoning \? entry\.efforts/);
// And a self-hosted endpoint's only reasoning signal is the flag on its connection.
assert.match(pickers, /provider\.isReasoningModel === true,/);
+ // Custom Responses connections must carry their transport into both row dialogs.
+ assert.match(pickers, /apiType: externalApiTypeById\.get\(model\.providerId\)/);
+ assert.match(pickers, /apiType=\{settingsModel\.apiType\}/);
+ assert.match(pickers, /apiType=\{infoModel\.apiType\}/);
});
test("a Codex connection resolves against OpenAI's catalogue", async () => {
diff --git a/studio/frontend/tests/custom-provider-api-type.test.ts b/studio/frontend/tests/custom-provider-api-type.test.ts
new file mode 100644
index 00000000000..cf0cec3d01f
--- /dev/null
+++ b/studio/frontend/tests/custom-provider-api-type.test.ts
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test, { after } from "node:test";
+import { createServer } from "vite";
+import { installLocalStorageFake } from "./helpers/kit.ts";
+
+installLocalStorageFake();
+Object.assign(window.location, { href: "http://localhost/" });
+const vite = await createServer({ server: { middlewareMode: true } });
+after(() => vite.close());
+test("Responses survives provider requests and cache while legacy cache defaults", async () => {
+ const api = await vite.ssrLoadModule("/src/features/chat/api/providers-api.ts");
+ const providers = await vite.ssrLoadModule("/src/features/chat/external-providers.ts");
+ const bodies: Record<string, unknown>[] = [];
+ const previous = globalThis.fetch;
+ globalThis.fetch = (async (_url, init) => {
+ bodies.push(JSON.parse(String(init?.body))); return Response.json({});
+ }) as typeof fetch;
+ try {
+ await api.createProviderConfig({ providerType: "custom", displayName: "Gateway", apiType: "responses" });
+ await api.updateProviderConfig("gateway", { apiType: "responses" });
+ await api.testProviderConnection({ providerType: "custom", apiKey: "", apiType: "responses" });
+ } finally {
+ globalThis.fetch = previous;
+ }
+ assert.equal(bodies.length, 3);
+ assert.ok(bodies.every((body) => body.api_type === "responses"));
+ const config = { id: "gateway", providerType: "custom", name: "Gateway",
+ baseUrl: "https://gateway.example/v1", models: ["responses-only"], createdAt: 1, updatedAt: 1 };
+ providers.saveExternalProviders([{ ...config, apiType: "responses" }]);
+ assert.equal(providers.loadExternalProviders()[0].apiType, "responses");
+ providers.saveExternalProviders([config]);
+ assert.equal(providers.loadExternalProviders()[0].apiType, "chat_completions");
+});
diff --git a/studio/frontend/tests/external-sampling-payload.test.ts b/studio/frontend/tests/external-sampling-payload.test.ts
index 67a5fe0177c..0fca938fd85 100644
--- a/studio/frontend/tests/external-sampling-payload.test.ts
+++ b/studio/frontend/tests/external-sampling-payload.test.ts
@@ -90,9 +90,12 @@ const buildSamplingFields = new Function(
function bodyFor(
providerType: string,
params = PARAMS,
+ apiType?: "chat_completions" | "responses",
+ modelId?: string,
+ baseUrl?: string,
): Record<string, number> {
return buildSamplingFields(
- getProviderCapabilities(providerType),
+ getProviderCapabilities(providerType, apiType, modelId, baseUrl),
params,
{ providerType },
minPSamplingPayload,
@@ -144,6 +147,16 @@ test("custom stays on the OpenAI-compatible baseline", () => {
assert.equal(body.temperature, PARAMS.temperature);
});
+test("custom Responses sends gateway sampling but omits it for managed fixed models", () => {
+ const gateway = bodyFor("custom", PARAMS, "responses", "gpt-5.5", "https://gateway.example/v1");
+ assert.equal(gateway.temperature, PARAMS.temperature);
+ assert.equal(gateway.top_p, PARAMS.topP);
+ const managed = bodyFor("custom", PARAMS, "responses", "gpt-5.5", "https://api.openai.com/v1");
+ assert.ok(!("temperature" in managed));
+ assert.ok(!("top_p" in managed));
+ assert.match(source, /getProviderCapabilities\(\s*externalProvider\?\.providerType,\s*externalProvider\?\.apiType,\s*externalSelection\?\.modelId,\s*externalProvider\?\.baseUrl/);
+});
+
test("ollama is sent none of the three its /v1 layer drops", () => {
const body = bodyFor("ollama");
assert.ok(!("min_p" in body));
diff --git a/studio/frontend/tests/legacy-custom-provider-api-type.test.ts b/studio/frontend/tests/legacy-custom-provider-api-type.test.ts
new file mode 100644
index 00000000000..5206720cd21
--- /dev/null
+++ b/studio/frontend/tests/legacy-custom-provider-api-type.test.ts
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import { readSrc } from "./helpers/kit.ts";
+
+test("only Custom connections expose API type selection", () => {
+ const dialog = readSrc("features/chat/chat-providers-dialog.tsx");
+ assert.match(dialog, /function shouldShowProviderApiType\(providerType: string\): boolean \{\s*return providerType === LEGACY_CUSTOM_PROVIDER_TYPE;\s*\}/);
+ assert.match(dialog, /\{shouldShowProviderApiType\(providerType\) \? \(/);
+});
diff --git a/studio/frontend/tests/openai-responses-reasoning-replay.test.ts b/studio/frontend/tests/openai-responses-reasoning-replay.test.ts
new file mode 100644
index 00000000000..9fe306d9bea
--- /dev/null
+++ b/studio/frontend/tests/openai-responses-reasoning-replay.test.ts
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { addCodexReasoning, codexReasoningForToolCalls, readOpenAIResponsesReasoning,
+ type CodexReasoningLedger } from "../src/features/chat/codex-reasoning.ts";
+import { readSrc } from "./helpers/kit.ts";
+
+test("Responses reasoning remains bound to its tool round and replay metadata", () => {
+ const item = { type: "reasoning", id: "rs_1", encrypted_content: "opaque" };
+ let ledger: CodexReasoningLedger = { byToolCall: {} };
+ ledger = addCodexReasoning(ledger, [item], ["call_1"]);
+
+ const stored = readOpenAIResponsesReasoning({
+ custom: { openaiResponsesReasoning: ledger },
+ });
+ assert.deepEqual(codexReasoningForToolCalls(stored, ["call_1"]), [item]);
+ assert.equal(codexReasoningForToolCalls(stored, ["call_2"]), undefined);
+ const adapter = readSrc("features/chat/api/chat-adapter.ts");
+ assert.match(adapter, /extraRecord\.openai_responses_reasoning/);
+ assert.match(adapter, /openai_responses_reasoning:\s*reasoning/);
+});
diff --git a/studio/frontend/tests/provider-capabilities-current-models.test.ts b/studio/frontend/tests/provider-capabilities-current-models.test.ts
index 5f9f47d54d7..5e18bed1bcc 100644
--- a/studio/frontend/tests/provider-capabilities-current-models.test.ts
+++ b/studio/frontend/tests/provider-capabilities-current-models.test.ts
@@ -17,6 +17,7 @@ const {
getExternalReasoningCapabilities,
providerHostsCodeExecution,
providerSupportsBuiltinCodeExecution,
+ providerSupportsBuiltinImageGeneration,
providerSupportsBuiltinWebSearch,
providerSupportsFastMode,
@@ -385,6 +386,19 @@ test("new Anthropic and OpenAI ids keep their max-output cap and code pill", ()
);
});
+test("custom Responses exposes OpenAI hosted tools only on managed cloud hosts", () => {
+ const model = "gpt-5.5";
+ for (const [baseUrl, apiType, expected] of [
+ ["https://api.openai.com/v1", "responses", true],
+ ["https://team.openai.azure.com/openai/v1", "responses", true],
+ ["https://api.openai.com.attacker.example/v1", "responses", false],
+ ["https://api.openai.com/v1", "chat_completions", false],
+ ] as const) {
+ assert.equal(providerSupportsBuiltinCodeExecution("custom", model, baseUrl, apiType), expected);
+ assert.equal(providerSupportsBuiltinImageGeneration("custom", model, baseUrl, apiType), expected);
+ }
+});
+
test("generic Custom connections use only their explicit max-output override", () => {
// no capability row targets `custom`, so a model id resembling a hosted family
// never enters the decision