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: enable anthropic studio tools (#12497)

Maheswar Kumarf5e78f11284d
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index a3f07a4d1b2..d70c67233e0 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -1600,6 +1600,7 @@ class ExternalProviderClient:
compaction_threshold,
tool_choice,
fast_mode = fast_mode,
+ tools = tools,
):
yield line
return
@@ -2414,6 +2415,7 @@ class ExternalProviderClient:
tool_choice: Optional[Any] = None,
*,
fast_mode: Optional[bool] = None,
+ tools: Optional[list[dict[str, Any]]] = None,
) -> AsyncGenerator[str, None]:
"""Call the Anthropic Messages API and translate its SSE to OpenAI format:
content_block_delta -> chunk with delta.content, message_delta -> chunk with
@@ -2434,6 +2436,11 @@ class ExternalProviderClient:
continue
content = msg.get("content")
+ extra = msg.get("extra_content") or {}
+ native_content = (extra.get("anthropic") or {}).get("content")
+ if msg.get("role") == "assistant" and isinstance(native_content, list):
+ # Replay the signed native blocks, not the loop's <think>-marked display text; kept calls follow.
+ content = []
# OpenAI role="tool" with list content -> Anthropic native tool_result block on a user message.
# Translating only in the string-content branch below would forward the list-content form as an invalid
# `role:"tool"` message Anthropic rejects, so handle both upfront.
@@ -2441,6 +2448,7 @@ class ExternalProviderClient:
_tr_id = msg.get("tool_call_id") or ""
if isinstance(content, list):
_flat_parts: list[str] = []
+ _result_images: list[dict[str, Any]] = []
for part in content:
if (
isinstance(part, dict)
@@ -2448,31 +2456,58 @@ class ExternalProviderClient:
and part.get("text")
):
_flat_parts.append(str(part["text"]))
+ elif isinstance(part, dict) and part.get("type") == "image_url":
+ image_url = part.get("image_url", {}).get("url", "")
+ if image_url.startswith("data:"):
+ header, _, data = image_url.partition(",")
+ source = {
+ "type": "base64",
+ "media_type": header[5:].split(";")[0],
+ "data": data,
+ }
+ else:
+ source = {"type": "url", "url": image_url}
+ _result_images.append({"type": "image", "source": source})
_flat_result = "".join(_flat_parts)
+ if _result_images:
+ _flat_result = (
+ [{"type": "text", "text": _flat_result}] if _flat_result else []
+ ) + _result_images
elif content is None:
_flat_result = ""
elif isinstance(content, str):
_flat_result = content
else:
_flat_result = _json.dumps(content)
- filtered.append(
- {
- "role": "user",
- "content": [
- {
- "type": "tool_result",
- "tool_use_id": _tr_id,
- "content": _flat_result,
- }
- ],
- }
- )
+ result_block = {
+ "type": "tool_result",
+ "tool_use_id": _tr_id,
+ "content": _flat_result,
+ }
+ if (
+ filtered
+ and filtered[-1]["role"] == "user"
+ and isinstance(filtered[-1]["content"], list)
+ and all(part.get("type") == "tool_result" for part in filtered[-1]["content"])
+ ):
+ filtered[-1]["content"].append(result_block)
+ else:
+ filtered.append({"role": "user", "content": [result_block]})
continue
if isinstance(content, list):
# Translate OpenAI multimodal parts -> Anthropic native shapes: `image_url` -> `{type:"image",
# source:...}`, and `input_document` -> `{type:"document", source:...}` (an Unsloth extension
# mirroring Anthropic's document block, which supports PDFs as base64 or URL).
- anthropic_parts: list[dict[str, Any]] = []
+ anthropic_parts: list[dict[str, Any]] = (
+ [
+ block
+ for block in native_content
+ if block.get("type") != "text"
+ or _anthropic_text_is_sendable(block.get("text"))
+ ]
+ if msg.get("role") == "assistant" and isinstance(native_content, list)
+ else []
+ )
for part in content:
if part.get("type") == "text" and _anthropic_text_is_sendable(part.get("text")):
anthropic_parts.append({"type": "text", "text": part["text"]})
@@ -2558,6 +2593,12 @@ class ExternalProviderClient:
# Assistant tool_calls -> Anthropic tool_use blocks appended to the same message: the native Messages
# API does not accept OpenAI's top-level `tool_calls` field, the call lives inside a content block
# `{type:"tool_use", id, name, input}`.
+ # Native client calls keep their streamed position; the loop's retained calls fill them in place.
+ native_calls = {
+ block.get("id"): i
+ for i, block in enumerate(anthropic_parts)
+ if block.get("type") == "tool_use"
+ }
if msg.get("role") == "assistant" and isinstance(msg.get("tool_calls"), list):
for _tc in msg["tool_calls"]:
if not isinstance(_tc, dict):
@@ -2572,14 +2613,22 @@ class ExternalProviderClient:
_input = {"_raw": _raw}
if not isinstance(_input, dict):
_input = {"value": _input}
- anthropic_parts.append(
- {
- "type": "tool_use",
- "id": _tc.get("id") or f"toolu_{time.time_ns()}",
- "name": _fn["name"],
- "input": _input,
- }
- )
+ _tool_use = {
+ "type": "tool_use",
+ "id": _tc.get("id") or f"toolu_{time.time_ns()}",
+ "name": _fn["name"],
+ "input": _input,
+ }
+ _slot = native_calls.pop(_tool_use["id"], None)
+ if _slot is None:
+ anthropic_parts.append(_tool_use)
+ else:
+ anthropic_parts[_slot] = _tool_use
+ if native_calls:
+ _withheld = set(native_calls.values())
+ anthropic_parts = [
+ part for i, part in enumerate(anthropic_parts) if i not in _withheld
+ ]
# Skip whole-message append when nothing usable survived. An empty content array (e.g. user dropped
# only an unparseable `input_document`) would 400 with "messages.N.content: at least one block is
# required".
@@ -2653,6 +2702,19 @@ class ExternalProviderClient:
continue
filtered.append(msg)
+ pending_server_calls: dict[str, str] = {}
+ for message in filtered:
+ if message["role"] != "assistant" or not isinstance(message["content"], list):
+ continue
+ for block in message["content"]:
+ if block.get("type") == "server_tool_use":
+ pending_server_calls[block["id"]] = block["name"]
+ elif block.get("tool_use_id"):
+ pending_server_calls.pop(block["tool_use_id"], None)
+ pending_hosted_tools = set(pending_server_calls.values())
+ if pending_hosted_tools & {"bash_code_execution", "text_editor_code_execution"}:
+ pending_hosted_tools.add("code_execution")
+
# Newer Claude models removed temperature/top_p/top_k entirely (400 "deprecated for this model"). Reuse the
# capability wherever those fields are set, including the thinking-mode temperature override.
sampling_removed = _anthropic_sampling_params_removed(model)
@@ -2785,9 +2847,61 @@ class ExternalProviderClient:
_anthropic_hosted_builtins_allowed = (
not _anthropic_tool_choice_disabled and not _anthropic_tool_choice_forced_function
)
+ if _anthropic_tool_choice_disabled and pending_hosted_tools:
+ # A pending server call still needs its declaration on the result-only continuation. Disallow new calls.
+ body["tool_choice"] = {"type": "none"}
+
+ # Anthropic 400s a history holding tool blocks without `tools`, so a withdrawn catalog stays declared.
+ history_has_tool_blocks = any(
+ isinstance(message["content"], list)
+ and any(
+ block.get("type") in ("tool_use", "tool_result") for block in message["content"]
+ )
+ for message in filtered
+ )
+ if tools and (not _anthropic_tool_choice_disabled or history_has_tool_blocks):
+ client_tools = []
+ for tool in tools:
+ if tool.get("type") != "function":
+ continue
+ function = tool.get("function") or {}
+ if not function.get("name"):
+ continue
+ entry = {
+ "name": function["name"],
+ "input_schema": function.get("parameters")
+ or {"type": "object", "properties": {}},
+ }
+ for key in ("description", "strict"):
+ if key in function:
+ entry[key] = function[key]
+ client_tools.append(entry)
+ if client_tools:
+ body["tools"] = client_tools
+ if _anthropic_tool_choice_disabled:
+ body["tool_choice"] = {"type": "none"}
+ elif _anthropic_tool_choice_forced_function:
+ body["tool_choice"] = {"type": "tool", "name": tool_choice["function"]["name"]}
+ elif tool_choice == "required":
+ body["tool_choice"] = {"type": "any"}
+ else:
+ body["tool_choice"] = {"type": "auto"}
+ # Manual thinking rejects forced tool use. Honor the explicit selection on that request.
+ if (
+ body["tool_choice"]["type"] in ("any", "tool")
+ and (body.get("thinking") or {}).get("type") == "enabled"
+ ):
+ body.pop("thinking")
+
+ client_tool_names = {tool["name"] for tool in body.get("tools", [])}
# Anthropic web_search (date-pinned per model family).
- if _anthropic_hosted_builtins_allowed and enabled_tools and "web_search" in enabled_tools:
+ if "web_search" in pending_hosted_tools or (
+ _anthropic_hosted_builtins_allowed
+ and enabled_tools
+ and "web_search" in enabled_tools
+ and "web_search" not in client_tool_names
+ ):
anthropic_tools = list(body.get("tools") or [])
anthropic_tools.append(
{
@@ -2800,7 +2914,13 @@ class ExternalProviderClient:
# Anthropic web_fetch: only URLs already in conversation. Date-pinned.
web_fetch_enabled = bool(
- _anthropic_hosted_builtins_allowed and enabled_tools and "web_fetch" in enabled_tools
+ "web_fetch" in pending_hosted_tools
+ or (
+ _anthropic_hosted_builtins_allowed
+ and enabled_tools
+ and "web_fetch" in enabled_tools
+ and "web_fetch" not in client_tool_names
+ )
)
if web_fetch_enabled:
anthropic_tools = list(body.get("tools") or [])
@@ -2816,9 +2936,13 @@ class ExternalProviderClient:
# Anthropic server-side code execution (date-pinned type per model, both unlocked by the same beta header set
# below).
code_execution_enabled = bool(
- _anthropic_hosted_builtins_allowed
- and enabled_tools
- and "code_execution" in enabled_tools
+ "code_execution" in pending_hosted_tools
+ or (
+ _anthropic_hosted_builtins_allowed
+ and enabled_tools
+ and "code_execution" in enabled_tools
+ and "code_execution" not in client_tool_names
+ )
)
if code_execution_enabled:
anthropic_tools = list(body.get("tools") or [])
@@ -2954,6 +3078,11 @@ class ExternalProviderClient:
# NOTE: same manual __anext__ loop as stream_chat_completion — see comment there.
lines_gen = response.aiter_lines().__aiter__()
thinking_open = False
+ client_tool_indices: dict[int, int] = {}
+ # Held until message_stop so an errored or cut-off stream never executes a partial call.
+ client_tool_chunks: list[str] = []
+ replay_blocks: dict[int, dict[str, Any]] = {}
+ replay_inputs: dict[int, str] = {}
# Diagnostic counters for "no thinking content" reports -- distinguish "Anthropic never sent
# thinking_delta" from "frontend did not render the chunks".
event_counts: dict[str, int] = {}
@@ -3011,6 +3140,15 @@ class ExternalProviderClient:
}
return f"data: {_json.dumps(chunk)}"
+ def _delta_chunk(delta: dict[str, Any]) -> str:
+ return "data: " + _json.dumps(
+ {
+ "id": completion_id,
+ "object": "chat.completion.chunk",
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}],
+ }
+ )
+
def _emit_tool_event(payload: dict[str, Any]) -> str:
_stamp_server_tool_marker(payload)
chunk = {
@@ -3157,7 +3295,29 @@ class ExternalProviderClient:
content_block = event.get("content_block") or {}
block_type = content_block.get("type")
block_name = content_block.get("name")
- if block_type == "server_tool_use" and block_name == "web_search":
+ block_index = event.get("index", 0)
+ if tools:
+ replay_blocks[block_index] = dict(content_block)
+ if block_type == "tool_use":
+ client_tool_indices[block_index] = len(client_tool_indices)
+ client_tool_chunks.append(
+ _delta_chunk(
+ {
+ "tool_calls": [
+ {
+ "index": client_tool_indices[block_index],
+ "id": content_block["id"],
+ "type": "function",
+ "function": {
+ "name": block_name,
+ "arguments": "",
+ },
+ }
+ ]
+ }
+ )
+ )
+ elif block_type == "server_tool_use" and block_name == "web_search":
tool_use_id = content_block.get("id", "") or (
f"ws_{len(web_search_calls)}"
)
@@ -3239,6 +3399,34 @@ class ExternalProviderClient:
elif event_type == "content_block_delta":
delta = event.get("delta", {})
delta_type = delta.get("type")
+ block_index = event.get("index", 0)
+ replay = replay_blocks.get(block_index)
+ if replay is not None:
+ field = {
+ "text_delta": "text",
+ "thinking_delta": "thinking",
+ "signature_delta": "signature",
+ }.get(delta_type)
+ if field == "text" and replay.get("type") == "compaction":
+ field = "content"
+ if field:
+ replay[field] = replay.get(field, "") + delta.get(
+ "text" if field == "content" else field, ""
+ )
+ elif delta_type == "input_json_delta":
+ replay_inputs[block_index] = replay_inputs.get(
+ block_index, ""
+ ) + delta.get("partial_json", "")
+ elif delta_type == "citations_delta":
+ replay.setdefault("citations", []).append(delta["citation"])
+ elif delta_type == "compaction_delta":
+ replay.update(
+ {
+ key: value
+ for key, value in delta.items()
+ if key != "type"
+ }
+ )
if delta_type == "thinking_delta":
# Wrap as <think>...</think> for parseAssistantContent.
thinking_text = delta.get("thinking", "")
@@ -3285,16 +3473,36 @@ class ExternalProviderClient:
# partial_json carrying tool inputs (web_search query, code-exec command, etc.); route
# to whichever buffer is open.
partial = delta.get("partial_json", "")
- if current_server_tool_use is not None:
+ if block_index in client_tool_indices:
+ client_tool_chunks.append(
+ _delta_chunk(
+ {
+ "tool_calls": [
+ {
+ "index": client_tool_indices[block_index],
+ "function": {"arguments": partial},
+ }
+ ]
+ }
+ )
+ )
+ elif current_server_tool_use is not None:
current_server_tool_use["buffer"] += partial
elif current_code_exec_use is not None:
current_code_exec_use["buffer"] += partial
elif current_web_fetch_use is not None:
current_web_fetch_use["buffer"] += partial
- # signature_delta and other delta types are skipped -- they carry trust / verification
- # metadata, not user-visible content.
elif event_type == "content_block_stop":
+ block_index = event.get("index", 0)
+ if block_index in replay_inputs:
+ try:
+ replay_blocks[block_index]["input"] = _json.loads(
+ replay_inputs.pop(block_index)
+ )
+ except _json.JSONDecodeError:
+ # max_tokens can leave a partial tool block. Still process its terminal reason.
+ replay_blocks.pop(block_index)
if current_server_tool_use is not None:
# End of the server_tool_use block -- parse the accumulated input_json into a query
# and emit tool_start. The matching tool_end fires later when the
@@ -3456,6 +3664,14 @@ class ExternalProviderClient:
thinking_open = False
elif event_type == "message_delta":
+ if replay_blocks:
+ yield _delta_chunk(
+ {
+ "extra_content": {
+ "anthropic": {"content": list(replay_blocks.values())}
+ }
+ }
+ )
delta_usage = event.get("usage")
if isinstance(delta_usage, dict):
last_usage.update(delta_usage)
@@ -3546,9 +3762,14 @@ class ExternalProviderClient:
}
],
}
- yield f"data: {_json.dumps(chunk)}"
+ if client_tool_indices:
+ client_tool_chunks.append(f"data: {_json.dumps(chunk)}")
+ else:
+ yield f"data: {_json.dumps(chunk)}"
elif event_type == "message_stop":
+ for client_chunk in client_tool_chunks:
+ yield client_chunk
if thinking_open:
yield _content_chunk("</think>")
thinking_open = False
diff --git a/studio/backend/core/inference/external_tool_transport.py b/studio/backend/core/inference/external_tool_transport.py
index 6817e52bdf6..548fcd9886f 100644
--- a/studio/backend/core/inference/external_tool_transport.py
+++ b/studio/backend/core/inference/external_tool_transport.py
@@ -62,6 +62,10 @@ class OAICompatTransport:
self._model = model
self._continue_final_message = continue_final_message
self._request_kwargs = request_kwargs
+ # Anthropic can leave a hosted call pending beside a client call; its continuation accepts only tool results.
+ self.tool_result_only_continuation = client.provider_type == "anthropic"
+ self._initial_message_count: int | None = None
+ self._last_tools: list[dict[str, Any]] | None = None
self.preserves_reasoning = (
client.provider_type == "llama_cpp" and request_kwargs.get("preserve_thinking") is True
)
@@ -74,6 +78,32 @@ class OAICompatTransport:
tool_choice: Any,
cancel_event: threading.Event,
) -> AsyncIterator[str]:
+ if tools:
+ self._last_tools = tools
+ elif self.tool_result_only_continuation and self._last_tools:
+ tools, tool_choice = self._last_tools, "none"
+ if self._initial_message_count is not None and self.tool_result_only_continuation:
+ # Promoted MCP images join the tool result: a new user turn would end a pending server-tool turn.
+ normalized = list(messages[: self._initial_message_count])
+ for message in messages[self._initial_message_count :]:
+ if (
+ message.get("role") == "user"
+ and normalized
+ and normalized[-1].get("role") == "tool"
+ ):
+ parts = []
+ for source in (normalized[-1], message):
+ content = source.get("content")
+ if isinstance(content, list):
+ parts.extend(content)
+ elif isinstance(content, str) and content:
+ parts.append({"type": "text", "text": content})
+ normalized[-1] = {**normalized[-1], "content": parts}
+ else:
+ normalized.append(message)
+ messages = normalized
+ elif self._initial_message_count is None:
+ self._initial_message_count = len(messages)
# "Resume the trailing assistant turn", so it is only ever true of the first request. Once a tool runs the
# conversation ends with a role="tool" result (or a role="user" no-op note), and vLLM / llama.cpp would splice
# the generation prompt off the end of *that* message: the model continues the tool output instead of answering
diff --git a/studio/backend/core/inference/providers.py b/studio/backend/core/inference/providers.py
index 04cf769cf34..dee0d561ae5 100644
--- a/studio/backend/core/inference/providers.py
+++ b/studio/backend/core/inference/providers.py
@@ -129,7 +129,8 @@ PROVIDER_REGISTRY: dict[str, dict[str, Any]] = {
# sync-external-providers.ts held a copy and had to go with it.
"supports_streaming": True,
"supports_vision": True,
- "supports_tool_calling": False,
+ "supports_tool_calling": True,
+ "studio_tools": True,
# Anthropic's own server tools, appended by `_stream_anthropic`.
"hosted_tools": ("web_search", "web_fetch", "code_execution"),
"auth_header": "x-api-key",
@@ -516,14 +517,8 @@ def provider_runs_local_tools(provider_type: str | None) -> bool:
Unsloth's tools (web_search, python, terminal, MCP, knowledge-base search) execute on the
Unsloth host, so any provider whose wire format can carry a tool schema out and a tool result
- back can use them: the whole OpenAI-compatible family plus Gemini, whose native shape is
- translated to and from OpenAI chunks in ``external_provider.py``.
-
- Anthropic is deliberately absent: ``_stream_anthropic`` only appends Anthropic's own hosted
- builtins and never forwards a caller's function-tool schemas, so the loop would advertise a
- catalog the model never sees. Enabling it needs OpenAI -> Anthropic schema translation plus
- tool_use / tool_result message replay, which is separate work. Anthropic keeps its hosted
- web_search, web_fetch and code_execution meanwhile.
+ back can use them: the whole OpenAI-compatible family plus Gemini and Anthropic, whose
+ native shapes are translated to and from OpenAI chunks in ``external_provider.py``.
"""
# isinstance, not a truthiness check: the value reaches here straight from a
# request body, and a list or dict key raises TypeError inside dict.get, which
diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.py
index 9d15addae17..81ede733b6a 100644
--- a/studio/backend/core/inference/studio_tool_loop.py
+++ b/studio/backend/core/inference/studio_tool_loop.py
@@ -1717,7 +1717,17 @@ async def stream_with_studio_tools(
decision.provenance["round_id"] = round_id
if not decision.should_execute:
completion = controller.record_noop(decision)
- noop_messages.append(completion.model_message())
+ if getattr(transport, "tool_result_only_continuation", False):
+ assistant_tool_calls.append(decision.as_assistant_tool_call())
+ tool_messages.append(
+ {
+ "role": "tool",
+ "tool_call_id": decision.tool_call_id,
+ "content": completion.model_message()["content"],
+ }
+ )
+ else:
+ noop_messages.append(completion.model_message())
# The provider's own tool_calls delta for this call was relayed verbatim while it streamed and the
# client painted a card from it. Nothing else closes that card, so without a terminal event it spins
# for the rest of the answer and then reads as a tool that ran and returned nothing. Keyed on the
@@ -2047,7 +2057,8 @@ async def stream_with_studio_tools(
spent_budget_passes += 1
# The catalog is gone from here on, so say why rather than letting the next pass ask for a tool no longer
# offered
- _append_user_turn(conversation, _BUDGET_EXHAUSTED_NUDGE)
+ if not getattr(transport, "tool_result_only_continuation", False):
+ _append_user_turn(conversation, _BUDGET_EXHAUSTED_NUDGE)
usage_line = _usage_chunk_line(model_name, usage_totals, last_timings)
if usage_line is not None:
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 1c69bff515f..e04ffc8e44f 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -26828,11 +26828,19 @@ def _build_external_messages(
result = []
for msg in messages:
- reasoning = (
+ replay = (
{"reasoning_content": msg.reasoning_content}
if provider_type == "llama_cpp" and msg.role == "assistant" and msg.reasoning_content
else {}
)
+ if anthropic and msg.role == "assistant" and isinstance(msg.extra_content, dict):
+ native = msg.extra_content.get("anthropic")
+ if (
+ isinstance(native, dict)
+ and isinstance(native.get("content"), list)
+ and native["content"]
+ ):
+ replay["extra_content"] = {"anthropic": {"content": native["content"]}}
# Drop role=tool messages whose matching server-builtin tool_call was
# filtered above. An orphan tool_result with no matching tool_call is
# rejected by OpenAI Responses and Anthropic.
@@ -26842,7 +26850,7 @@ def _build_external_messages(
and msg.tool_call_id in dropped_server_builtin_tool_call_ids
):
continue
- if isinstance(msg.content, str) or (msg.content is None and reasoning):
+ if isinstance(msg.content, str) or (msg.content is None and replay):
# Drop bare assistant messages with no content AND no tool_calls
# (some providers reject empty assistant turns). Preserve assistant
# turns whose only payload is tool_calls so multi-turn
@@ -26851,15 +26859,15 @@ def _build_external_messages(
msg.role == "assistant"
and not (msg.content or "").strip()
and not msg.tool_calls
- and not reasoning
+ and not replay
):
continue
- out: dict[str, Any] = {"role": msg.role, "content": msg.content or "", **reasoning}
+ out: dict[str, Any] = {"role": msg.role, "content": msg.content or "", **replay}
if msg.role == "assistant" and msg.tool_calls:
_tcs = _filter_tool_calls(msg.tool_calls)
if _tcs:
out["tool_calls"] = _tcs
- elif not (msg.content or "").strip() and not reasoning:
+ elif not (msg.content or "").strip() and not replay:
# Every tool_call was a dropped synthetic provider card;
# the turn would be an empty
# `{"role":"assistant","content":""}` that some providers
@@ -26934,17 +26942,17 @@ def _build_external_messages(
# `compaction` block; every other provider would 400 on
# the unknown part, so gate by provider_type.
parts.append({"type": "compaction", "content": part.content})
- entry: dict[str, Any] = {"role": msg.role, "content": parts, **reasoning}
+ entry: dict[str, Any] = {"role": msg.role, "content": parts, **replay}
if msg.role == "assistant" and msg.tool_calls:
_tcs = _filter_tool_calls(msg.tool_calls)
if _tcs:
entry["tool_calls"] = _tcs
- elif not parts and not reasoning:
+ elif not parts and not replay:
# All tool_calls were synthetic and dropped, and no
# content parts survived. Skip rather than forward an
# empty assistant turn that downstream providers reject.
continue
- elif msg.role == "assistant" and not parts and not reasoning:
+ elif msg.role == "assistant" and not parts and not replay:
continue
if msg.role == "tool":
if msg.tool_call_id:
@@ -26971,14 +26979,14 @@ def _build_external_messages(
preserved.append(_rp)
elif p.type == "compaction" and anthropic:
preserved.append({"type": "compaction", "content": p.content})
- if msg.role == "assistant" and not preserved and not reasoning:
+ if msg.role == "assistant" and not preserved and not replay:
continue
if len(preserved) == 1 and preserved[0]["type"] == "text":
# Single text part collapses to a string for providers that
# don't accept content arrays.
- entry = {"role": msg.role, "content": preserved[0]["text"], **reasoning}
+ entry = {"role": msg.role, "content": preserved[0]["text"], **replay}
else:
- entry = {"role": msg.role, "content": preserved, **reasoning}
+ entry = {"role": msg.role, "content": preserved, **replay}
if msg.role == "assistant" and msg.tool_calls:
_tcs = _filter_tool_calls(msg.tool_calls)
if _tcs:
@@ -26990,7 +26998,7 @@ def _build_external_messages(
_has_text = (
isinstance(_entry_content, str) and _entry_content.strip()
) or (isinstance(_entry_content, list) and len(_entry_content) > 0)
- if not _has_text and not reasoning:
+ if not _has_text and not replay:
continue
if msg.role == "tool":
if msg.tool_call_id:
diff --git a/studio/backend/tests/test_anthropic_studio_tools.py b/studio/backend/tests/test_anthropic_studio_tools.py
new file mode 100644
index 00000000000..e2471da8a73
--- /dev/null
+++ b/studio/backend/tests/test_anthropic_studio_tools.py
@@ -0,0 +1,775 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Native Anthropic calls through the real Studio transport and execution loop."""
+
+import asyncio
+import json
+import threading
+
+import httpx
+import pytest
+
+from core.inference import external_provider as ep
+from core.inference import studio_tool_loop as loop
+from core.inference.external_tool_transport import OAICompatTransport
+
+
+TOOLS = [
+ {
+ "type": "function",
+ "function": {
+ "name": name,
+ "description": f"Run {name}",
+ "parameters": {
+ "type": "object",
+ "properties": {"query": {"type": "string"}},
+ "required": ["query"],
+ },
+ },
+ }
+ for name in ("python", "web_search")
+]
+
+
+def _block(index, block, *deltas):
+ return [
+ {"type": "content_block_start", "index": index, "content_block": block},
+ *({"type": "content_block_delta", "index": index, "delta": delta} for delta in deltas),
+ {"type": "content_block_stop", "index": index},
+ ]
+
+
+def _finish(reason):
+ return [
+ {"type": "message_delta", "delta": {"stop_reason": reason}, "usage": {"output_tokens": 10}},
+ {"type": "message_stop"},
+ ]
+
+
+def _response(events):
+ return httpx.Response(
+ 200,
+ content = "".join(f"data: {json.dumps(event)}\n\n" for event in events).encode(),
+ headers = {"content-type": "text/event-stream"},
+ )
+
+
+def _client(monkeypatch, handler):
+ monkeypatch.setattr(
+ ep, "_http_client", httpx.AsyncClient(transport = httpx.MockTransport(handler))
+ )
+ return ep.ExternalProviderClient(
+ provider_type = "anthropic", base_url = "https://api.anthropic.com/v1", api_key = "test"
+ )
+
+
+def _request(client, **kwargs):
+ return client.stream_chat_completion(
+ messages = kwargs.pop("messages", [{"role": "user", "content": "Use the tools"}]),
+ model = kwargs.pop("model", "claude-sonnet-4-6"),
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ enable_prompt_caching = False,
+ **kwargs,
+ )
+
+
+def _collect(client, **kwargs):
+ async def run():
+ try:
+ return [line async for line in _request(client, **kwargs)]
+ finally:
+ await client.close()
+
+ return asyncio.run(run())
+
+
+ "choice,expected",
+ [
+ (None, {"type": "auto"}),
+ ("auto", {"type": "auto"}),
+ ("required", {"type": "any"}),
+ ({"type": "function", "function": {"name": "python"}}, {"type": "tool", "name": "python"}),
+ ("none", None),
+ ],
+)
+def test_function_schemas_and_choice_reach_anthropic(monkeypatch, choice, expected):
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(client, tools = TOOLS, tool_choice = choice, enabled_tools = ["web_fetch"])
+ body = bodies[0]
+ assert body.get("tool_choice") == expected
+ if choice == "none":
+ assert "tools" not in body
+ else:
+ assert body["tools"][:2] == [
+ {
+ "name": tool["function"]["name"],
+ "description": tool["function"]["description"],
+ "input_schema": tool["function"]["parameters"],
+ }
+ for tool in TOOLS
+ ]
+ assert any(tool["name"] == "web_fetch" for tool in body["tools"]) is not isinstance(
+ choice, dict
+ )
+
+
+def test_manual_thinking_does_not_override_a_forced_tool(monkeypatch):
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(
+ client,
+ tools = TOOLS,
+ tool_choice = "required",
+ model = "claude-haiku-4-5-20251001",
+ enable_thinking = True,
+ )
+ assert "thinking" not in bodies[0]
+ assert bodies[0]["tool_choice"] == {"type": "any"}
+
+
[email protected]("max_calls", [2, 4])
[email protected]("pending_fetch", [False, True])
[email protected]("repeat_calls", [False, True])
+def test_loop_executes_fragmented_calls_and_replays_signed_and_hosted_blocks(
+ monkeypatch, max_calls, pending_fetch, repeat_calls
+):
+ thinking = {"type": "thinking", "thinking": "Choose tools.", "signature": "signed-thinking"}
+ redacted = {"type": "redacted_thinking", "data": "opaque"}
+ hosted = {
+ "type": "server_tool_use",
+ "id": "srv_1",
+ "name": "web_fetch",
+ "input": {"url": "https://example.com"},
+ }
+ hosted_result = {
+ "type": "web_fetch_tool_result",
+ "tool_use_id": "srv_1",
+ "content": {"type": "web_fetch_tool_error", "error_code": "unavailable"},
+ }
+ events = [
+ *_block(
+ 0,
+ {"type": "thinking", "thinking": "", "signature": ""},
+ {"type": "thinking_delta", "thinking": thinking["thinking"]},
+ {"type": "signature_delta", "signature": "signed-"},
+ {"type": "signature_delta", "signature": "thinking"},
+ ),
+ *_block(1, redacted),
+ *_block(
+ 2,
+ {**hosted, "input": {}},
+ {"type": "input_json_delta", "partial_json": json.dumps(hosted["input"])},
+ ),
+ *([] if pending_fetch else _block(3, hosted_result)),
+ *_block(
+ 4,
+ {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {}},
+ {"type": "input_json_delta", "partial_json": '{"query":'},
+ {"type": "input_json_delta", "partial_json": '"one"}'},
+ ),
+ *_block(
+ 5,
+ {"type": "tool_use", "id": "toolu_b", "name": "web_search", "input": {}},
+ {"type": "input_json_delta", "partial_json": '{"query":"two"}'},
+ ),
+ *_finish("tool_use"),
+ ]
+ bodies, executed = [], []
+
+ def handler(request):
+ bodies.append(json.loads(request.content))
+ if len(bodies) == 1 or (repeat_calls and len(bodies) == 2 and max_calls > 2):
+ return _response(events)
+ return _response(
+ [
+ *_block(
+ 0, {"type": "text", "text": ""}, {"type": "text_delta", "text": "Finished."}
+ ),
+ *_finish("end_turn"),
+ ]
+ )
+
+ def execute(name, arguments, **kwargs):
+ executed.append((name, arguments))
+ return f"result:{arguments['query']}"
+
+ monkeypatch.setattr(loop, "execute_tool", execute)
+ monkeypatch.setattr(loop, "build_rag_autoinject", lambda *a, **k: None)
+ client = _client(monkeypatch, handler)
+
+ async def run():
+ try:
+ return [
+ line
+ async for line in loop.stream_with_studio_tools(
+ OAICompatTransport(
+ client,
+ model = "claude-sonnet-4-6",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ enable_thinking = True,
+ enable_prompt_caching = False,
+ enabled_tools = ["web_fetch"],
+ ),
+ run = loop.ToolLoopRun(messages = [{"role": "user", "content": "Use both tools"}]),
+ policy = loop.ToolLoopPolicy(
+ tools = TOOLS,
+ max_calls = max_calls,
+ timeout = 30,
+ permission_mode = "off",
+ confirm_calls = False,
+ bypass_permissions = False,
+ rag_scope = None,
+ auto_heal = False,
+ nudge_tool_calls = False,
+ ),
+ cancel_event = threading.Event(),
+ )
+ ]
+ finally:
+ await client.close()
+
+ lines = asyncio.run(run())
+ assert executed == [("python", {"query": "one"}), ("web_search", {"query": "two"})]
+ assert len(bodies) == (3 if repeat_calls and max_calls > 2 else 2)
+ assistant, results = bodies[1]["messages"][1:3]
+ assert assistant["role"] == "assistant"
+ native_prefix = [thinking, redacted, hosted] + ([] if pending_fetch else [hosted_result])
+ assert assistant["content"][: len(native_prefix)] == native_prefix
+ assert assistant["content"][len(native_prefix) :] == [
+ {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {"query": "one"}},
+ {"type": "tool_use", "id": "toolu_b", "name": "web_search", "input": {"query": "two"}},
+ ]
+ assert results == {
+ "role": "user",
+ "content": [
+ {"type": "tool_result", "tool_use_id": "toolu_a", "content": "result:one"},
+ {"type": "tool_result", "tool_use_id": "toolu_b", "content": "result:two"},
+ ],
+ }
+ assert any("Finished." in line for line in lines)
+ assert not any('"error"' in line for line in lines)
+ assert len(bodies[1]["messages"]) == 3
+ if max_calls == 2:
+ assert {"python", "web_search"} <= {tool["name"] for tool in bodies[1]["tools"]}
+ assert bodies[1]["tool_choice"] == {"type": "none"}
+ if pending_fetch:
+ assert any(tool["name"] == "web_fetch" for tool in bodies[1]["tools"])
+ if len(bodies) == 3:
+ noop_assistant, noop_results = bodies[-1]["messages"][-2:]
+ assert noop_assistant["role"] == "assistant"
+ assert [block["type"] for block in noop_results["content"]] == [
+ "tool_result",
+ "tool_result",
+ ]
+ assert (
+ len([block for block in noop_assistant["content"] if block["type"] == "tool_use"]) == 2
+ )
+
+
+def test_tool_result_images_survive_translation(monkeypatch):
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(
+ client,
+ tools = TOOLS,
+ messages = [
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "toolu_a",
+ "type": "function",
+ "function": {"name": "python", "arguments": "{}"},
+ }
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "toolu_a",
+ "content": [
+ {"type": "text", "text": "Image"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,cGlj"}},
+ ],
+ },
+ ],
+ )
+ assert bodies[0]["messages"][-1]["content"][0]["content"] == [
+ {"type": "text", "text": "Image"},
+ {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "cGlj"}},
+ ]
+
+
+ "ending, expected_calls",
+ [("tool_use", 1), ("max_tokens", 0), ("refusal", 0), ("error", 0), ("eof", 0)],
+)
+def test_incomplete_turns_never_execute_client_calls(monkeypatch, ending, expected_calls):
+ executed, bodies = [], []
+ events = [
+ {
+ "type": "content_block_start",
+ "index": 0,
+ "content_block": {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {}},
+ }
+ ]
+ if ending == "error":
+ events.append(
+ {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
+ )
+ elif ending != "eof":
+ events.extend([{"type": "content_block_stop", "index": 0}, *_finish(ending)])
+
+ def handler(request):
+ bodies.append(json.loads(request.content))
+ return _response(events if len(bodies) == 1 else _finish("end_turn"))
+
+ def execute(name, arguments, **kwargs):
+ executed.append((name, arguments))
+ return "done"
+
+ monkeypatch.setattr(loop, "execute_tool", execute)
+ monkeypatch.setattr(loop, "build_rag_autoinject", lambda *a, **k: None)
+ client = _client(monkeypatch, handler)
+
+ async def run():
+ try:
+ return [
+ line
+ async for line in loop.stream_with_studio_tools(
+ OAICompatTransport(
+ client,
+ model = "claude-sonnet-4-6",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ ),
+ run = loop.ToolLoopRun(messages = [{"role": "user", "content": "Use tool"}]),
+ policy = loop.ToolLoopPolicy(
+ tools = TOOLS,
+ max_calls = 1,
+ timeout = 30,
+ permission_mode = "off",
+ confirm_calls = False,
+ bypass_permissions = False,
+ rag_scope = None,
+ auto_heal = False,
+ nudge_tool_calls = False,
+ ),
+ cancel_event = threading.Event(),
+ )
+ ]
+ finally:
+ await client.close()
+
+ asyncio.run(run())
+ assert len(executed) == expected_calls
+ assert len(bodies) == 1 + expected_calls
+
+
+def test_native_compaction_delta_is_replayed_with_encrypted_content(monkeypatch):
+ bodies = []
+ summary = {"content": "Earlier conversation summary", "encrypted_content": "opaque-compaction"}
+ events = [
+ *_block(
+ 0, {"type": "compaction", "content": None}, {"type": "compaction_delta", **summary}
+ ),
+ *_finish("tool_use"),
+ ]
+ client = _client(
+ monkeypatch,
+ lambda request: (bodies.append(json.loads(request.content)) or _response(events)),
+ )
+ lines = _collect(client, tools = TOOLS)
+ deltas = [
+ json.loads(line[6:])["choices"][0]["delta"]
+ for line in lines
+ if line.startswith("data: {") and json.loads(line[6:]).get("choices")
+ ]
+ extra = next(delta["extra_content"] for delta in deltas if "extra_content" in delta)
+ assert extra["anthropic"]["content"] == [{"type": "compaction", **summary}]
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(
+ client, tools = TOOLS, messages = [{"role": "assistant", "content": "", "extra_content": extra}]
+ )
+ assert bodies[-1]["messages"][0]["content"] == [{"type": "compaction", **summary}]
+
+
+def test_truncated_hosted_json_still_reports_length(monkeypatch):
+ events = [
+ *_block(
+ 0,
+ {"type": "server_tool_use", "id": "srv_1", "name": "web_fetch", "input": {}},
+ {"type": "input_json_delta", "partial_json": '{"url":"https://exa'},
+ ),
+ *_finish("max_tokens"),
+ ]
+ client = _client(monkeypatch, lambda request: _response(events))
+ lines = _collect(client, tools = TOOLS, enabled_tools = ["web_fetch"])
+ assert any('"finish_reason": "length"' in line for line in lines)
+ assert lines[-1] == "data: [DONE]"
+
+
+def test_detached_mcp_images_join_tool_result_only_on_followup(monkeypatch):
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ messages = [
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "toolu_a",
+ "type": "function",
+ "function": {"name": "python", "arguments": "{}"},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": "toolu_a", "content": "result"},
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Tool image"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,cGlj"}},
+ ],
+ },
+ ]
+ original = json.dumps(messages)
+ initial = [{"role": "user", "content": "Use the tools"}]
+ third_turn = [
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "toolu_b",
+ "type": "function",
+ "function": {"name": "python", "arguments": "{}"},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": "toolu_b", "content": "second result"},
+ ]
+
+ async def run():
+ transport = OAICompatTransport(
+ client,
+ model = "claude-sonnet-4-6",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ enable_prompt_caching = False,
+ )
+ try:
+ for history in (initial, initial + messages, initial + messages + third_turn):
+ async for _ in transport.stream(
+ messages = history,
+ tools = TOOLS,
+ tool_choice = "auto",
+ cancel_event = threading.Event(),
+ ):
+ pass
+ finally:
+ await client.close()
+
+ asyncio.run(run())
+ assert len(bodies[0]["messages"]) == 1
+ assert len(bodies[1]["messages"]) == 3
+ assert len(bodies[2]["messages"]) == 5
+ assert bodies[2]["messages"][:3] == bodies[1]["messages"]
+ result = bodies[1]["messages"][-1]["content"][0]
+ assert result["type"] == "tool_result"
+ assert result["content"][-1] == {
+ "type": "image",
+ "source": {"type": "base64", "media_type": "image/png", "data": "cGlj"},
+ }
+ assert "Tool image" in result["content"][0]["text"]
+ assert json.dumps(messages) == original
+
+
[email protected]("name", ["web_search", "web_fetch", "code_execution"])
+def test_caller_names_take_precedence_over_automatic_hosted_tools(monkeypatch, name):
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ tool = {
+ "type": "function",
+ "function": {"name": name, "parameters": {"type": "object", "properties": {}}},
+ }
+ _collect(client, tools = [tool], enabled_tools = [name])
+ assert bodies[0]["tools"] == [{"name": name, "input_schema": tool["function"]["parameters"]}]
+
+
+ "content", ["Rendered reply", None, [{"type": "text", "text": "Rendered reply"}], []]
+)
[email protected]("vision", [True, False])
+def test_client_continuation_replays_native_anthropic_state(monkeypatch, content, vision):
+ from models.inference import ChatMessage
+ from routes.inference import _build_external_messages
+
+ native = [
+ {"type": "thinking", "thinking": "Choose tools", "signature": "signed"},
+ {"type": "redacted_thinking", "data": "opaque"},
+ {
+ "type": "server_tool_use",
+ "id": "srv_1",
+ "name": "web_fetch",
+ "input": {"url": "https://example.com"},
+ },
+ ]
+ calls = [
+ {"id": "toolu_1", "type": "function", "function": {"name": "get_status", "arguments": "{}"}}
+ ]
+ messages = [
+ ChatMessage(role = "user", content = "Fetch the page and get status"),
+ ChatMessage(
+ role = "assistant",
+ content = content,
+ tool_calls = calls,
+ extra_content = {
+ "anthropic": {"content": native, "unrelated": "omit"},
+ "google": {"thought_signature": "foreign"},
+ "openai_codex": {"items": []},
+ },
+ ),
+ ChatMessage(role = "tool", tool_call_id = "toolu_1", content = "READY"),
+ ]
+ normalized = _build_external_messages(messages, vision, provider_type = "anthropic")
+ assert normalized[1]["extra_content"] == {"anthropic": {"content": native}}
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(
+ client,
+ messages = normalized,
+ tools = [{"type": "function", "function": {"name": "get_status"}}],
+ enabled_tools = [],
+ tool_choice = "none",
+ )
+ assert bodies[0]["messages"][1]["content"] == native + [
+ {"type": "tool_use", "id": "toolu_1", "name": "get_status", "input": {}}
+ ]
+ assert bodies[0]["messages"][2]["content"] == [
+ {"type": "tool_result", "tool_use_id": "toolu_1", "content": "READY"}
+ ]
+ assert [tool["name"] for tool in bodies[0]["tools"]] == ["get_status", "web_fetch"]
+ assert bodies[0]["tool_choice"] == {"type": "none"}
+
+
+def test_anthropic_does_not_forward_foreign_message_metadata(monkeypatch):
+ from models.inference import ChatMessage
+ from routes.inference import _build_external_messages
+
+ messages = _build_external_messages(
+ [
+ ChatMessage(role = "user", content = "Hello"),
+ ChatMessage(
+ role = "assistant",
+ content = "Previous reply",
+ extra_content = {
+ "google": {"thought_signature": "foreign"},
+ "openai_codex": {"items": []},
+ },
+ ),
+ ChatMessage(role = "user", content = "Continue"),
+ ],
+ True,
+ provider_type = "anthropic",
+ )
+ bodies = []
+ client = _client(
+ monkeypatch,
+ lambda request: (
+ bodies.append(json.loads(request.content)) or _response(_finish("end_turn"))
+ ),
+ )
+ _collect(client, messages = messages)
+ assert bodies[0]["messages"][1] == {"role": "assistant", "content": "Previous reply"}
+
+
+def test_whitespace_text_blocks_are_not_replayed(monkeypatch):
+ # Anthropic 400s "text content blocks must contain non-whitespace text" on a replayed blank block.
+ events = [
+ *_block(0, {"type": "text", "text": ""}, {"type": "text_delta", "text": "\n\n"}),
+ *_block(
+ 1,
+ {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {}},
+ {"type": "input_json_delta", "partial_json": '{"query":"one"}'},
+ ),
+ *_finish("tool_use"),
+ ]
+ bodies = []
+
+ def handler(request):
+ bodies.append(json.loads(request.content))
+ if len(bodies) == 1:
+ return _response(events)
+ return _response(
+ [
+ *_block(0, {"type": "text", "text": ""}, {"type": "text_delta", "text": "Done."}),
+ *_finish("end_turn"),
+ ]
+ )
+
+ monkeypatch.setattr(loop, "execute_tool", lambda name, arguments, **kwargs: "ok")
+ monkeypatch.setattr(loop, "build_rag_autoinject", lambda *a, **k: None)
+ client = _client(monkeypatch, handler)
+
+ async def run():
+ try:
+ return [
+ line
+ async for line in loop.stream_with_studio_tools(
+ OAICompatTransport(
+ client,
+ model = "claude-sonnet-4-6",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ enable_prompt_caching = False,
+ ),
+ run = loop.ToolLoopRun(messages = [{"role": "user", "content": "Run it"}]),
+ policy = loop.ToolLoopPolicy(
+ tools = TOOLS,
+ max_calls = 5,
+ timeout = 30,
+ permission_mode = "off",
+ confirm_calls = False,
+ bypass_permissions = False,
+ rag_scope = None,
+ auto_heal = False,
+ nudge_tool_calls = False,
+ ),
+ cancel_event = threading.Event(),
+ )
+ ]
+ finally:
+ await client.close()
+
+ asyncio.run(run())
+ assert len(bodies) == 2
+ assistant = bodies[1]["messages"][1]
+ assert assistant["content"] == [
+ {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {"query": "one"}}
+ ]
+
+
+def test_client_calls_keep_their_position_in_the_replayed_turn(monkeypatch):
+ client_call = {"type": "tool_use", "id": "toolu_a", "name": "python", "input": {"query": "one"}}
+ hosted = {
+ "type": "server_tool_use",
+ "id": "srv_1",
+ "name": "web_fetch",
+ "input": {"url": "https://example.com"},
+ }
+ events = [
+ *_block(
+ 0,
+ {**client_call, "input": {}},
+ {"type": "input_json_delta", "partial_json": '{"query":"one"}'},
+ ),
+ *_block(
+ 1,
+ {**hosted, "input": {}},
+ {"type": "input_json_delta", "partial_json": json.dumps(hosted["input"])},
+ ),
+ *_block(2, {"type": "text", "text": ""}, {"type": "text_delta", "text": "Working."}),
+ *_finish("tool_use"),
+ ]
+ bodies = []
+
+ def handler(request):
+ bodies.append(json.loads(request.content))
+ if len(bodies) == 1:
+ return _response(events)
+ return _response(
+ [
+ *_block(0, {"type": "text", "text": ""}, {"type": "text_delta", "text": "Done."}),
+ *_finish("end_turn"),
+ ]
+ )
+
+ monkeypatch.setattr(loop, "execute_tool", lambda name, arguments, **kwargs: "ok")
+ monkeypatch.setattr(loop, "build_rag_autoinject", lambda *a, **k: None)
+ client = _client(monkeypatch, handler)
+
+ async def run():
+ try:
+ return [
+ line
+ async for line in loop.stream_with_studio_tools(
+ OAICompatTransport(
+ client,
+ model = "claude-sonnet-4-6",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 4096,
+ enable_prompt_caching = False,
+ enabled_tools = ["web_fetch"],
+ ),
+ run = loop.ToolLoopRun(messages = [{"role": "user", "content": "Run it"}]),
+ policy = loop.ToolLoopPolicy(
+ tools = TOOLS,
+ max_calls = 5,
+ timeout = 30,
+ permission_mode = "off",
+ confirm_calls = False,
+ bypass_permissions = False,
+ rag_scope = None,
+ auto_heal = False,
+ nudge_tool_calls = False,
+ ),
+ cancel_event = threading.Event(),
+ )
+ ]
+ finally:
+ await client.close()
+
+ asyncio.run(run())
+ assert len(bodies) == 2
+ assert bodies[1]["messages"][1]["content"] == [
+ client_call,
+ hosted,
+ {"type": "text", "text": "Working."},
+ ]
diff --git a/studio/backend/tests/test_external_tools_compat.py b/studio/backend/tests/test_external_tools_compat.py
index c0ab4879702..844e152d376 100644
--- a/studio/backend/tests/test_external_tools_compat.py
+++ b/studio/backend/tests/test_external_tools_compat.py
@@ -239,13 +239,11 @@ def test_registry_entry_schema_tolerates_a_pre_change_payload():
# ── capability allowlist ─────────────────────────────────────────────
-def test_anthropic_is_not_studio_tools_capable():
- """``_stream_anthropic`` never forwards caller function-tool schemas.
-
- Advertising the capability would hand the loop a catalog the model never
- sees, so every turn would look like a model that declined to call a tool.
- """
- assert provider_runs_local_tools("anthropic") is False
+def test_anthropic_is_studio_tools_capable():
+ assert provider_runs_local_tools("anthropic") is True
+ entry = next(row for row in list_available_providers() if row["provider_type"] == "anthropic")
+ assert entry["supports_studio_tools"] is True
+ assert entry["supports_tool_calling"] is True
def test_openai_codex_keeps_the_capability_it_already_had():