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: follow the JSON format a client asks for on /v1/messages (#11854)

Nilay34992c351f50
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 60e424c15de..9e00a11f635 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -33286,6 +33286,13 @@ def _guard_anthropic_client_tool_catalog(
)
+def _anthropic_client_tools_for_turn(openai_client_tools, openai_tool_choice, openai_messages):
+ """Withdraw a disabled catalog unless replayed history still needs its schemas."""
+ if openai_tool_choice == "none" and not _has_openai_tool_history(openai_messages):
+ return []
+ return openai_client_tools
+
+
def _anthropic_requested_studio_tools(tools: Optional[list]) -> set[str]:
requested: set[str] = set()
for tool in tools or []:
@@ -33654,6 +33661,36 @@ def _validate_anthropic_client_tools(tools) -> None:
)
+def _anthropic_response_format(
+ payload, *, require_llama_compatibility: bool = True
+) -> Optional[dict]:
+ output_config = payload.output_config if isinstance(payload.output_config, dict) else {}
+ fmt = output_config.get("format")
+ if fmt is None:
+ fmt = (payload.model_extra or {}).get("output_format")
+ if fmt is None:
+ return None
+ if not (
+ isinstance(fmt, dict)
+ and fmt.get("type") == "json_schema"
+ and isinstance(fmt.get("schema"), dict)
+ ):
+ logger.warning("Ignoring unsupported Anthropic output format: %r", fmt)
+ return None
+ schema = fmt["schema"]
+ if require_llama_compatibility and _llama_compatible_tool_schema(schema) != schema:
+ raise HTTPException(
+ status_code = 400,
+ detail = anthropic_error_body(
+ "The output format schema contains constraints that llama.cpp cannot enforce "
+ "without weakening them; use anchored patterns and supported repetition bounds.",
+ status = 400,
+ err_type = "invalid_request_error",
+ ),
+ )
+ return {"type": "json_schema", "json_schema": {"name": "response", "schema": schema}}
+
+
def _append_to_codex_instructions(messages: list[dict], addition: str) -> list[dict]:
"""Append text to the leading system message, or prepend one.
@@ -34288,6 +34325,8 @@ async def anthropic_count_tokens(
# Reject malformed tools before the switch, like /messages, so an invalid
# count request can't evict the loaded model.
_validate_anthropic_client_tools(payload.tools)
+ # Counting compiles no output grammar, matching Anthropic's count_tokens behavior.
+ _count_response_format = _anthropic_response_format(payload, require_llama_compatibility = False)
# Count with the requested model's tokenizer, like the sibling /messages.
# Carry the vision guard too: an image count naming a text-only GGUF must not
# evict a loaded vision model for a swap that can't serve the request.
@@ -34356,7 +34395,6 @@ async def anthropic_count_tokens(
openai_messages = _sanitize_anthropic_openai_messages(openai_messages, llama_backend)
# /apply-template fetches remote media even though token counting only needs its marker.
_placeholder_remote_images_for_count(openai_messages)
- openai_tools = anthropic_tools_to_openai(payload.tools or []) or None
# Only the client-tool passthrough is forwarded verbatim, so reproduce /messages' own
# routing rather than "any tools": a Studio server-tool alias, or a template without
# passthrough support, falls through to plain generation there and does carry the date.
@@ -34366,19 +34404,39 @@ async def anthropic_count_tokens(
or anthropic_schema_client_tool_kind(t) is not None
for t in payload.tools or []
)
+ _count_openai_tool_choice = anthropic_tool_choice_to_openai(payload.tool_choice) or "auto"
+ _count_schema_disables_server_tools = (
+ _count_openai_tool_choice == "none" and _count_response_format is not None
+ )
_count_server_tools = (
_anthropic_selects_server_tools(payload, _count_studio_tools, _count_has_client_tool)
and llama_backend.supports_tools
and not _anthropic_request_has_image(payload, tool_results = llama_backend.is_vision)
+ and not _count_schema_disables_server_tools
+ )
+ _count_selected_server_tools = []
+ if _count_server_tools:
+ from core.inference.tools import ALL_TOOLS as _ANTHROPIC_COUNT_TOOLS
+ _count_selected_server_tools = _tools_for_search_images(
+ _select_anthropic_server_tools(
+ _ANTHROPIC_COUNT_TOOLS,
+ _count_studio_tools,
+ payload.enabled_tools,
+ )
+ )
+ _count_server_tools = bool(_count_selected_server_tools)
+ _count_openai_client_tools = _anthropic_client_tools_for_turn(
+ [
+ tool
+ for tool in anthropic_tools_to_openai(payload.tools or [])
+ if tool.get("function", {}).get("name") not in _count_studio_tools
+ ],
+ _count_openai_tool_choice,
+ openai_messages,
)
- _count_openai_client_tools = [
- tool
- for tool in anthropic_tools_to_openai(payload.tools or [])
- if tool.get("function", {}).get("name") not in _count_studio_tools
- ]
_guard_anthropic_client_tool_catalog(
_count_openai_client_tools,
- anthropic_tool_choice_to_openai(payload.tool_choice) or "auto",
+ _count_openai_tool_choice,
_count_server_tools,
llama_backend,
)
@@ -34387,6 +34445,7 @@ async def anthropic_count_tokens(
and bool(_count_openai_client_tools)
and getattr(llama_backend, "supports_tool_passthrough", llama_backend.supports_tools)
)
+ openai_tools = _count_openai_client_tools if _count_client_tools else None
if _count_client_tools:
from core.inference.chat_template_helpers import (
forced_tool_catalog,
@@ -34398,10 +34457,7 @@ async def anthropic_count_tokens(
openai_tools, None, getattr(llama_backend, "markup_profile", None)
)
openai_tools = (
- forced_tool_catalog(
- anthropic_tool_choice_to_openai(payload.tool_choice), _count_safe_tools
- )
- or openai_tools
+ forced_tool_catalog(_count_openai_tool_choice, _count_safe_tools) or openai_tools
)
else:
openai_messages = _prepend_current_date_to_messages(
@@ -34410,15 +34466,7 @@ async def anthropic_count_tokens(
include_api_key = _count_server_tools,
)
if _count_server_tools:
- from core.inference.tools import ALL_TOOLS as _ANTHROPIC_COUNT_TOOLS
-
- openai_tools = _tools_for_search_images(
- _select_anthropic_server_tools(
- _ANTHROPIC_COUNT_TOOLS,
- _count_studio_tools,
- payload.enabled_tools,
- )
- )
+ openai_tools = _count_selected_server_tools
_count_full_access = bool(getattr(payload, "bypass_permissions", False))
if _count_full_access:
# Same schemas /messages renders under Full access, or the count prices a different prompt.
@@ -34582,20 +34630,31 @@ async def anthropic_messages(
_selects_server_tools = _anthropic_selects_server_tools(
payload, requested_studio_tools, _has_client_tool
)
- _server_tools_requested_pre = _selects_server_tools and not _anthropic_top_level_image
- if _server_tools_requested_pre:
+ _response_format = _anthropic_response_format(payload)
+ _schema_disables_server_tools = (
+ _response_format is not None
+ and anthropic_tool_choice_to_openai(payload.tool_choice) == "none"
+ )
+ _server_tools_selected_pre = (
+ _selects_server_tools
+ and not _anthropic_top_level_image
+ and not _schema_disables_server_tools
+ )
+ selected_server_tools = []
+ if _server_tools_selected_pre:
from core.inference.tools import ALL_TOOLS as _ALL_TOOLS_PRE
-
- _selected_pre = _tools_for_search_images(
+ selected_server_tools = _tools_for_search_images(
_select_anthropic_server_tools(
_ALL_TOOLS_PRE, requested_studio_tools, payload.enabled_tools
)
)
+ _server_tools_requested_pre = bool(selected_server_tools)
+ if _server_tools_requested_pre:
_perm_mode_pre = getattr(payload, "permission_mode", None)
_confirm_opt_out_pre = getattr(payload, "confirm_tool_calls", None) is False
_gated_tool_selected_pre = any(
tool["function"]["name"] not in _ANTHROPIC_UNPROMPTED_SAFE_TOOLS
- for tool in _selected_pre
+ for tool in selected_server_tools
)
# An explicit confirm_tool_calls=False opts out of the gate entirely (it
# wins over the mode, mirroring _permission_mode_confirm and the GGUF path),
@@ -34737,11 +34796,15 @@ async def anthropic_messages(
# Match /v1/chat/completions: server tools reject caller attachments but
# accept replayed images. Check original blocks because promotion also sets
# _has_image. Tool selection and mixed-mode rejection preceded the switch.
- openai_client_tools = [
- tool
- for tool in anthropic_tools_to_openai(payload.tools or [])
- if tool.get("function", {}).get("name") not in requested_studio_tools
- ]
+ openai_client_tools = _anthropic_client_tools_for_turn(
+ [
+ tool
+ for tool in anthropic_tools_to_openai(payload.tools or [])
+ if tool.get("function", {}).get("name") not in requested_studio_tools
+ ],
+ openai_tool_choice,
+ openai_messages,
+ )
# An Anthropic server-tool declaration implies server-tool mode, but only
# when tools aren't explicitly disabled (CLI --disable-tools or per-request
@@ -34749,7 +34812,10 @@ async def anthropic_messages(
# permission gate above: deciding "did this request select server tools"
# twice is what let the gate reject requests the router then served.
server_tools = (
- _selects_server_tools and llama_backend.supports_tools and not _anthropic_has_image
+ bool(selected_server_tools)
+ and llama_backend.supports_tools
+ and not _anthropic_has_image
+ and not _schema_disables_server_tools
)
# One short-circuiting chain: a backend whose supports_tools raises must not turn a plain
# no-tools turn into a 500.
@@ -34763,6 +34829,17 @@ async def anthropic_messages(
openai_client_tools, openai_tool_choice, server_tools, llama_backend
)
+ # Decided on the final routing: a tool request that cannot run tools (image, tool-less
+ # template, tool_choice none) still gets its schema.
+ response_format = _response_format
+ if (
+ response_format is not None
+ and (server_tools or client_tools)
+ and openai_tool_choice != "none"
+ ):
+ logger.warning("Ignoring Anthropic output format: callable tools cannot run under a schema")
+ response_format = None
+
# Studio composes the prompt on every branch but the client-tool passthrough, which forwards
# the caller's own request verbatim (mirrors the GGUF passthrough gate in /chat/completions).
if not client_tools:
@@ -35058,8 +35135,8 @@ async def anthropic_messages(
reservation.cancel()
# ── Client-side pass-through path ─────────────────────────
- if client_tools:
- openai_tools = openai_client_tools
+ if client_tools or response_format is not None:
+ openai_tools = openai_client_tools if client_tools else []
if payload.stream:
return await _admitted_anthropic(
@@ -35085,6 +35162,7 @@ async def anthropic_messages(
cancel_id = payload.cancel_id,
disable_parallel_tool_use = _disable_parallel,
auto_heal_tool_calls = payload.auto_heal_tool_calls,
+ response_format = response_format,
parse_think = _think_parsing_expected(llama_backend, payload),
**_anthropic_reasoning_args(payload),
)
@@ -35111,6 +35189,7 @@ async def anthropic_messages(
nudge_tool_calls = payload.nudge_tool_calls,
request = request,
cancel_event = cancel_event,
+ response_format = response_format,
parse_think = _think_parsing_expected(llama_backend, payload),
**_anthropic_reasoning_args(payload),
)
@@ -35138,19 +35217,13 @@ async def anthropic_messages(
err_type = "invalid_request_error",
),
)
- from core.inference.tools import ALL_TOOLS, apply_full_access_tool_descriptions
+ from core.inference.tools import apply_full_access_tool_descriptions
# ask/auto (and an omitted mode selecting a gate-needing terminal/python
# tool) were already rejected before the auto-switch above, so an invalid
# confirm-gated request never evicts the resident model; the selection
# here just picks the tools for the actual server-tool loop.
- openai_tools = _tools_for_search_images(
- _select_anthropic_server_tools(
- ALL_TOOLS,
- requested_studio_tools,
- payload.enabled_tools,
- )
- )
+ openai_tools = selected_server_tools
# Mirrors _select_request_tools: this path builds its own selection, so
# the Full access swap has to be repeated rather than inherited.
_full_access = bool(getattr(payload, "bypass_permissions", False))
@@ -36340,6 +36413,7 @@ async def _anthropic_passthrough_stream(
cancel_id = None,
disable_parallel_tool_use = False,
auto_heal_tool_calls = None,
+ response_format = None,
enable_thinking = None,
reasoning_effort = None,
preserve_thinking = None,
@@ -36362,6 +36436,7 @@ async def _anthropic_passthrough_stream(
presence_penalty = presence_penalty,
seed = seed,
tool_choice = tool_choice,
+ response_format = response_format,
chat_template_kwargs = _reasoning_template_kwargs(
llama_backend, enable_thinking, reasoning_effort, preserve_thinking
),
@@ -36669,6 +36744,7 @@ async def _anthropic_passthrough_non_streaming(
nudge_tool_calls = None,
request: Optional[Request] = None,
cancel_event = None,
+ response_format = None,
enable_thinking = None,
reasoning_effort = None,
preserve_thinking = None,
@@ -36696,6 +36772,7 @@ async def _anthropic_passthrough_non_streaming(
presence_penalty = presence_penalty,
seed = seed,
tool_choice = tool_choice,
+ response_format = response_format,
chat_template_kwargs = _reasoning_template_kwargs(
llama_backend, enable_thinking, reasoning_effort, preserve_thinking
),
@@ -36849,7 +36926,7 @@ async def _anthropic_passthrough_non_streaming(
# or no-client-tool requests. The protected helper preserves <think> rehearsal and
# balanced [TOOL_CALLS] prose, gated on the declared tools so an inactive
# NAME[ARGS]{...} example is kept.
- if not healing_active:
+ if not healing_active and response_format is None:
text = _strip_tool_xml_for_display(
text,
auto_heal_tool_calls = True,
diff --git a/studio/backend/tests/test_anthropic_structured_outputs.py b/studio/backend/tests/test_anthropic_structured_outputs.py
new file mode 100644
index 00000000000..f7afd950b15
--- /dev/null
+++ b/studio/backend/tests/test_anthropic_structured_outputs.py
@@ -0,0 +1,585 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+import asyncio
+import json
+import os
+import sys
+from types import SimpleNamespace
+
+import httpx
+import pytest
+from fastapi import HTTPException
+
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
+
+import routes.inference as inf_mod
+from core.inference.api_monitor import ApiMonitor
+from core.inference.llama_admission import reset_llama_admission_queues
+from models.inference import AnthropicMessagesRequest
+from routes.inference import anthropic_messages
+from state.tool_policy import reset_tool_policy
+
+_SCHEMA = {
+ "type": "object",
+ "properties": {"name": {"type": "string"}},
+ "required": ["name"],
+ "additionalProperties": False,
+}
+_ANSWER = '{"name": "Ada"}'
+_CLIENT_TOOL = {"name": "lookup", "description": "Look up", "input_schema": {"type": "object"}}
+
+
[email protected](autouse = True)
+def _isolate(monkeypatch):
+ reset_tool_policy()
+ reset_llama_admission_queues()
+ monkeypatch.setattr(inf_mod, "api_monitor", ApiMonitor(max_entries = 64))
+ monkeypatch.setattr(inf_mod, "_CANCEL_REGISTRY", {})
+ monkeypatch.setattr(inf_mod, "current_date_prompt_line", lambda **_kwargs: "")
+ yield
+ reset_llama_admission_queues()
+ reset_tool_policy()
+
+
+class _Request:
+ def __init__(self):
+ self.state = SimpleNamespace()
+ self.url = SimpleNamespace(path = "/v1/messages")
+ self.method = "POST"
+
+ async def is_disconnected(self):
+ return False
+
+
+def _install(
+ monkeypatch,
+ *,
+ supports_tool_passthrough = False,
+ answer = _ANSWER,
+ **overrides,
+):
+ calls = []
+ upstream = []
+
+ def _gen_plain(**kwargs):
+ calls.append(("plain", kwargs))
+ yield "not json"
+
+ def _gen_tools(**kwargs):
+ calls.append(("tools", kwargs))
+ yield {"type": "content", "text": "not json"}
+
+ backend = SimpleNamespace(
+ is_loaded = True,
+ is_vision = False,
+ supports_tools = True,
+ supports_tool_passthrough = supports_tool_passthrough,
+ model_identifier = "test-model",
+ context_length = 2048,
+ count_chat_tokens = lambda *a, **k: 2,
+ generate_chat_completion = _gen_plain,
+ generate_chat_completion_with_tools = _gen_tools,
+ effective_parallel_slots = 1,
+ base_url = "http://llama.structured.test",
+ )
+ for key, value in overrides.items():
+ setattr(backend, key, value)
+ monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ body = json.loads(request.content.decode())
+ upstream.append(body)
+ if body.get("stream"):
+ chunks = [
+ {"choices": [{"delta": {"content": answer}}]},
+ {"choices": [{"delta": {}, "finish_reason": "stop"}]},
+ ]
+ content = "".join(f"data: {json.dumps(c)}\n\n" for c in chunks) + "data: [DONE]\n\n"
+ return httpx.Response(
+ 200, content = content.encode(), headers = {"content-type": "text/event-stream"}
+ )
+ return httpx.Response(
+ 200,
+ json = {
+ "choices": [
+ {
+ "message": {"role": "assistant", "content": answer},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 2, "completion_tokens": 5},
+ },
+ )
+
+ transport = httpx.MockTransport(handler)
+ real_client = httpx.AsyncClient
+ monkeypatch.setattr(
+ inf_mod.httpx,
+ "AsyncClient",
+ lambda *a, **k: real_client(transport = transport, timeout = k.get("timeout", 60)),
+ )
+ return calls, upstream
+
+
+def _payload(**fields) -> AnthropicMessagesRequest:
+ base = {"max_tokens": 64, "messages": [{"role": "user", "content": "Name a scientist."}]}
+ base.update(fields)
+ return AnthropicMessagesRequest(**base)
+
+
+def _run(payload):
+ async def _go():
+ response = await anthropic_messages(payload, request = _Request(), current_subject = "t")
+ if payload.stream:
+ chunks = []
+ async for chunk in response.body_iterator:
+ chunks.append(chunk.decode() if isinstance(chunk, bytes) else chunk)
+ return response.status_code, "".join(chunks)
+ return response.status_code, response.body.decode()
+
+ return asyncio.run(_go())
+
+
+_EXPECTED = {"type": "json_schema", "json_schema": {"name": "response", "schema": _SCHEMA}}
+
+
[email protected]("stream", [False, True])
+ "fields",
+ [
+ {"output_config": {"format": {"type": "json_schema", "schema": _SCHEMA}}},
+ {"output_format": {"type": "json_schema", "schema": _SCHEMA}},
+ ],
+ ids = ["output_config", "legacy_output_format"],
+)
+def test_format_reaches_llama_server_as_response_format(monkeypatch, fields, stream):
+ calls, upstream = _install(monkeypatch)
+
+ status, body = _run(_payload(stream = stream, **fields))
+
+ assert status == 200
+ assert calls == []
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert "tools" not in sent
+ if stream:
+ assert json.dumps(_ANSWER)[1:-1] in body
+ else:
+ assert json.loads(json.loads(body)["content"][0]["text"]) == {"name": "Ada"}
+
+
[email protected]("stream", [False, True])
+def test_format_rides_along_with_uncallable_client_tools(monkeypatch, stream):
+ _calls, upstream = _install(monkeypatch, supports_tool_passthrough = True)
+
+ _run(
+ _payload(
+ stream = stream,
+ tools = [_CLIENT_TOOL],
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert "tools" not in sent
+ assert "tool_choice" not in sent
+
+
+def test_format_keeps_client_tools_needed_by_replayed_history(monkeypatch):
+ _calls, upstream = _install(monkeypatch, supports_tool_passthrough = True)
+
+ _run(
+ _payload(
+ messages = [
+ {"role": "user", "content": "Look up a scientist."},
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_1",
+ "name": "lookup",
+ "input": {"name": "Ada"},
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "tool_result", "tool_use_id": "toolu_1", "content": "Ada"}
+ ],
+ },
+ ],
+ tools = [_CLIENT_TOOL],
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert [tool["function"]["name"] for tool in sent["tools"]] == ["lookup"]
+ assert sent["tool_choice"] == "none"
+
+
+ "fmt",
+ [{"type": "bogus"}, {"type": "json_schema"}, "json_schema"],
+ ids = ["unknown-type", "missing-schema", "not-an-object"],
+)
+def test_unsupported_format_is_ignored_as_before(monkeypatch, fmt):
+ calls, upstream = _install(monkeypatch)
+
+ status, _body = _run(_payload(output_config = {"format": fmt}))
+
+ assert status == 200
+ assert [path for path, _kwargs in calls] == ["plain"]
+ assert upstream == []
+
+
+ "schema",
+ [
+ {
+ "type": "object",
+ "properties": {"name": {"type": "string", "pattern": "[0-9]+"}},
+ "required": ["name"],
+ },
+ {"type": "array", "items": {"type": "string"}, "maxItems": 1998},
+ ],
+ ids = ["unanchored-pattern", "excessive-repetition-bound"],
+)
+def test_format_rejects_schema_constraints_llama_would_drop(monkeypatch, schema):
+ calls, upstream = _install(monkeypatch)
+
+ with pytest.raises(HTTPException) as exc:
+ _run(_payload(output_config = {"format": {"type": "json_schema", "schema": schema}}))
+
+ assert exc.value.status_code == 400
+ assert "cannot enforce" in exc.value.detail["error"]["message"]
+ assert calls == []
+ assert upstream == []
+
+
+ "fields",
+ [
+ {"enable_tools": True, "permission_mode": "off"},
+ {"tools": [_CLIENT_TOOL]},
+ {"tools": [_CLIENT_TOOL], "tool_choice": {"type": "auto"}},
+ ],
+ ids = ["server-tools", "client-tools", "client-tools-auto"],
+)
+def test_format_with_callable_tools_keeps_tools_callable(monkeypatch, fields):
+ calls, upstream = _install(monkeypatch, supports_tool_passthrough = True)
+ payload = _payload(
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}}, **fields
+ )
+
+ status, _body = _run(payload)
+
+ assert status == 200
+ assert calls or upstream
+ assert all("response_format" not in body for body in upstream)
+ assert all(kwargs.get("response_format") is None for _path, kwargs in calls)
+
+
+def test_request_without_format_stays_on_the_plain_path(monkeypatch):
+ calls, upstream = _install(monkeypatch)
+
+ status, _body = _run(_payload(output_config = {"effort": "high"}))
+
+ assert status == 200
+ assert [path for path, _kwargs in calls] == ["plain"]
+ assert upstream == []
+
+
+_PNG_1PX = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
+
+
+ "overrides, content",
+ [
+ ({"supports_tools": False}, "Name a scientist."),
+ (
+ {"is_vision": True},
+ [
+ {
+ "type": "image",
+ "source": {"type": "base64", "media_type": "image/png", "data": _PNG_1PX},
+ },
+ {"type": "text", "text": "Name a scientist."},
+ ],
+ ),
+ ],
+ ids = ["toolless-backend", "top-level-image"],
+)
+def test_format_kept_when_requested_server_tools_cannot_run(monkeypatch, overrides, content):
+ calls, upstream = _install(monkeypatch, **overrides)
+
+ status, _body = _run(
+ _payload(
+ enable_tools = True,
+ permission_mode = "off",
+ messages = [{"role": "user", "content": content}],
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ assert status == 200
+ assert calls == []
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+
+
[email protected]("enabled_tools", [[], ["unavailable_tool"]])
+def test_format_kept_when_server_tool_selection_is_empty(monkeypatch, enabled_tools):
+ calls, upstream = _install(monkeypatch)
+
+ status, _body = _run(
+ _payload(
+ enable_tools = True,
+ enabled_tools = enabled_tools,
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ assert status == 200
+ assert calls == []
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert "tools" not in sent
+
+
+def test_format_kept_when_server_tools_are_disabled_by_tool_choice(monkeypatch):
+ calls, upstream = _install(monkeypatch)
+
+ status, _body = _run(
+ _payload(
+ enable_tools = True,
+ permission_mode = "off",
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ assert status == 200
+ assert calls == []
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert "tools" not in sent
+
+
+def test_format_with_tool_choice_none_does_not_require_server_tool_permission(monkeypatch):
+ calls, upstream = _install(monkeypatch)
+
+ status, _body = _run(
+ _payload(
+ enable_tools = True,
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ assert status == 200
+ assert calls == []
+ [sent] = upstream
+ assert sent["response_format"] == _EXPECTED
+ assert "tools" not in sent
+
+
+def test_schema_only_server_tool_request_does_not_add_date_for_api_key(monkeypatch):
+ _calls, upstream = _install(monkeypatch)
+ monkeypatch.setattr(
+ inf_mod, "current_date_prompt_line", lambda **_kwargs: "Current date: 2026-09-25"
+ )
+ monkeypatch.setattr(inf_mod, "_request_has_api_key", lambda _request: True)
+ monkeypatch.setattr(inf_mod, "_request_is_internal_workflow", lambda _request: False)
+
+ status, _body = _run(
+ _payload(
+ enable_tools = True,
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ )
+ )
+
+ assert status == 200
+ [sent] = upstream
+ assert sent["messages"] == [{"role": "user", "content": "Name a scientist."}]
+
+
[email protected]("with_format", [False, True])
+def test_count_tokens_matches_schema_routing_under_tool_choice_none(monkeypatch, with_format):
+ counted = []
+
+ def _count(messages, _template, tools, **_kwargs):
+ counted.append((messages, tools))
+ return 2
+
+ _install(monkeypatch, count_chat_tokens = _count)
+ fields = {"enable_tools": True, "permission_mode": "off", "tool_choice": {"type": "none"}}
+ if with_format:
+ fields["output_config"] = {"format": {"type": "json_schema", "schema": _SCHEMA}}
+
+ response = asyncio.run(
+ inf_mod.anthropic_count_tokens(_payload(**fields), request = _Request(), current_subject = "t")
+ )
+
+ assert response.status_code == 200
+ [(_messages, tools)] = counted
+ assert bool(tools) is not with_format
+
+
+def test_count_tokens_omits_server_tool_date_for_schema_only_api_key(monkeypatch):
+ counted = []
+
+ def _count(messages, _template, tools, **_kwargs):
+ counted.append((messages, tools))
+ return 2
+
+ _install(monkeypatch, count_chat_tokens = _count)
+ monkeypatch.setattr(
+ inf_mod, "current_date_prompt_line", lambda **_kwargs: "Current date: 2026-09-25"
+ )
+ monkeypatch.setattr(inf_mod, "_request_has_api_key", lambda _request: True)
+ monkeypatch.setattr(inf_mod, "_request_is_internal_workflow", lambda _request: False)
+
+ response = asyncio.run(
+ inf_mod.anthropic_count_tokens(
+ _payload(
+ enable_tools = True,
+ tool_choice = {"type": "none"},
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ ),
+ request = _Request(),
+ current_subject = "t",
+ )
+ )
+
+ assert response.status_code == 200
+ [(messages, tools)] = counted
+ assert tools is None
+ assert messages == [{"role": "user", "content": "Name a scientist."}]
+
+
+def test_count_tokens_omits_server_tool_date_when_selection_is_empty(monkeypatch):
+ counted = []
+
+ def _count(messages, _template, tools, **_kwargs):
+ counted.append((messages, tools))
+ return 2
+
+ _install(monkeypatch, count_chat_tokens = _count)
+ monkeypatch.setattr(
+ inf_mod, "current_date_prompt_line", lambda **_kwargs: "Current date: 2026-09-25"
+ )
+ monkeypatch.setattr(inf_mod, "_request_has_api_key", lambda _request: True)
+ monkeypatch.setattr(inf_mod, "_request_is_internal_workflow", lambda _request: False)
+
+ response = asyncio.run(
+ inf_mod.anthropic_count_tokens(
+ _payload(
+ enable_tools = True,
+ enabled_tools = [],
+ output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ ),
+ request = _Request(),
+ current_subject = "t",
+ )
+ )
+
+ assert response.status_code == 200
+ [(messages, tools)] = counted
+ assert tools is None
+ assert messages == [{"role": "user", "content": "Name a scientist."}]
+
+
+def test_count_tokens_accepts_schema_constraints_without_compiling(monkeypatch):
+ counted = []
+
+ def _count(*_args, **_kwargs):
+ counted.append(True)
+ return 2
+
+ _install(monkeypatch, count_chat_tokens = _count)
+ schema = {
+ "type": "object",
+ "properties": {"name": {"type": "string", "pattern": "[0-9]+"}},
+ "required": ["name"],
+ }
+
+ response = asyncio.run(
+ inf_mod.anthropic_count_tokens(
+ _payload(output_config = {"format": {"type": "json_schema", "schema": schema}}),
+ request = _Request(),
+ current_subject = "t",
+ )
+ )
+
+ assert response.status_code == 200
+ assert counted == [True]
+
+
[email protected]("replayed_history", [False, True])
+def test_count_tokens_matches_withdrawn_client_tools(monkeypatch, replayed_history):
+ counted = []
+
+ def _count(messages, _template, tools, **_kwargs):
+ counted.append((messages, tools))
+ return 2
+
+ _install(monkeypatch, supports_tool_passthrough = True, count_chat_tokens = _count)
+ fields = {
+ "tools": [_CLIENT_TOOL],
+ "tool_choice": {"type": "none"},
+ "output_config": {"format": {"type": "json_schema", "schema": _SCHEMA}},
+ }
+ if replayed_history:
+ fields["messages"] = [
+ {"role": "user", "content": "Look up a scientist."},
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_1",
+ "name": "lookup",
+ "input": {"name": "Ada"},
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": "Ada"}],
+ },
+ ]
+
+ response = asyncio.run(
+ inf_mod.anthropic_count_tokens(_payload(**fields), request = _Request(), current_subject = "t")
+ )
+
+ assert response.status_code == 200
+ [(_messages, tools)] = counted
+ assert bool(tools) is replayed_history
+
+
[email protected]("stream", [False, True])
+def test_tool_markup_inside_json_is_returned_verbatim(monkeypatch, stream):
+ answer = json.dumps({"a": "<function=f>", "b": 2, "c": "</function>"})
+ _install(monkeypatch, answer = answer)
+
+ status, body = _run(
+ _payload(
+ stream = stream, output_config = {"format": {"type": "json_schema", "schema": _SCHEMA}}
+ )
+ )
+
+ assert status == 200
+ if stream:
+ assert json.dumps(answer)[1:-1] in body
+ else:
+ assert json.loads(body)["content"][0]["text"] == answer
diff --git a/studio/backend/tests/test_tool_xml_strip.py b/studio/backend/tests/test_tool_xml_strip.py
index ad8e8576463..caf17926026 100644
--- a/studio/backend/tests/test_tool_xml_strip.py
+++ b/studio/backend/tests/test_tool_xml_strip.py
@@ -697,7 +697,7 @@ def test_route_history_and_passthrough_forward_the_display_gate():
"safetensors history": r"Strip stale tool-call XML from prior assistant turns.*?\.strip\(\)",
"anthropic history": r"Strip stale tool-call XML via the protected display helper.*?\.strip\(\)",
# Anchored on the code, not the comment above it, so rewrapping prose cannot break this.
- "anthropic passthrough": r"if not healing_active:.*?\.strip\(\)",
+ "anthropic passthrough": r"if not healing_active\b.*?\.strip\(\)",
}
for label, pat in blocks.items():
m = _re.search(pat, _src, _re.DOTALL)