Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
"""Tests for Anthropic Messages API schemas and translation layer (no server/GPU)."""
import contextlib
import sys
import os
import json
import threading
import time
import httpx
import pytest
_backend = os.path.join(os.path.dirname(__file__), "..")
sys.path.insert(0, _backend)
from models.inference import (
AnthropicMessagesRequest,
AnthropicMessagesResponse,
AnthropicMessage,
AnthropicTextBlock,
AnthropicToolUseBlock,
AnthropicToolResultBlock,
AnthropicTool,
AnthropicUsage,
AnthropicResponseTextBlock,
AnthropicResponseToolUseBlock,
)
from core.inference.anthropic_compat import (
DOCUMENT_IMAGE_OMITTED,
DOCUMENT_OMITTED,
anthropic_messages_to_openai,
anthropic_schema_client_tool_kind,
anthropic_tools_to_openai,
build_anthropic_sse_event,
AnthropicStreamEmitter,
AnthropicPassthroughEmitter,
)
from core.inference.api_monitor import ApiMonitor
from routes.inference import (
_build_tool_action_nudge,
_normalize_anthropic_openai_images,
_select_anthropic_server_tools,
_anthropic_requested_studio_tools,
_anthropic_passthrough_stream,
_anthropic_plain_non_streaming,
_anthropic_tool_non_streaming,
_monitor_anthropic_sse_line,
anthropic_messages,
)
from state.tool_policy import reset_tool_policy, set_tool_policy
from fastapi import HTTPException
import asyncio
import base64 as _b64
from io import BytesIO as _BytesIO
from types import SimpleNamespace
def _chunk(finish_reason = None, **delta):
"""One OpenAI streaming chunk carrying ``delta`` on its single choice."""
choice = {"delta": delta}
if finish_reason is not None:
choice["finish_reason"] = finish_reason
return {"choices": [choice]}
def _emitter_client_text(events: list[str]) -> str:
"""Concatenate the text_delta payloads an SSE event list carries."""
text = ""
for line in events:
for raw in line.split("\n"):
raw = raw.strip()
if not raw.startswith("data: "):
continue
data = json.loads(raw[len("data: ") :])
delta = data.get("delta", {})
if delta.get("type") == "text_delta":
text += delta.get("text", "")
return text
def _emitter_client_thinking(events):
thinking = ""
for line in events:
for raw in line.split("\n"):
raw = raw.strip()
if not raw.startswith("data: "):
continue
data = json.loads(raw[len("data: ") :])
delta = data.get("delta", {})
if delta.get("type") == "thinking_delta":
thinking += delta.get("thinking", "")
return thinking
def _tool_event(**overrides):
"""A studio tool-loop event, with per-test overrides."""
return {
"type": "tool_start",
"tool_name": "python",
"tool_call_id": "call_0",
# Explicit: .get('arguments', {}) is the malformed-event fallback.
"arguments": {},
**overrides,
}
def _tool_result_event(**overrides):
"""A studio tool-loop result event, with per-test overrides."""
return {
"type": "tool_end",
"tool_name": "python",
"tool_call_id": "call_0",
"result": "done",
**overrides,
}
_WEB_SEARCH_RESULT = (
"Title: Releases · ggml-org/llama.cpp\n"
"URL: https://github.com/ggml-org/llama.cpp/releases\n"
"Snippet: b9999 (2026-09-28)"
"\n\n---\n\n"
"Title: llama.cpp - Wikipedia\n"
"URL: https://en.wikipedia.org/wiki/Llama.cpp\n"
"Snippet: llama.cpp is an open source library."
"\n\n---\n\nIMPORTANT: These are only short snippets."
)
_WEB_SEARCH_HITS = [
{
"type": "web_search_result",
"title": "Releases · ggml-org/llama.cpp",
"url": "https://github.com/ggml-org/llama.cpp/releases",
"encrypted_content": "",
"page_age": None,
},
{
"type": "web_search_result",
"title": "llama.cpp - Wikipedia",
"url": "https://en.wikipedia.org/wiki/Llama.cpp",
"encrypted_content": "",
"page_age": None,
},
]
_WEB_SEARCH_ERROR = {"type": "web_search_tool_result_error", "error_code": "unavailable"}
_FETCHED_URL = "https://github.com/ggml-org/llama.cpp"
_WEB_SEARCH_OUTCOMES = [
({"query": "llama.cpp"}, "No results found.", []),
(
{"query": "llama.cpp"},
"Search failed: the search engines did not respond.",
_WEB_SEARCH_ERROR,
),
(
{"url": _FETCHED_URL},
"# llama.cpp\nTitle: quoted page text\nURL: https://example.com",
[{**_WEB_SEARCH_HITS[0], "title": _FETCHED_URL, "url": _FETCHED_URL}],
),
({"url": _FETCHED_URL}, "Failed to fetch URL: HTTP 404 Not Found", _WEB_SEARCH_ERROR),
]
def _tool_result_turn(
*,
role = "user",
type = "tool_result",
tool_use_id = "t1",
content = "42",
):
"""A turn whose content is one tool_result part, with per-test overrides."""
return {
"role": role,
"content": [{"type": type, "tool_use_id": tool_use_id, "content": content}],
}
def test_anthropic_emitter_reasoning_only_becomes_thinking_block():
emitter = AnthropicStreamEmitter()
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "<think>The capital"})
events += emitter.feed({"type": "content", "text": "<think>The capital of France is Paris."})
events += emitter.feed(
{"type": "content", "text": "<think>The capital of France is Paris.</think>"}
)
events += emitter.finish()
assert _emitter_client_thinking(events) == "The capital of France is Paris."
assert _emitter_client_text(events) == ""
thinking_start = next(e for e in events if '"type": "thinking"' in e)
# Strict stream decoders reject a thinking block start without signature.
assert '"signature": ""' in thinking_start
def test_anthropic_emitter_splits_think_from_answer():
emitter = AnthropicStreamEmitter()
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "<think>Thinking."})
events += emitter.feed({"type": "content", "text": "<think>Thinking.</think>Answer."})
events += emitter.finish()
assert _emitter_client_thinking(events) == "Thinking."
assert _emitter_client_text(events) == "Answer."
def test_anthropic_emitter_parse_think_off_keeps_literal_tags():
emitter = AnthropicStreamEmitter(parse_think = False)
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "Use <think>like this</think> tags."})
events += emitter.finish()
assert _emitter_client_thinking(events) == ""
assert _emitter_client_text(events) == "Use <think>like this</think> tags."
def test_think_parsing_expected_gates_on_capability_and_request():
from routes.inference import _think_parsing_expected
class _Backend:
def __init__(
self,
supports = True,
always_on = False,
default = True,
):
self.supports_reasoning = supports
self.reasoning_always_on = always_on
self.reasoning_default = default
assert _think_parsing_expected(_Backend(supports = False), _basic_payload()) is False
assert (
_think_parsing_expected(_Backend(supports = False, always_on = True), _basic_payload()) is True
)
assert _think_parsing_expected(_Backend(), _basic_payload(enable_thinking = False)) is False
assert _think_parsing_expected(_Backend(), _basic_payload(reasoning_effort = "none")) is False
assert _think_parsing_expected(_Backend(default = False), _basic_payload()) is False
assert _think_parsing_expected(_Backend(), _basic_payload()) is True
assert _think_parsing_expected(_Backend(), _basic_payload(enable_thinking = True)) is True
# Effort-dial templates map False to low effort; only 'none' disables parsing.
class _EffortBackend(_Backend):
def _request_reasoning_kwargs(self, enable_thinking, reasoning_effort, preserve_thinking):
if reasoning_effort == "none":
return {"reasoning_effort": "none"}
return {"reasoning_effort": "low" if enable_thinking is False else "high"}
assert _think_parsing_expected(_EffortBackend(), _basic_payload(enable_thinking = False)) is True
assert (
_think_parsing_expected(_EffortBackend(), _basic_payload(reasoning_effort = "none")) is False
)
# _coerce_reasoning_effort rewrites 'none' to 0, so 0 reads as off.
class _NumericEffortBackend(_Backend):
def _request_reasoning_kwargs(self, enable_thinking, reasoning_effort, preserve_thinking):
return {"reasoning_effort": 0.0 if reasoning_effort == "none" else 0.2}
assert (
_think_parsing_expected(_NumericEffortBackend(), _basic_payload(reasoning_effort = "none"))
is False
)
assert (
_think_parsing_expected(_NumericEffortBackend(), _basic_payload(enable_thinking = False))
is True
)
def test_anthropic_reasoning_args_maps_effort_only_to_enable_thinking():
from routes.inference import _anthropic_reasoning_args
assert (
_anthropic_reasoning_args(_basic_payload(reasoning_effort = "none"))["enable_thinking"]
is False
)
assert (
_anthropic_reasoning_args(_basic_payload(reasoning_effort = "high"))["enable_thinking"]
is True
)
assert _anthropic_reasoning_args(_basic_payload())["enable_thinking"] is None
assert _anthropic_reasoning_args(
_basic_payload(enable_thinking = True, reasoning_effort = "none")
) == {
"enable_thinking": True,
"reasoning_effort": None,
"preserve_thinking": None,
}
assert _anthropic_reasoning_args(
_basic_payload(enable_thinking = False, reasoning_effort = "high")
) == {
"enable_thinking": False,
"reasoning_effort": None,
"preserve_thinking": None,
}
# reasoning_effort must win over `thinking`; pinned as a full cross product.
_THINKING_EFFORT_MATRIX = [
(None, None, None),
(None, "none", False),
(None, "low", True),
(None, "high", True),
("enabled", None, True),
("enabled", "none", False),
("enabled", "low", True),
("enabled", "high", True),
("disabled", None, False),
("disabled", "none", False),
("disabled", "low", True),
("disabled", "high", True),
]
@pytest.mark.parametrize("thinking_type, effort, expected", _THINKING_EFFORT_MATRIX)
def test_reasoning_effort_outranks_native_thinking(thinking_type, effort, expected):
from routes.inference import _anthropic_reasoning_args
fields = {}
if thinking_type is not None:
fields["thinking"] = {"type": thinking_type}
if effort is not None:
fields["reasoning_effort"] = effort
args = _anthropic_reasoning_args(_basic_payload(**fields))
assert args["enable_thinking"] is expected
assert args["reasoning_effort"] == effort
# preserve_thinking None keeps llama-server's load-time kwargs.
assert args["preserve_thinking"] is None
@pytest.mark.parametrize("thinking_type, effort, expected", _THINKING_EFFORT_MATRIX)
def test_thinking_effort_matrix_reaches_enable_thinking_templates(thinking_type, effort, expected):
"""enable_thinking templates read only the boolean, so the effort dial cannot reach them."""
from routes.inference import _anthropic_reasoning_args, _reasoning_template_kwargs
class _QwenStyleBackend:
"""Mirrors LlamaCppBackend._request_reasoning_kwargs for reasoning_style
'enable_thinking': the boolean is the only dial, effort is ignored."""
def _request_reasoning_kwargs(self, enable_thinking, reasoning_effort, preserve_thinking):
kwargs = {}
if enable_thinking is not None:
kwargs["enable_thinking"] = enable_thinking
if preserve_thinking is not None:
kwargs["preserve_thinking"] = preserve_thinking
return kwargs or None
fields = {}
if thinking_type is not None:
fields["thinking"] = {"type": thinking_type}
if effort is not None:
fields["reasoning_effort"] = effort
args = _anthropic_reasoning_args(_basic_payload(**fields))
resolved = _reasoning_template_kwargs(
_QwenStyleBackend(),
args["enable_thinking"],
args["reasoning_effort"],
args["preserve_thinking"],
)
if expected is None:
assert resolved is None
else:
assert resolved == {"enable_thinking": expected}
def test_x_unsloth_enable_thinking_still_outranks_effort_and_thinking():
from routes.inference import _anthropic_reasoning_args
args = _anthropic_reasoning_args(
_basic_payload(
thinking = {"type": "disabled"},
enable_thinking = True,
reasoning_effort = "none",
)
)
assert args["enable_thinking"] is True
args = _anthropic_reasoning_args(
_basic_payload(
thinking = {"type": "enabled"},
enable_thinking = False,
reasoning_effort = "high",
)
)
assert args["enable_thinking"] is False
def test_replayed_thinking_preserved_only_when_requested():
from core.inference.anthropic_compat import anthropic_messages_to_openai
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "prior trace", "signature": ""},
{"type": "text", "text": "the answer"},
],
},
{"role": "user", "content": "and then?"},
]
dropped = anthropic_messages_to_openai(messages)
assert "reasoning_content" not in dropped[1]
kept = anthropic_messages_to_openai(messages, preserve_thinking = True)
assert kept[1]["reasoning_content"] == "prior trace"
assert kept[1]["content"] == "the answer"
def test_the_current_turns_thinking_survives_without_preservation():
# Templates render reasoning after the last real query, so an agent's tool rounds keep theirs;
# dropping it made every round re-read the previous one's reasoning.
from core.inference.anthropic_compat import anthropic_messages_to_openai
def _round(trace, call_id):
return {
"role": "assistant",
"content": [
{"type": "thinking", "thinking": trace, "signature": ""},
{"type": "tool_use", "id": call_id, "name": "bash", "input": {"cmd": "ls"}},
],
}
def _result(call_id):
return {
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": call_id, "content": "ok"}],
}
messages = [
{"role": "user", "content": "first task"},
_round("old trace", "t0"),
_result("t0"),
{"role": "assistant", "content": [{"type": "text", "text": "done"}]},
{"role": "user", "content": "second task"},
_round("round one", "t1"),
_result("t1"),
_round("round two", "t2"),
_result("t2"),
]
out = anthropic_messages_to_openai(messages)
traces = [m.get("reasoning_content") for m in out if m.get("role") == "assistant"]
assert traces == [None, None, "round one", "round two"]
def test_anthropic_emitter_only_leading_think_is_reasoning():
emitter = AnthropicStreamEmitter()
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "<think>why</think>Use "})
events += emitter.feed(
{"type": "content", "text": "<think>why</think>Use <think>like this</think> tags."}
)
events += emitter.finish()
assert _emitter_client_thinking(events) == "why"
assert _emitter_client_text(events) == "Use <think>like this</think> tags."
def test_anthropic_emitter_no_leading_think_keeps_all_tags_literal():
emitter = AnthropicStreamEmitter()
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "Wrap it in <think>"})
events += emitter.feed({"type": "content", "text": "Wrap it in <think>...</think> please."})
events += emitter.finish()
assert _emitter_client_thinking(events) == ""
assert _emitter_client_text(events) == "Wrap it in <think>...</think> please."
def test_split_think_segments_only_parses_leading_block():
from routes.inference import _split_think_segments
assert _split_think_segments("<think>why</think>Use <think>x</think> tags.") == [
("thinking", "why"),
("text", "Use <think>x</think> tags."),
]
assert _split_think_segments("Use <think>x</think> tags.") == [
("text", "Use <think>x</think> tags.")
]
def test_anthropic_emitter_keeps_embedded_close_tag_in_trace():
# Generator-recorded length keeps a trace quoting </think> intact.
trace = "the tag </think> ends a block"
prov = {"wrapped": 1, "wraps": [{"len": 0}]}
emitter = AnthropicStreamEmitter(think_provenance = prov)
events = emitter.start("msg_1", "m")
prov["wraps"][0]["len"] = len("the tag </think>")
events += emitter.feed({"type": "content", "text": "<think>the tag </think>"})
prov["wraps"][0]["len"] = len(trace)
events += emitter.feed({"type": "content", "text": f"<think>{trace}"})
events += emitter.feed({"type": "content", "text": f"<think>{trace}</think>Answer."})
events += emitter.finish()
assert _emitter_client_thinking(events) == trace
assert _emitter_client_text(events) == "Answer."
def test_split_think_segments_wrap_length_beats_embedded_tag():
from routes.inference import _split_think_segments
trace = "quote </think> inside"
text = f"<think>{trace}</think>Visible."
assert _split_think_segments(text, {"len": len(trace)}) == [
("thinking", trace),
("text", "Visible."),
]
assert _split_think_segments(text) == [
("thinking", "quote "),
("text", " inside</think>Visible."),
]
def test_anthropic_emitter_holds_back_partial_think_tag():
emitter = AnthropicStreamEmitter()
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": "<th"})
events += emitter.feed({"type": "content", "text": "<think>Deep"})
events += emitter.feed({"type": "content", "text": "<think>Deep</th"})
events += emitter.feed({"type": "content", "text": "<think>Deep</think>Out"})
events += emitter.finish()
assert _emitter_client_thinking(events) == "Deep"
assert _emitter_client_text(events) == "Out"
@pytest.mark.parametrize("block_type", ["tool_use", "server_tool_use"])
def test_streamed_anthropic_tool_use_records_api_monitor_reply(monkeypatch, block_type):
import routes.inference as inf_mod
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
monitor_id = monitor.start(
endpoint = "/v1/messages",
method = "POST",
model = "m",
prompt = "hi",
)
for payload in (
{
"type": "content_block_start",
"index": 0,
"content_block": {
"type": block_type,
"id": "toolu_1",
"name": "lookup",
"input": {},
},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {
"type": "input_json_delta",
"partial_json": '{"query":"weather"}',
},
},
{"type": "content_block_stop", "index": 0},
):
_monitor_anthropic_sse_line(monitor_id, f"data: {json.dumps(payload)}")
entry = monitor.get(monitor_id)
assert entry is not None
assert entry["reply"] == 'Tool call: lookup\nInput: {"query":"weather"}'
class TestToolActionNudge:
def test_balanced_nudge_uses_expanded_web_and_code_tips(self):
nudge = _build_tool_action_nudge(
tools = [
{"type": "function", "function": {"name": "web_search"}},
{"type": "function", "function": {"name": "python"}},
],
model_name = "Llama-3.1-70B-Instruct",
)
assert "The current date is " not in nudge
assert nudge.startswith("Tools are available when they materially improve")
assert "prefer using tools rather than answering from memory" not in nudge
assert "fetch its full content by calling web_search with the url parameter" in nudge
assert "Use code execution for math" in nudge
assert "render_html" not in nudge
def test_balanced_nudge_preserves_compact_web_tip_and_canvas_gate(self):
nudge = _build_tool_action_nudge(
tools = [
{"type": "function", "function": {"name": "web_search"}},
{"type": "function", "function": {"name": "render_html"}},
],
model_name = "Llama-3.1-8B-Instruct",
)
assert "When using web_search, do not repeat the same search query." in nudge
assert "fetch its full content" not in nudge
assert "call render_html once" in nudge
def test_balanced_nudge_empty_without_known_tool_categories(self):
assert _build_tool_action_nudge(tools = [], model_name = "Llama-3.1-8B-Instruct") == ""
def test_read_skill_only_nudge_does_not_advertise_create_skill(self, monkeypatch):
import routes.inference as inference_routes
monkeypatch.setattr(
inference_routes,
"_enabled_agent_skills",
lambda: [{"name": "guided", "description": "Guide this task."}],
)
nudge = _build_tool_action_nudge(
tools = [{"type": "function", "function": {"name": "read_skill"}}],
model_name = "test",
)
assert "- guided: Guide this task." in nudge
assert "create_skill" not in nudge
# without python or terminal, a skill's bundled scripts cannot run here.
assert "bundled scripts cannot run" in nudge
assert "Unsloth Studio's local Tools" in nudge
@pytest.mark.parametrize("code_tool", ["python", "terminal"])
def test_skill_nudge_with_a_code_tool_does_not_warn_about_scripts(self, monkeypatch, code_tool):
import routes.inference as inference_routes
monkeypatch.setattr(
inference_routes,
"_enabled_agent_skills",
lambda: [{"name": "guided", "description": "Guide this task."}],
)
nudge = _build_tool_action_nudge(
tools = [
{"type": "function", "function": {"name": "read_skill"}},
{"type": "function", "function": {"name": code_tool}},
],
model_name = "test",
)
assert "- guided: Guide this task." in nudge
assert "bundled scripts cannot run" not in nudge
class TestAnthropicModels:
def test_minimal_request(self):
req = AnthropicMessagesRequest(
messages = [{"role": "user", "content": "Hi"}],
)
assert req.max_tokens is None
assert req.model == "default"
assert req.stream is False
def test_max_tokens_optional(self):
req = AnthropicMessagesRequest(
max_tokens = 100,
messages = [{"role": "user", "content": "Hi"}],
)
assert req.max_tokens == 100
def test_system_as_string(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
messages = [{"role": "user", "content": "Hi"}],
system = "You are helpful.",
)
assert req.system == "You are helpful."
def test_system_role_message_normalized_to_system_field(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hi"},
],
)
assert req.system == "You are helpful."
assert len(req.messages) == 1
assert req.messages[0].role == "user"
def test_later_system_role_message_stays_in_the_user_turn_it_follows(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
system = "Base instructions.",
messages = [
{"role": "user", "content": "Hi"},
{"role": "system", "content": "Additional instructions."},
{"role": "assistant", "content": "Hello."},
],
)
assert req.system == "Base instructions."
assert [msg.role for msg in req.messages] == ["user", "assistant"]
assert [block.text for block in req.messages[0].content] == [
"Hi",
"<system-reminder>\nAdditional instructions.\n</system-reminder>",
]
def test_leading_system_role_message_still_joins_the_system_field(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
system = "Base instructions.",
messages = [
{"role": "system", "content": "Additional instructions."},
{"role": "user", "content": "Hi"},
],
)
assert req.system == "Base instructions.\n\nAdditional instructions."
assert req.messages[0].content == "Hi"
def test_system_role_message_after_an_assistant_turn_opens_the_next_user_turn(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
messages = [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello."},
{"role": "system", "content": "Note."},
{"role": "user", "content": [{"type": "text", "text": "Next."}]},
],
)
assert [msg.role for msg in req.messages] == ["user", "assistant", "user"]
assert [block.text for block in req.messages[2].content] == [
"<system-reminder>\nNote.\n</system-reminder>",
"Next.",
]
def test_system_role_message_after_a_trailing_prefill_keeps_the_prefill_last(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
messages = [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello."},
{"role": "system", "content": "Note."},
],
)
assert [msg.role for msg in req.messages] == ["user", "assistant"]
assert req.system == "Note."
@pytest.mark.parametrize(
"messages",
[
[{"role": "user", "content": 5}, {"role": "system", "content": "env"}],
[{"role": "user", "content": None}, {"role": "system", "content": "env"}],
[
{"role": "user", "content": "Hi"},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "t", "name": "R", "input": {}}],
},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "t", "content": {"a": 1}}],
},
{"role": "system", "content": "env"},
],
],
)
def test_malformed_turns_are_still_rejected_with_a_system_message_after_them(self, messages):
from pydantic import ValidationError
with pytest.raises(ValidationError):
AnthropicMessagesRequest(max_tokens = 50, messages = messages)
def test_claude_environment_message_leaves_system_and_tools_identical_across_sessions(self):
def converted(cwd):
req = AnthropicMessagesRequest(
max_tokens = 50,
system = "Static instructions.",
tools = [{"name": "Read", "input_schema": {"type": "object"}}],
messages = [
{"role": "user", "content": "Fix the bug."},
{
"role": "system",
"content": f"# Environment\n - Primary working directory: {cwd}",
},
],
).model_dump()
return anthropic_messages_to_openai(req["messages"], req["system"]), req["tools"]
first, first_tools = converted("/work/a")
second, second_tools = converted("/work/b")
assert first[0] == second[0] == {"role": "system", "content": "Static instructions."}
assert first_tools == second_tools
assert first[1]["content"].startswith("Fix the bug.\n<system-reminder>\n# Environment")
assert "/work/a" in first[1]["content"] and "/work/b" in second[1]["content"]
def test_per_round_system_reminders_only_append(self):
base = [
{"role": "user", "content": "Read it."},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "r1", "name": "Read", "input": {}}],
},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "r1", "content": "data"}],
},
{"role": "system", "content": "<total_tokens>900 tokens left</total_tokens>"},
]
later = [
*base,
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "r2", "name": "Read", "input": {}}],
},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "r2", "content": "more"}],
},
{"role": "system", "content": "<total_tokens>800 tokens left</total_tokens>"},
]
def converted(messages):
req = AnthropicMessagesRequest(
max_tokens = 50, system = "Base.", messages = messages
).model_dump()
return anthropic_messages_to_openai(req["messages"], req["system"])
before, after = converted(base), converted(later)
assert after[: len(before)] == before
assert after[0] == {"role": "system", "content": "Base."}
assert after[-1]["role"] == "tool"
assert "800 tokens left" in after[-1]["content"]
def test_a_reminder_after_a_tool_result_keeps_the_turn_and_its_thinking(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
messages = [
{"role": "user", "content": "Read it."},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "I should read it.", "signature": "s"},
{"type": "tool_use", "id": "r1", "name": "Read", "input": {}},
],
},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "r1", "content": "data"}],
},
{"role": "system", "content": "<total_tokens>900 tokens left</total_tokens>"},
],
).model_dump()
result = anthropic_messages_to_openai(req["messages"], req.get("system"))
assert [m["role"] for m in result] == ["user", "assistant", "tool"]
assert result[1].get("reasoning_content") == "I should read it."
assert result[2]["content"] == (
"<system-reminder>\n<total_tokens>900 tokens left</total_tokens>\n</system-reminder>\ndata"
)
def test_system_role_message_with_null_content_ignored(self):
req = AnthropicMessagesRequest(
max_tokens = 50,
system = "Base.",
messages = [
{"role": "system", "content": None},
{
"role": "system",
"content": [
None,
{"type": "text", "text": "Use short answers."},
],
},
{"role": "user", "content": "Hi"},
],
)
assert req.system == "Base.\n\nUse short answers."
assert "None" not in str(req.system)
assert [msg.role for msg in req.messages] == ["user"]
def test_tools_field_parses(self):
req = AnthropicMessagesRequest(
max_tokens = 100,
messages = [{"role": "user", "content": "Hi"}],
tools = [{"name": "web_search", "input_schema": {"type": "object"}}],
)
assert len(req.tools) == 1
assert req.tools[0].name == "web_search"
def test_server_tool_field_parses(self):
req = AnthropicMessagesRequest(
max_tokens = 100,
messages = [{"role": "user", "content": "Hi"}],
tools = [{"type": "web_fetch_20250910", "name": "web_fetch"}],
)
assert len(req.tools) == 1
assert req.tools[0].type == "web_fetch_20250910"
assert req.tools[0].name == "web_fetch"
assert req.tools[0].input_schema is None
def test_extra_fields_accepted(self):
req = AnthropicMessagesRequest(
max_tokens = 100,
messages = [{"role": "user", "content": "Hi"}],
some_future_field = "hello",
)
assert req.max_tokens == 100
def test_stream_defaults_false(self):
req = AnthropicMessagesRequest(
max_tokens = 100,
messages = [{"role": "user", "content": "Hi"}],
)
assert req.stream is False
def test_enable_tools_shorthand(self):
req = AnthropicMessagesRequest(
messages = [{"role": "user", "content": "Hi"}],
enable_tools = True,
enabled_tools = ["web_search", "python"],
session_id = "my-session",
)
assert req.enable_tools is True
assert req.enabled_tools == ["web_search", "python"]
assert req.session_id == "my-session"
def test_extension_fields_default_none(self):
req = AnthropicMessagesRequest(
messages = [{"role": "user", "content": "Hi"}],
)
assert req.enable_tools is None
assert req.enabled_tools is None
assert req.session_id is None
def test_response_model_defaults(self):
resp = AnthropicMessagesResponse()
assert resp.type == "message"
assert resp.role == "assistant"
assert resp.id.startswith("msg_")
assert resp.content == []
assert resp.usage.input_tokens == 0
class TestAnthropicMessagesToOpenAI:
def test_simple_user_message(self):
msgs = [{"role": "user", "content": "Hello"}]
result = anthropic_messages_to_openai(msgs)
assert result == [{"role": "user", "content": "Hello"}]
def test_system_string_prepended(self):
msgs = [{"role": "user", "content": "Hello"}]
result = anthropic_messages_to_openai(msgs, system = "Be brief.")
assert result[0] == {"role": "system", "content": "Be brief."}
assert result[1] == {"role": "user", "content": "Hello"}
def test_top_level_system_request_translates_unchanged(self):
req = AnthropicMessagesRequest(
messages = [{"role": "user", "content": "Hello"}],
system = "Be brief.",
)
result = anthropic_messages_to_openai(
[m.model_dump() for m in req.messages],
req.system,
)
assert result == [
{"role": "system", "content": "Be brief."},
{"role": "user", "content": "Hello"},
]
def test_system_as_block_list(self):
system = [
{"type": "text", "text": "Be brief."},
{"type": "text", "text": "Be accurate."},
]
msgs = [{"role": "user", "content": "Hello"}]
result = anthropic_messages_to_openai(msgs, system = system)
assert result[0]["role"] == "system"
assert "Be brief." in result[0]["content"]
assert "Be accurate." in result[0]["content"]
def test_multi_turn_conversation(self):
msgs = [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello!"},
{"role": "user", "content": "How are you?"},
]
result = anthropic_messages_to_openai(msgs)
assert len(result) == 3
assert result[0]["role"] == "user"
assert result[1]["role"] == "assistant"
assert result[2]["role"] == "user"
def test_assistant_tool_use_maps_to_tool_calls(self):
msgs = [
{
"role": "assistant",
"content": [
{"type": "text", "text": "Let me search."},
{
"type": "tool_use",
"id": "tu_1",
"name": "web_search",
"input": {"query": "test"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert len(result) == 1
m = result[0]
assert m["role"] == "assistant"
assert m["content"] == "Let me search."
assert len(m["tool_calls"]) == 1
tc = m["tool_calls"][0]
assert tc["id"] == "tu_1"
assert tc["function"]["name"] == "web_search"
assert json.loads(tc["function"]["arguments"]) == {"query": "test"}
def test_replayed_web_search_becomes_call_and_result(self):
call = {"type": "server_tool_use", "name": "web_search", "input": {"query": "llama.cpp"}}
msgs = [
{
"role": "assistant",
"content": [
{"type": "text", "text": "Searching."},
{**call, "id": "srvtoolu_fetch", "name": "web_fetch"},
{**call, "id": "srvtoolu_1"},
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_1",
"content": _WEB_SEARCH_HITS,
},
{"type": "text", "text": "It is b9999."},
{"type": "tool_use", "id": "tu_1", "name": "lookup", "input": {}},
],
}
]
search, result, answer = anthropic_messages_to_openai(msgs)
assert search["content"] == "Searching."
assert [tc["id"] for tc in search["tool_calls"]] == ["srvtoolu_1"]
assert result == {
"role": "tool",
"tool_call_id": "srvtoolu_1",
"content": (
"Title: Releases · ggml-org/llama.cpp\n"
"URL: https://github.com/ggml-org/llama.cpp/releases"
"\n\n---\n\n"
"Title: llama.cpp - Wikipedia\n"
"URL: https://en.wikipedia.org/wiki/Llama.cpp"
),
}
assert answer["content"] == "It is b9999."
assert [tc["id"] for tc in answer["tool_calls"]] == ["tu_1"]
@pytest.mark.parametrize(
"content, text",
[([], "No results found."), (_WEB_SEARCH_ERROR, "Search failed: unavailable")],
)
def test_replayed_web_search_without_hits_ends_on_its_result(self, content, text):
search_only = [
{"type": "server_tool_use", "id": "srvtoolu_1", "name": "web_search"},
{"type": "web_search_tool_result", "tool_use_id": "srvtoolu_1", "content": content},
]
call, result = anthropic_messages_to_openai([{"role": "assistant", "content": search_only}])
assert call["tool_calls"][0]["function"]["arguments"] == "{}"
assert result == {"role": "tool", "tool_call_id": "srvtoolu_1", "content": text}
def test_empty_assistant_turn_is_kept(self):
assert anthropic_messages_to_openai([{"role": "assistant", "content": []}]) == [
{"role": "assistant"}
]
def test_tool_result_maps_to_tool_role(self):
msgs = [_tool_result_turn(tool_use_id = "tu_1", content = "Result text")]
result = anthropic_messages_to_openai(msgs)
assert len(result) == 1
assert result[0]["role"] == "tool"
assert result[0]["tool_call_id"] == "tu_1"
assert result[0]["content"] == "Result text"
def test_tool_result_precedes_trailing_user_text(self):
msgs = [
{"role": "user", "content": [{"type": "text", "text": "what files are here?"}]},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Let me look."},
{"type": "tool_use", "id": "tu_1", "name": "Bash", "input": {"command": "ls"}},
],
},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tu_1", "content": "README.md\nsrc/"},
{"type": "text", "text": "<system-reminder>Keep going.</system-reminder>"},
],
},
]
result = anthropic_messages_to_openai(msgs)
assert [m["role"] for m in result] == ["user", "assistant", "tool", "user"]
assert result[2]["tool_call_id"] == "tu_1"
assert result[2]["content"] == "README.md\nsrc/"
assert result[3]["content"] == "<system-reminder>Keep going.</system-reminder>"
def test_tool_result_with_image_keeps_user_parts_after_tool(self):
msgs = [
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tu_1", "content": "ok"},
{"type": "text", "text": "and this?"},
{
"type": "image",
"source": {"type": "base64", "media_type": "image/jpeg", "data": "AAAA"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert [m["role"] for m in result] == ["tool", "user"]
parts = result[1]["content"]
assert parts[0] == {"type": "text", "text": "and this?"}
assert parts[1]["type"] == "image_url"
def test_tool_results_fold_into_user_turns_for_a_toolless_template(self):
"""Gemma 2/3 lack a tool role and check alternation by parity, so tool messages fail."""
from core.inference.anthropic_compat import fold_tool_results_into_user
msgs = [
{"role": "user", "content": "what files are here?"},
{
"role": "assistant",
"content": "Let me look.",
"tool_calls": [
{
"id": "tu_1",
"type": "function",
"function": {"name": "Bash", "arguments": '{"command": "ls"}'},
}
],
},
{"role": "tool", "tool_call_id": "tu_1", "content": "README.md"},
{"role": "user", "content": "keep going"},
]
folded = fold_tool_results_into_user(msgs)
assert [m["role"] for m in folded] == ["user", "assistant", "user", "user"]
payload = json.loads(folded[2]["content"])
assert payload == {
"tool_response": {
"tool": "Bash",
"content": "README.md",
"tool_call_id": "tu_1",
}
}
assert folded[1] is msgs[1]
def test_folding_is_a_no_op_without_tool_messages(self):
from core.inference.anthropic_compat import fold_tool_results_into_user
msgs = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
]
assert fold_tool_results_into_user(msgs) == msgs
def test_folding_survives_an_orphan_tool_result(self):
"""No tool_calls to resolve the name against: the id still carries."""
from core.inference.anthropic_compat import fold_tool_results_into_user
msgs = [{"role": "tool", "tool_call_id": "nope", "content": "R"}]
folded = fold_tool_results_into_user(msgs)
assert [m["role"] for m in folded] == ["user"]
assert json.loads(folded[0]["content"]) == {
"tool_response": {"content": "R", "tool_call_id": "nope"}
}
def test_sanitizer_folds_only_when_the_template_lacks_tool_support(self):
"""A tool-capable template keeps role=tool as the converter emitted it."""
from routes.inference import _sanitize_anthropic_openai_messages
class Backend:
def __init__(self, supports_tools):
self.supports_tools = supports_tools
anthropic = [
{"role": "user", "content": [{"type": "text", "text": "q"}]},
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": "tu_1", "name": "Bash", "input": {"command": "ls"}}
],
},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tu_1", "content": "README.md"},
{"type": "text", "text": "keep going"},
],
},
]
converted = anthropic_messages_to_openai(anthropic)
tool_capable = _sanitize_anthropic_openai_messages(converted, Backend(True))
assert [m["role"] for m in tool_capable] == ["user", "assistant", "tool", "user"]
toolless = _sanitize_anthropic_openai_messages(converted, Backend(False))
assert [m["role"] for m in toolless] == ["user", "assistant", "user"]
assert "README.md" in toolless[-1]["content"]
assert "keep going" in toolless[-1]["content"]
def test_sanitizer_defaults_to_not_folding_when_the_backend_cannot_answer(self):
from routes.inference import _sanitize_anthropic_openai_messages
class Hostile:
@property
def supports_tools(self):
raise RuntimeError("backend not ready")
msgs = [{"role": "tool", "tool_call_id": "tu_1", "content": "R"}]
assert _sanitize_anthropic_openai_messages(msgs, Hostile()) == msgs
assert _sanitize_anthropic_openai_messages(msgs, object()) == msgs
def test_folding_follows_the_passthrough_capability_not_the_tool_loop_one(self):
"""DiffusionGemma reports supports_tools=False to stay out of the agentic
loop, but its template renders tool roles and client tools dispatch on
supports_tool_passthrough; folding on the former strips that framing."""
from routes.inference import _sanitize_anthropic_openai_messages, _template_supports_tools
class DiffusionGemma:
supports_tools = False
supports_tool_passthrough = True
class ToollessTemplate:
supports_tools = False
supports_tool_passthrough = False
assert _template_supports_tools(DiffusionGemma()) is True
assert _template_supports_tools(ToollessTemplate()) is False
msgs = [
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "tu_1",
"type": "function",
"function": {"name": "Bash", "arguments": "{}"},
}
],
},
{"role": "tool", "tool_call_id": "tu_1", "content": "README.md"},
{"role": "user", "content": "keep going"},
]
kept = _sanitize_anthropic_openai_messages(msgs, DiffusionGemma())
assert [m["role"] for m in kept] == ["assistant", "tool", "user"]
folded = _sanitize_anthropic_openai_messages(msgs, ToollessTemplate())
assert [m["role"] for m in folded] == ["assistant", "user"]
def test_folding_gate_prefers_passthrough_even_when_supports_tools_raises(self):
from routes.inference import _template_supports_tools
class HalfReady:
supports_tool_passthrough = False
@property
def supports_tools(self):
raise RuntimeError("not ready")
# Passthrough answers first, so a raising supports_tools cannot skip the fold.
assert _template_supports_tools(HalfReady()) is False
def test_mixed_text_and_tool_use_blocks(self):
msgs = [
{
"role": "assistant",
"content": [
{"type": "text", "text": "Thinking..."},
{
"type": "tool_use",
"id": "tu_1",
"name": "python",
"input": {"code": "1+1"},
},
{
"type": "tool_use",
"id": "tu_2",
"name": "terminal",
"input": {"command": "ls"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert len(result) == 1
m = result[0]
assert m["content"] == "Thinking..."
assert len(m["tool_calls"]) == 2
def test_tool_result_with_list_content(self):
msgs = [
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "tu_1",
"content": [
{"type": "text", "text": "Line 1"},
{"type": "text", "text": "Line 2"},
],
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert result[0]["content"] == "Line 1\nLine 2"
@pytest.mark.parametrize(
"content, expected",
[
("permission denied", "Error: permission denied"),
("Error: disk full", "Error: disk full"),
("", "Error: tool returned no content"),
(None, "Error: tool returned no content"),
([{"type": "text", "text": "exit 1"}], "Error: exit 1"),
([], "Error: tool returned no content"),
],
)
def test_tool_result_is_error_marks_the_tool_message(self, content, expected):
block = {"type": "tool_result", "tool_use_id": "tu_1", "is_error": True, "content": content}
request = AnthropicMessagesRequest(
max_tokens = 16, messages = [{"role": "user", "content": [block]}]
)
result = anthropic_messages_to_openai([m.model_dump() for m in request.messages])
assert result == [{"role": "tool", "tool_call_id": "tu_1", "content": expected}]
@pytest.mark.parametrize("flag", [{}, {"is_error": False}, {"is_error": None}])
@pytest.mark.parametrize("content", ["42", ""])
def test_tool_result_without_is_error_is_unchanged(self, flag, content):
block = {"type": "tool_result", "tool_use_id": "tu_1", "content": content, **flag}
request = AnthropicMessagesRequest(
max_tokens = 16, messages = [{"role": "user", "content": [block]}]
)
result = anthropic_messages_to_openai([m.model_dump() for m in request.messages])
assert result == [{"role": "tool", "tool_call_id": "tu_1", "content": content}]
def test_tool_result_is_error_with_image_prepends_the_marker(self):
image = {
"type": "image",
"source": {"type": "base64", "media_type": "image/png", "data": "AAAA"},
}
msgs = [
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "tu_1",
"is_error": True,
"content": [{"type": "text", "text": "crashed"}, image],
}
],
}
]
parts = anthropic_messages_to_openai(msgs)[0]["content"]
assert [p["type"] for p in parts] == ["text", "text", "image_url"]
assert [p["text"] for p in parts[:2]] == ["Error:", "crashed"]
def test_tool_result_search_results_keep_title_source_and_text(self):
msgs = [
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "tu_1",
"content": [
{
"type": "search_result",
"source": "https://docs.example.com/vault",
"title": "Vault",
"content": [
{"type": "text", "text": "The code is PURPLE-ELEPHANT-42."},
{"type": "text", "text": "Rotate it monthly."},
],
"citations": {"enabled": True},
},
{
"type": "search_result",
"source": "kb://faq",
"title": "FAQ",
"content": [{"type": "text", "text": "Ask the admin."}],
},
],
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert result == [
{
"role": "tool",
"tool_call_id": "tu_1",
"content": "Title: Vault\nSource: https://docs.example.com/vault\n"
"The code is PURPLE-ELEPHANT-42.\nRotate it monthly.\n"
"Title: FAQ\nSource: kb://faq\nAsk the admin.",
}
]
@pytest.mark.parametrize(
"source, body",
[
({"type": "text", "media_type": "text/plain", "data": "Plain body."}, "Plain body."),
(
{
"type": "content",
"content": [
{"type": "text", "text": "First chunk"},
{"type": "text", "text": "Second chunk"},
],
},
"First chunk\nSecond chunk",
),
({"type": "content", "content": "Whole body."}, "Whole body."),
(
{
"type": "content",
"content": [
{"type": "text", "text": "Before"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "iVBOR",
},
},
{"type": "text", "text": "After"},
],
},
f"Before\n{DOCUMENT_IMAGE_OMITTED}\nAfter",
),
(
{"type": "base64", "media_type": "application/pdf", "data": "JVBERi0="},
DOCUMENT_OMITTED,
),
({"type": "url", "url": "https://example.com/a.pdf"}, DOCUMENT_OMITTED),
],
)
def test_tool_result_document_renders_its_readable_source(self, source, body):
document = {
"type": "document",
"source": source,
"title": "Handbook",
"context": "Internal",
}
msgs = [
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "tu_1", "content": [document]}],
}
]
result = anthropic_messages_to_openai(msgs)
assert result[0]["content"] == f"Title: Handbook\nContext: Internal\n{body}"
def test_top_level_search_result_and_document_reach_the_user_turn(self):
request = AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{
"role": "user",
"content": [
{
"type": "search_result",
"source": "kb://vault",
"title": "Vault",
"content": [{"type": "text", "text": "PURPLE-ELEPHANT-42"}],
},
{
"type": "document",
"source": {"type": "text", "media_type": "text/plain", "data": "Memo."},
},
{"type": "document", "source": {"type": "content", "content": "Notes."}},
{"type": "text", "text": "What is the code?"},
],
}
],
)
result = anthropic_messages_to_openai([m.model_dump() for m in request.messages])
assert result == [
{
"role": "user",
"content": "Title: Vault\nSource: kb://vault\nPURPLE-ELEPHANT-42\n"
"Memo.\nNotes.\nWhat is the code?",
}
]
def test_image_base64_block_becomes_multimodal_part(self):
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is this?"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "AAAA",
},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert len(result) == 1
assert result[0]["role"] == "user"
parts = result[0]["content"]
assert isinstance(parts, list)
assert parts[0] == {"type": "text", "text": "What is this?"}
assert parts[1]["type"] == "image_url"
assert parts[1]["image_url"]["url"] == "data:image/jpeg;base64,AAAA"
def test_image_url_block_forwarded_as_url(self):
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe it"},
{
"type": "image",
"source": {"type": "url", "url": "https://x/y.png"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
parts = result[0]["content"]
assert parts[1] == {"type": "image_url", "image_url": {"url": "https://x/y.png"}}
def test_image_only_user_message_emits_no_text_part(self):
msgs = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "ZZ",
},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
parts = result[0]["content"]
assert len(parts) == 1
assert parts[0]["type"] == "image_url"
def test_image_default_media_type_when_missing(self):
msgs = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "base64", "data": "BB"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
parts = result[0]["content"]
assert parts[0]["image_url"]["url"].startswith("data:image/jpeg;base64,")
def test_image_text_order_preserved(self):
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "before"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "AA",
},
},
{"type": "text", "text": "after"},
{
"type": "image",
"source": {"type": "url", "url": "https://x/y.png"},
},
],
}
]
result = anthropic_messages_to_openai(msgs)
parts = result[0]["content"]
assert [p["type"] for p in parts] == ["text", "image_url", "text", "image_url"]
assert parts[0]["text"] == "before"
assert parts[2]["text"] == "after"
assert parts[1]["image_url"]["url"] == "data:image/png;base64,AA"
assert parts[3]["image_url"]["url"] == "https://x/y.png"
def test_malformed_image_block_is_skipped(self):
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "Hi"},
{"type": "image", "source": {"type": "base64"}},
{"type": "image", "source": {"type": "url"}},
],
}
]
result = anthropic_messages_to_openai(msgs)
assert result[0] == {"role": "user", "content": "Hi"}
class TestAnthropicToolsToOpenAI:
def test_single_tool(self):
tools = [
{
"name": "web_search",
"description": "Search",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string"}},
},
}
]
result = anthropic_tools_to_openai(tools)
assert len(result) == 1
assert result[0]["type"] == "function"
assert result[0]["function"]["name"] == "web_search"
assert result[0]["function"]["parameters"]["type"] == "object"
def test_multiple_tools(self):
tools = [
{"name": "a", "description": "Tool A", "input_schema": {}},
{"name": "b", "description": "Tool B", "input_schema": {}},
]
result = anthropic_tools_to_openai(tools)
assert len(result) == 2
assert result[0]["function"]["name"] == "a"
assert result[1]["function"]["name"] == "b"
def test_empty_list(self):
assert anthropic_tools_to_openai([]) == []
def test_server_tools_are_not_converted_to_openai_functions(self):
tools = [
{"type": "web_fetch_20250910", "name": "web_fetch"},
{"type": "web_search_20250305", "name": "web_search"},
]
assert anthropic_tools_to_openai(tools) == []
@pytest.mark.parametrize(
("type_", "name", "kind"),
[
("bash_20250124", "bash", "bash"),
("text_editor_20250728", "str_replace_based_edit_tool", "text_editor"),
("computer_20251124", "computer", "computer"),
("memory_20250818", "memory", "memory"),
],
)
def test_schema_client_tools_are_converted_to_openai_functions(self, type_, name, kind):
tool = {"type": type_, "name": name}
[result] = anthropic_tools_to_openai([tool])
assert anthropic_schema_client_tool_kind(tool) == kind
assert result["function"]["name"] == name
assert result["function"]["parameters"]["type"] == "object"
@pytest.mark.parametrize(
("type_", "supports_undo"),
[
("text_editor_20241022", True),
("text_editor_20250124", True),
("text_editor_20250429", False),
("text_editor_20250728", False),
],
)
def test_text_editor_commands_follow_tool_version(self, type_, supports_undo):
[result] = anthropic_tools_to_openai(
[{"type": type_, "name": "str_replace_based_edit_tool"}]
)
commands = result["function"]["parameters"]["properties"]["command"]["enum"]
assert ("undo_edit" in commands) is supports_undo
def test_server_tool_selection_merges_enabled_tools_extension(self):
all_tools = [
{"type": "function", "function": {"name": "web_search"}},
{"type": "function", "function": {"name": "python"}},
{"type": "function", "function": {"name": "terminal"}},
]
result = _select_anthropic_server_tools(
all_tools,
requested_studio_tools = {"web_search"},
enabled_tools = ["python"],
)
assert [tool["function"]["name"] for tool in result] == ["web_search", "python"]
@pytest.mark.parametrize(
("requested_studio_tools", "enabled_tools"),
[({"web_search"}, None), (set(), ["web_search"])],
)
def test_explicit_server_tool_selection_does_not_add_read_skill(
self, monkeypatch, requested_studio_tools, enabled_tools
):
import routes.inference as inference_routes
monkeypatch.setattr(
inference_routes,
"_enabled_agent_skills",
lambda: [{"name": "guided", "description": "Guide this task."}],
)
result = _select_anthropic_server_tools(
[{"type": "function", "function": {"name": "web_search"}}],
requested_studio_tools = requested_studio_tools,
enabled_tools = enabled_tools,
)
assert [tool["function"]["name"] for tool in result] == ["web_search"]
def test_pydantic_model_input(self):
tool = AnthropicTool(name = "test", description = "desc", input_schema = {"type": "object"})
result = anthropic_tools_to_openai([tool])
assert result[0]["function"]["name"] == "test"
class TestBuildAnthropicSSEEvent:
def test_basic_event(self):
result = build_anthropic_sse_event("message_start", {"type": "message_start"})
assert result.startswith("event: message_start\n")
assert "data: " in result
assert result.endswith("\n\n")
def test_data_is_valid_json(self):
result = build_anthropic_sse_event("test", {"key": "value"})
data_line = result.split("\n")[1]
payload = json.loads(data_line.removeprefix("data: "))
assert payload == {"key": "value"}
class TestAnthropicStreamEmitter:
def test_start_emits_message_start_only(self):
e = AnthropicStreamEmitter()
events = e.start("msg_123", "test-model")
assert len(events) == 1
assert "message_start" in events[0]
def test_content_delta_opens_text_block_and_emits_delta(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
events = e.feed({"type": "content", "text": "Hello"})
assert len(events) == 2
assert "content_block_start" in events[0]
assert '"type": "text"' in events[0]
parsed = json.loads(events[1].split("data: ")[1])
assert parsed["delta"]["type"] == "text_delta"
assert parsed["delta"]["text"] == "Hello"
def test_cumulative_content_diffs_correctly(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Hel"})
events = e.feed({"type": "content", "text": "Hello"})
parsed = json.loads(events[0].split("data: ")[1])
assert parsed["delta"]["text"] == "lo"
def test_empty_content_diff_no_event(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Hi"})
events = e.feed({"type": "content", "text": "Hi"})
assert events == []
def test_tool_start_closes_text_opens_tool_block(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Thinking"})
events = e.feed(
{
"type": "tool_start",
"tool_name": "web_search",
"tool_call_id": "tc_1",
"arguments": {"query": "test"},
}
)
assert len(events) == 3
assert "content_block_stop" in events[0]
assert "tool_use" in events[1]
assert "input_json_delta" in events[2]
def test_duplicate_tool_start_merges_into_open_tool_block(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
first_events = e.feed(_tool_event(tool_name = "render_html"))
second_events = e.feed(
{
"type": "tool_start",
"tool_name": "render_html",
"tool_call_id": "call_0",
"arguments": {"code": "<!doctype html><html></html>"},
}
)
first_payloads = [json.loads(event.split("data: ")[1]) for event in first_events]
second_payloads = [json.loads(event.split("data: ")[1]) for event in second_events]
tool_starts = [
payload
for payload in first_payloads + second_payloads
if payload["type"] == "content_block_start"
and payload["content_block"]["type"] == "tool_use"
]
assert len(tool_starts) == 1
assert tool_starts[0]["content_block"]["id"].startswith("toolu_")
assert second_payloads == [
{
"type": "content_block_delta",
"index": tool_starts[0]["index"],
"delta": {
"type": "input_json_delta",
"partial_json": json.dumps({"code": "<!doctype html><html></html>"}),
},
}
]
def test_tool_end_closes_tool_opens_new_text_block(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
start_events = e.feed(_tool_event(tool_name = "t", tool_call_id = "tc_1"))
start_payload = next(
json.loads(event.split("data: ")[1])
for event in start_events
if "content_block_start" in event
)
tool_use_id = start_payload["content_block"]["id"]
assert tool_use_id.startswith("toolu_")
events = e.feed(_tool_result_event(tool_name = "t", tool_call_id = "tc_1"))
assert len(events) == 2
assert "content_block_stop" in events[0]
assert "tool_result" in events[1]
parsed = json.loads(events[1].split("data: ")[1])
assert parsed["content"] == "done"
assert parsed["tool_use_id"] == tool_use_id
def test_finish_emits_stop_events(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Hi"})
events = e.finish("end_turn")
assert len(events) == 3
assert "content_block_stop" in events[0]
assert "message_delta" in events[1]
assert "end_turn" in events[1]
assert "message_stop" in events[2]
def test_finish_without_content_skips_block_stop(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
events = e.finish("end_turn")
assert len(events) == 2
assert "message_delta" in events[0]
assert "message_stop" in events[1]
def test_metadata_captured_in_finish_usage(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed(
{
"type": "metadata",
"usage": {"prompt_tokens": 10, "completion_tokens": 20},
}
)
events = e.finish("end_turn")
delta_event = [ev for ev in events if "message_delta" in ev][0]
parsed = json.loads(delta_event.split("data: ")[1])
assert parsed["usage"]["output_tokens"] == 20
def test_status_events_ignored(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
events = e.feed({"type": "status", "text": "Searching..."})
assert events == []
def test_no_tool_calls_simple_text_flow(self):
e = AnthropicStreamEmitter()
start_events = e.start("msg_1", "m")
content_events = e.feed({"type": "content", "text": "Hello world"})
meta_events = e.feed(
{"type": "metadata", "usage": {"prompt_tokens": 5, "completion_tokens": 2}}
)
end_events = e.finish("end_turn")
assert len(start_events) == 1
assert len(content_events) == 2
assert meta_events == []
assert len(end_events) == 3
def test_block_index_increments(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Before"})
assert e.block_index == 0
e.feed(_tool_event(tool_name = "t", tool_call_id = "tc_1"))
assert e.block_index == 1
e.feed(_tool_result_event(tool_name = "t", tool_call_id = "tc_1", result = "ok"))
e.feed({"type": "content", "text": "After"})
assert e.block_index == 2
def test_text_after_tool_resets_prev_text(self):
e = AnthropicStreamEmitter()
e.start("msg_1", "m")
e.feed({"type": "content", "text": "Before tool"})
e.feed(_tool_event(tool_name = "t", tool_call_id = "tc_1"))
e.feed(_tool_result_event(tool_name = "t", tool_call_id = "tc_1", result = "ok"))
events = e.feed({"type": "content", "text": "After tool"})
assert "content_block_start" in events[0]
parsed = json.loads(events[1].split("data: ")[1])
assert parsed["delta"]["text"] == "After tool"
def _web_search_stream(
self,
result,
arguments = None,
):
e = AnthropicStreamEmitter()
events = e.start("msg_1", "m")
events += e.feed(
_tool_event(
tool_name = "web_search",
arguments = arguments or {"query": "llama.cpp latest release"},
)
)
events += e.feed(_tool_result_event(tool_name = "web_search", result = result))
events += e.feed({"type": "content", "text": "The latest release is b9999."})
events += e.finish("end_turn")
return events, [json.loads(ev.split("data: ")[1]) for ev in events]
def test_web_search_streams_as_server_tool_with_source_links(self):
events, payloads = self._web_search_stream(_WEB_SEARCH_RESULT)
starts = [p for p in payloads if p["type"] == "content_block_start"]
stops = [p["index"] for p in payloads if p["type"] == "content_block_stop"]
blocks = [p["content_block"] for p in starts]
assert [b["type"] for b in blocks] == ["server_tool_use", "web_search_tool_result", "text"]
assert blocks[0]["id"].startswith("srvtoolu_")
assert blocks[0]["name"] == "web_search"
assert blocks[1] == {
"type": "web_search_tool_result",
"tool_use_id": blocks[0]["id"],
"content": _WEB_SEARCH_HITS,
}
assert stops == [p["index"] for p in starts] == [0, 1, 2]
assert {
"type": "content_block_delta",
"index": 0,
"delta": {
"type": "input_json_delta",
"partial_json": json.dumps({"query": "llama.cpp latest release"}),
},
} in payloads
assert not any(ev.startswith("event: tool_result") for ev in events)
assert _emitter_client_text(events) == "The latest release is b9999."
def test_consecutive_web_searches_reusing_a_call_id_stay_separate(self):
e = AnthropicStreamEmitter()
events = e.start("msg_1", "m")
for i, url in enumerate(["https://e.com/0", "https://e.com/1"]):
events += e.feed(_tool_event(tool_name = "web_search", arguments = {"url": url}))
events += e.feed(_tool_result_event(tool_name = "web_search", result = f"page {i}"))
payloads = [json.loads(ev.split("data: ")[1]) for ev in events]
first, first_result, second, second_result = [
p["content_block"] for p in payloads if p["type"] == "content_block_start"
]
assert first["id"] != second["id"]
assert first_result["tool_use_id"] == first["id"]
assert second_result["tool_use_id"] == second["id"]
assert second_result["content"][0]["url"] == "https://e.com/1"
@pytest.mark.parametrize("arguments, result, content", _WEB_SEARCH_OUTCOMES)
def test_web_search_result_content_by_outcome(self, arguments, result, content):
_, payloads = self._web_search_stream(result, arguments)
blocks = [p["content_block"] for p in payloads if p["type"] == "content_block_start"]
assert [b["type"] for b in blocks] == ["server_tool_use", "web_search_tool_result", "text"]
assert blocks[1]["content"] == content
def _connected_request(disconnected = False):
async def _is_disconnected():
return disconnected
return SimpleNamespace(is_disconnected = _is_disconnected)
class TestAnthropicToolNonStreaming:
@pytest.mark.parametrize(
("helper", "event"),
[
pytest.param(
_anthropic_tool_non_streaming,
{"type": "content", "text": "ok"},
id = "tools",
),
pytest.param(_anthropic_plain_non_streaming, "ok", id = "plain"),
],
)
def test_complete_response_build_keeps_event_loop_responsive(self, helper, event):
loop_thread = threading.current_thread()
generator_threads = []
def _run_gen():
generator_threads.append(threading.current_thread())
time.sleep(0.08)
yield event
async def _run():
task = asyncio.create_task(helper(_connected_request(), _run_gen, "msg_1", "m"))
await asyncio.sleep(0)
heartbeat_ticks = 0
while not task.done():
heartbeat_ticks += 1
await asyncio.sleep(0.005)
return await task, heartbeat_ticks
response, heartbeat_ticks = asyncio.run(_run())
assert response.status_code == 200
assert heartbeat_ticks > 0
assert len(generator_threads) == 1
assert generator_threads[0] is not loop_thread
def test_tool_event_reduction_keeps_event_loop_responsive(self, monkeypatch):
import routes.inference as inf_mod
reduction_threads = []
real_strip = inf_mod._strip_tool_xml_for_display
def _slow_strip(*args, **kwargs):
reduction_threads.append(threading.current_thread())
time.sleep(0.08)
return real_strip(*args, **kwargs)
monkeypatch.setattr(inf_mod, "_strip_tool_xml_for_display", _slow_strip)
def _run_gen():
# Carries markup: an event without any is returned as is and never reaches the full strip.
yield {"type": "content", "text": "ok </tool_call>"}
async def _run():
task = asyncio.create_task(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
await asyncio.sleep(0)
heartbeat_ticks = 0
while not task.done():
heartbeat_ticks += 1
await asyncio.sleep(0.005)
return await task, heartbeat_ticks
response, heartbeat_ticks = asyncio.run(_run())
assert response.status_code == 200
assert heartbeat_ticks > 0
assert len(reduction_threads) == 1
assert reduction_threads[0] is not threading.current_thread()
assert reduction_threads[0].daemon is True
@pytest.mark.parametrize(
("helper", "event"),
[
pytest.param(
_anthropic_tool_non_streaming,
{"type": "content", "text": "ok"},
id = "tools",
),
pytest.param(_anthropic_plain_non_streaming, "ok", id = "plain"),
],
)
def test_cancellation_waits_for_generator_worker(self, helper, event):
generator_started = threading.Event()
generator_stopped = threading.Event()
cancel_event = threading.Event()
def _run_gen():
generator_started.set()
assert cancel_event.wait(1.0)
generator_stopped.set()
yield event
async def _cancel_generation():
task = asyncio.create_task(
helper(_connected_request(), _run_gen, "msg_1", "m", cancel_event = cancel_event)
)
assert await asyncio.to_thread(generator_started.wait, 1.0)
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
assert cancel_event.is_set()
assert generator_stopped.is_set()
asyncio.run(_cancel_generation())
@pytest.mark.parametrize(
("helper", "event"),
[
pytest.param(
_anthropic_tool_non_streaming,
{"type": "content", "text": "ok"},
id = "tools",
),
pytest.param(_anthropic_plain_non_streaming, "ok", id = "plain"),
],
)
def test_client_disconnect_cancels_generation(self, helper, event):
generator_started = threading.Event()
cancel_event = threading.Event()
emitted = 0
def _run_gen():
nonlocal emitted
generator_started.set()
for _ in range(400):
if cancel_event.wait(0.005):
return
emitted += 1
yield event
async def _drive():
request = _connected_request(disconnected = True)
response = await helper(request, _run_gen, "msg_1", "m", cancel_event = cancel_event)
assert await asyncio.to_thread(generator_started.wait, 1.0)
return response
response = asyncio.run(_drive())
assert cancel_event.is_set()
assert emitted < 400
assert response.status_code == 200
def test_duplicate_tool_start_replaces_provisional_tool_block(self):
def _run_gen():
yield _tool_event(tool_name = "render_html")
yield {
"type": "tool_start",
"tool_name": "render_html",
"tool_call_id": "call_0",
"arguments": {"code": "<!doctype html><html></html>"},
}
yield _tool_result_event(tool_name = "render_html", result = "Rendered HTML canvas.")
response = asyncio.run(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
body = json.loads(response.body)
tool_blocks = [block for block in body["content"] if block["type"] == "tool_use"]
assert len(tool_blocks) == 1
assert tool_blocks[0]["type"] == "tool_use"
assert tool_blocks[0]["id"].startswith("toolu_")
assert tool_blocks[0]["name"] == "render_html"
assert tool_blocks[0]["input"] == {"code": "<!doctype html><html></html>"}
def test_web_search_returns_server_tool_blocks_with_source_links(self):
def _run_gen():
yield _tool_event(tool_name = "web_search", arguments = {"query": "llama.cpp release"})
yield _tool_result_event(tool_name = "web_search", result = _WEB_SEARCH_RESULT)
yield {"type": "content", "text": "The latest release is b9999."}
response = asyncio.run(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
body = json.loads(response.body)
call, result, text = body["content"]
assert call["type"] == "server_tool_use"
assert call["id"].startswith("srvtoolu_")
assert call["input"] == {"query": "llama.cpp release"}
assert result == {
"type": "web_search_tool_result",
"tool_use_id": call["id"],
"content": _WEB_SEARCH_HITS,
}
assert text == {"type": "text", "text": "The latest release is b9999."}
assert body["stop_reason"] == "end_turn"
def test_consecutive_web_searches_reusing_a_call_id_stay_separate(self):
def _run_gen():
for i, query in enumerate(["first", "second"]):
yield _tool_event(tool_name = "web_search", arguments = {"query": query})
yield _tool_result_event(
tool_name = "web_search", result = f"Title: T{i}\nURL: https://e.com/{i}"
)
response = asyncio.run(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
first, first_result, second, second_result = json.loads(response.body)["content"]
assert [first["input"], second["input"]] == [{"query": "first"}, {"query": "second"}]
assert first["id"] != second["id"]
assert first_result["tool_use_id"] == first["id"]
assert second_result["tool_use_id"] == second["id"]
assert second_result["content"][0]["url"] == "https://e.com/1"
def test_consecutive_tool_calls_reusing_a_call_id_keep_their_own_input(self):
def _run_gen():
for code in ["print(1)", "print(2)"]:
yield _tool_event(arguments = {"code": code})
yield _tool_result_event()
response = asyncio.run(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
blocks = json.loads(response.body)["content"]
assert [b["input"] for b in blocks] == [{"code": "print(1)"}, {"code": "print(2)"}]
@pytest.mark.parametrize("arguments, result, content", _WEB_SEARCH_OUTCOMES)
def test_web_search_result_content_by_outcome(self, arguments, result, content):
def _run_gen():
yield _tool_event(tool_name = "web_search", arguments = arguments)
yield _tool_result_event(tool_name = "web_search", result = result)
response = asyncio.run(
_anthropic_tool_non_streaming(_connected_request(), _run_gen, "msg_1", "m")
)
call, search_result = json.loads(response.body)["content"]
assert search_result["tool_use_id"] == call["id"]
assert search_result["content"] == content
def test_display_strip_gates_on_declared_tools(self):
def _run_gen():
yield {
"type": "content",
"text": 'Try foo[ARGS]{"x": 1} but not web_search[ARGS]{"q": "hi"} here.',
}
tools = [{"type": "function", "function": {"name": "web_search", "parameters": {}}}]
response = asyncio.run(
_anthropic_tool_non_streaming(
_connected_request(), _run_gen, "msg_1", "m", openai_tools = tools
)
)
body = json.loads(response.body)
text = "".join(b["text"] for b in body["content"] if b["type"] == "text")
assert 'foo[ARGS]{"x": 1}' in text
assert "web_search[ARGS]" not in text
class TestAnthropicPassthroughEmitter:
def _parse(self, event_str):
return json.loads(event_str.split("data: ")[1])
def test_start_emits_message_start_only(self):
e = AnthropicPassthroughEmitter()
events = e.start("msg_1", "test-model")
assert len(events) == 1
assert "message_start" in events[0]
parsed = self._parse(events[0])
assert parsed["message"]["id"] == "msg_1"
assert parsed["message"]["model"] == "test-model"
def test_text_chunk_opens_text_block_and_emits_delta(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
chunk = {"choices": [{"delta": {"content": "Hello"}}]}
events = e.feed_chunk(chunk)
assert len(events) == 2
assert "content_block_start" in events[0]
assert '"type": "text"' in events[0]
delta = self._parse(events[1])
assert delta["delta"]["type"] == "text_delta"
assert delta["delta"]["text"] == "Hello"
def test_sequential_text_chunks_single_block(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
events1 = e.feed_chunk({"choices": [{"delta": {"content": "Hello"}}]})
events2 = e.feed_chunk({"choices": [{"delta": {"content": " world"}}]})
assert len(events1) == 2
assert len(events2) == 1
assert self._parse(events2[0])["delta"]["text"] == " world"
def test_tool_call_opens_tool_use_block(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
chunk = _chunk(
tool_calls = [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "Bash", "arguments": ""},
},
],
)
events = e.feed_chunk(chunk)
assert len(events) == 1
parsed = self._parse(events[0])
assert parsed["type"] == "content_block_start"
assert parsed["content_block"]["type"] == "tool_use"
assert parsed["content_block"]["id"].startswith("toolu_")
assert parsed["content_block"]["name"] == "Bash"
def test_tool_call_arguments_streamed_as_input_json_delta(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk(
_chunk(
tool_calls = [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "Bash", "arguments": ""},
},
],
)
)
events1 = e.feed_chunk(
_chunk(tool_calls = [{"index": 0, "function": {"arguments": '{"cmd'}}])
)
events2 = e.feed_chunk(
_chunk(tool_calls = [{"index": 0, "function": {"arguments": '": "ls"}'}}])
)
parsed1 = self._parse(events1[0])
parsed2 = self._parse(events2[0])
assert parsed1["delta"]["type"] == "input_json_delta"
assert parsed1["delta"]["partial_json"] == '{"cmd'
assert parsed2["delta"]["partial_json"] == '": "ls"}'
def test_text_then_tool_closes_text_block(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk({"choices": [{"delta": {"content": "Let me check."}}]})
events = e.feed_chunk(
_chunk(
tool_calls = [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "Bash", "arguments": ""},
},
],
)
)
assert "content_block_stop" in events[0]
assert "content_block_start" in events[1]
assert '"type": "tool_use"' in events[1]
def test_finish_reason_tool_calls_sets_tool_use_stop(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk(
_chunk(
tool_calls = [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "Bash", "arguments": "{}"},
},
],
)
)
e.feed_chunk({"choices": [{"delta": {}, "finish_reason": "tool_calls"}]})
events = e.finish()
delta_event = [ev for ev in events if "message_delta" in ev][0]
parsed = self._parse(delta_event)
assert parsed["delta"]["stop_reason"] == "tool_use"
def test_finish_reason_stop_sets_end_turn(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk({"choices": [{"delta": {"content": "Hi"}}]})
e.feed_chunk({"choices": [{"delta": {}, "finish_reason": "stop"}]})
events = e.finish()
delta_event = [ev for ev in events if "message_delta" in ev][0]
parsed = self._parse(delta_event)
assert parsed["delta"]["stop_reason"] == "end_turn"
def test_finish_reason_length_sets_max_tokens(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk({"choices": [{"delta": {"content": "Hi"}}]})
e.feed_chunk({"choices": [{"delta": {}, "finish_reason": "length"}]})
events = e.finish()
delta_event = [ev for ev in events if "message_delta" in ev][0]
parsed = self._parse(delta_event)
assert parsed["delta"]["stop_reason"] == "max_tokens"
def test_finish_closes_current_block(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk({"choices": [{"delta": {"content": "Hi"}}]})
events = e.finish()
assert "content_block_stop" in events[0]
assert "message_delta" in events[1]
assert "message_stop" in events[2]
def test_usage_chunk_captured(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk({"choices": [{"delta": {"content": "Hi"}}]})
e.feed_chunk(
{
"choices": [],
"usage": {"prompt_tokens": 10, "completion_tokens": 5},
}
)
events = e.finish()
delta_event = [ev for ev in events if "message_delta" in ev][0]
parsed = self._parse(delta_event)
assert parsed["usage"]["output_tokens"] == 5
def test_empty_chunk_returns_no_events(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
events = e.feed_chunk({"choices": []})
assert events == []
def test_no_blocks_at_all_still_produces_valid_finish(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
events = e.finish()
assert not any("content_block_stop" in ev for ev in events)
assert any("message_delta" in ev for ev in events)
assert any("message_stop" in ev for ev in events)
def test_multiple_tool_calls_distinct_blocks(self):
e = AnthropicPassthroughEmitter()
e.start("msg_1", "m")
e.feed_chunk(
_chunk(
tool_calls = [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "Bash", "arguments": "{}"},
},
],
)
)
events = e.feed_chunk(
_chunk(
tool_calls = [
{
"index": 1,
"id": "c2",
"type": "function",
"function": {"name": "Read", "arguments": "{}"},
},
],
)
)
assert "content_block_stop" in events[0]
assert "content_block_start" in events[1]
parsed = self._parse(events[1])
assert parsed["content_block"]["name"] == "Read"
assert parsed["content_block"]["id"].startswith("toolu_")
class TestAnthropicPassthroughStreamAdapter:
class _Request:
async def is_disconnected(self):
return False
@staticmethod
async def _collect(response):
chunks = []
async for chunk in response.body_iterator:
chunks.append(chunk.decode() if isinstance(chunk, bytes) else chunk)
return chunks
@staticmethod
def _payloads(lines, event_name):
prefix = f"event: {event_name}\n"
return [
json.loads(line.split("data: ", 1)[1].strip())
for line in lines
if line.startswith(prefix)
]
def test_stream_forced_tool_is_sent_as_its_one_tool_under_required(self, monkeypatch):
import routes.inference as inf_mod
captured = {}
def handler(request: httpx.Request) -> httpx.Response:
captured["body"] = json.loads(request.content.decode())
chunks = [
{"choices": [{"delta": {"content": '<tool_call>{"name":"other","arguments":{}}'}}]},
{"choices": [{"delta": {"content": "</tool_call>"}, "finish_reason": "stop"}]},
]
content = "".join(f"data: {json.dumps(chunk)}\n\n" for chunk in chunks)
content += "data: [DONE]\n\n"
return httpx.Response(
200,
content = content.encode(),
headers = {"content-type": "text/event-stream"},
)
transport = httpx.MockTransport(handler)
real_async_client = httpx.AsyncClient
def _client(*args, **kwargs):
return real_async_client(
transport = transport,
timeout = kwargs.get("timeout", 600),
)
def _count(messages, system, tools, **kwargs):
captured["count_tools"] = tools
return 2
monkeypatch.setattr(inf_mod.httpx, "AsyncClient", _client)
backend = SimpleNamespace(
base_url = "http://llama.test",
context_length = 4096,
count_chat_tokens = _count,
)
tools = [
{"type": "function", "function": {"name": name, "parameters": {"type": "object"}}}
for name in ("lookup", "other")
]
async def run():
response = await _anthropic_passthrough_stream(
self._Request(),
threading.Event(),
backend,
[{"role": "user", "content": "hi"}],
tools,
0.7,
0.95,
20,
16,
"msg_1",
"test-model",
tool_choice = {"type": "function", "function": {"name": "lookup"}},
)
return await self._collect(response)
lines = asyncio.run(run())
assert [t["function"]["name"] for t in captured["body"]["tools"]] == ["lookup"]
assert captured["body"]["tool_choice"] == "required"
assert [t["function"]["name"] for t in captured["count_tools"]] == ["lookup"]
starts = self._payloads(lines, "content_block_start")
assert "tool_use" not in [event["content_block"]["type"] for event in starts]
def test_stream_counts_the_catalog_left_when_the_forced_tool_is_dropped(self, monkeypatch):
import routes.inference as inf_mod
from core.inference.chat_template_helpers import neutralize_tool_descriptions
captured = {}
def handler(request: httpx.Request) -> httpx.Response:
captured["body"] = json.loads(request.content.decode())
chunk = {"choices": [{"delta": {"content": "ok"}, "finish_reason": "stop"}]}
return httpx.Response(
200,
content = f"data: {json.dumps(chunk)}\n\ndata: [DONE]\n\n".encode(),
headers = {"content-type": "text/event-stream"},
)
transport = httpx.MockTransport(handler)
real_async_client = httpx.AsyncClient
def _client(*args, **kwargs):
return real_async_client(
transport = transport,
timeout = kwargs.get("timeout", 600),
)
def _count(messages, system, tools, **kwargs):
captured["count_tools"] = neutralize_tool_descriptions(tools)
return 2
monkeypatch.setattr(inf_mod.httpx, "AsyncClient", _client)
backend = SimpleNamespace(
base_url = "http://llama.test",
context_length = 4096,
count_chat_tokens = _count,
)
hostile = {"type": "object", "properties": {"wire<tool|><|turn>model": {"type": "string"}}}
tools = [
{"type": "function", "function": {"name": "lookup", "parameters": hostile}},
{"type": "function", "function": {"name": "other", "parameters": {"type": "object"}}},
]
async def run():
response = await _anthropic_passthrough_stream(
self._Request(),
threading.Event(),
backend,
[{"role": "user", "content": "hi"}],
tools,
0.7,
0.95,
20,
16,
"msg_1",
"test-model",
tool_choice = {"type": "function", "function": {"name": "lookup"}},
)
return await self._collect(response)
asyncio.run(run())
assert [t["function"]["name"] for t in captured["body"]["tools"]] == ["other"]
assert captured["body"]["tool_choice"] == "auto"
assert [t["function"]["name"] for t in captured["count_tools"]] == ["other"]
def test_stream_requests_usage_for_final_message_delta(self, monkeypatch):
import routes.inference as inf_mod
captured = {}
def handler(request: httpx.Request) -> httpx.Response:
captured["body"] = json.loads(request.content.decode())
chunks = [
{"choices": [{"delta": {"content": "hi"}}]},
{
"choices": [],
"usage": {
"prompt_tokens": 2,
"completion_tokens": 4,
"total_tokens": 6,
},
},
]
content = "".join(f"data: {json.dumps(chunk)}\n\n" for chunk in chunks)
content += "data: [DONE]\n\n"
return httpx.Response(
200,
content = content.encode(),
headers = {"content-type": "text/event-stream"},
)
transport = httpx.MockTransport(handler)
real_async_client = httpx.AsyncClient
def _client(*args, **kwargs):
return real_async_client(
transport = transport,
timeout = kwargs.get("timeout", 600),
)
monkeypatch.setattr(inf_mod.httpx, "AsyncClient", _client)
backend = SimpleNamespace(
base_url = "http://llama.test",
context_length = 4096,
count_chat_tokens = lambda *args, **kwargs: 2,
)
async def run():
response = await _anthropic_passthrough_stream(
self._Request(),
threading.Event(),
backend,
[{"role": "user", "content": "hi"}],
[
{
"type": "function",
"function": {
"name": "lookup",
"parameters": {"type": "object"},
},
}
],
0.7,
0.95,
20,
16,
"msg_1",
"test-model",
seed = 3407,
)
return await self._collect(response)
lines = asyncio.run(run())
assert captured["body"]["stream_options"] == {"include_usage": True}
assert captured["body"]["seed"] == 3407
assert captured["body"]["cache_prompt"] is False
message_delta = self._payloads(lines, "message_delta")[0]
assert message_delta["usage"]["input_tokens"] == 2
assert message_delta["usage"]["output_tokens"] == 4
@pytest.mark.parametrize(
"reasoning_kwargs, expected",
[
({}, None),
({"enable_thinking": True}, {"enable_thinking": True}),
({"enable_thinking": False}, {"enable_thinking": False}),
(
{"enable_thinking": True, "reasoning_effort": "high"},
{"enable_thinking": True, "reasoning_effort": "high"},
),
],
)
def test_stream_forwards_reasoning_to_llama_server(
self, monkeypatch, reasoning_kwargs, expected
):
"""Without it the reasoning request is dropped and thinking can never be switched on."""
import routes.inference as inf_mod
captured = {}
def handler(request: httpx.Request) -> httpx.Response:
captured["body"] = json.loads(request.content.decode())
content = 'data: {"choices": [{"delta": {"content": "hi"}}]}\n\ndata: [DONE]\n\n'
return httpx.Response(
200,
content = content.encode(),
headers = {"content-type": "text/event-stream"},
)
transport = httpx.MockTransport(handler)
real_async_client = httpx.AsyncClient
def _client(*args, **kwargs):
return real_async_client(transport = transport, timeout = kwargs.get("timeout", 600))
monkeypatch.setattr(inf_mod.httpx, "AsyncClient", _client)
# Echo kwargs back so the test covers route wiring, not backend style logic.
backend = SimpleNamespace(
base_url = "http://llama.test",
context_length = 4096,
count_chat_tokens = lambda *args, **kwargs: 2,
_request_reasoning_kwargs = lambda et, re_, pt: (
{
k: v
for k, v in (
("enable_thinking", et),
("reasoning_effort", re_),
("preserve_thinking", pt),
)
if v is not None
}
or None
),
)
async def run():
response = await _anthropic_passthrough_stream(
self._Request(),
threading.Event(),
backend,
[{"role": "user", "content": "hi"}],
[{"type": "function", "function": {"name": "lookup", "parameters": {}}}],
0.7,
0.95,
20,
16,
"msg_1",
"test-model",
**reasoning_kwargs,
)
return await self._collect(response)
asyncio.run(run())
assert captured["body"].get("chat_template_kwargs") == expected
@pytest.mark.parametrize("upstream_error", [False, True])
def test_stream_stopped_by_the_server_does_not_look_finished(self, monkeypatch, upstream_error):
import routes.inference as inf_mod
cancel_event = threading.Event()
async def upstream_lines():
chunk = {"choices": [{"delta": {"content": "Once"}}]}
yield f"data: {json.dumps(chunk)}\n\n".encode()
cancel_event.set()
await asyncio.sleep(0.2)
if upstream_error:
raise httpx.ReadError("closed by cancel")
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
content = upstream_lines(),
headers = {"content-type": "text/event-stream"},
)
transport = httpx.MockTransport(handler)
real_async_client = httpx.AsyncClient
def _client(*args, **kwargs):
return real_async_client(transport = transport, timeout = kwargs.get("timeout", 600))
monkeypatch.setattr(inf_mod.httpx, "AsyncClient", _client)
backend = SimpleNamespace(
base_url = "http://llama.test",
context_length = 4096,
count_chat_tokens = lambda *args, **kwargs: 2,
)
async def run():
response = await _anthropic_passthrough_stream(
self._Request(),
cancel_event,
backend,
[{"role": "user", "content": "hi"}],
[{"type": "function", "function": {"name": "lookup", "parameters": {}}}],
0.7,
0.95,
20,
16,
"msg_1",
"test-model",
)
return await self._collect(response)
lines = asyncio.run(run())
assert self._payloads(lines, "content_block_delta")[0]["delta"]["text"] == "Once"
assert self._payloads(lines, "message_delta") == []
assert self._payloads(lines, "message_stop") == []
@pytest.mark.parametrize("stopped", [False, True])
def test_in_process_client_tool_stream_stopped_does_not_look_finished(self, stopped):
import routes.inference as inf_mod
class Source:
cancel_event = threading.Event()
@property
def cancelled(self):
return self.cancel_event.is_set()
async def chunks(self):
yield {"choices": [{"delta": {"content": "Once"}}]}
if stopped:
self.cancel_event.set()
async def next_chunk(self, chunks):
return await anext(chunks, None)
async def close(self, *, cancelled):
pass
async def run():
response = inf_mod._anthropic_in_process_sse(
Source(),
request = self._Request(),
monitor_id = None,
message_id = "msg_1",
model_name = "test-model",
input_tokens = 2,
disable_parallel_tool_use = False,
)
return await self._collect(response)
lines = asyncio.run(run())
assert self._payloads(lines, "content_block_delta")[0]["delta"]["text"] == "Once"
assert bool(self._payloads(lines, "message_stop")) is not stopped
class TestReasoningContentReachesTheClient:
"""llama-server puts the thinking trace in `reasoning_content`, not `content`.
Reading only `content` drops it and the model looks like it never thought --
which is what every Claude Code turn hit, since tool turns always split."""
def test_stream_emits_thinking_block(self):
from core.inference.anthropic_compat import AnthropicPassthroughEmitter
emitter = AnthropicPassthroughEmitter()
emitter.start("msg_1", "test-model")
out = emitter.feed_chunk({"choices": [{"delta": {"reasoning_content": "step one"}}]})
out += emitter.feed_chunk({"choices": [{"delta": {"content": "the answer"}}]})
blob = "".join(out)
assert '"type": "thinking"' in blob
assert "thinking_delta" in blob
assert "step one" in blob
assert blob.index("thinking_delta") < blob.index("text_delta")
def test_stream_thinking_only_still_emits(self):
"""A reasoning-only reply must not come back as an empty message."""
from core.inference.anthropic_compat import AnthropicPassthroughEmitter
emitter = AnthropicPassthroughEmitter()
emitter.start("msg_1", "test-model")
blob = "".join(emitter.feed_chunk({"choices": [{"delta": {"reasoning_content": "only"}}]}))
assert "thinking_delta" in blob and "only" in blob
def test_stream_reasoning_reconstructed_as_text_when_thinking_off(self):
"""Thinking effectively off: llama-server may still shunt a literal
<think> example into reasoning_content; the client asked for those
bytes, so they come back as text with the tags restored."""
from core.inference.anthropic_compat import AnthropicPassthroughEmitter
emitter = AnthropicPassthroughEmitter(reasoning_as_thinking = False)
emitter.start("msg_1", "test-model")
out = emitter.feed_chunk({"choices": [{"delta": {"reasoning_content": "like this"}}]})
out += emitter.feed_chunk({"choices": [{"delta": {"content": " is the syntax"}}]})
out += emitter.finish()
blob = "".join(out)
assert "thinking_delta" not in blob
text = "".join(
json.loads(stripped[len("data: ") :])["delta"].get("text", "")
for line in out
for stripped in (part.strip() for part in line.split("\n"))
if stripped.startswith("data: ") and '"text_delta"' in stripped
)
assert text == "<think>like this</think> is the syntax"
def test_stream_reconstruction_closes_before_same_chunk_content(self):
"""One chunk can carry final reasoning and content; the closing tag goes between them."""
from core.inference.anthropic_compat import AnthropicPassthroughEmitter
emitter = AnthropicPassthroughEmitter(reasoning_as_thinking = False)
emitter.start("msg_1", "test-model")
out = emitter.feed_chunk({"choices": [{"delta": {"reasoning_content": "like this"}}]})
out += emitter.feed_chunk(
{"choices": [{"delta": {"reasoning_content": " too", "content": "Answer"}}]}
)
out += emitter.finish()
text = "".join(
json.loads(stripped[len("data: ") :])["delta"].get("text", "")
for line in out
for stripped in (part.strip() for part in line.split("\n"))
if stripped.startswith("data: ") and '"text_delta"' in stripped
)
assert text == "<think>like this too</think>Answer"
def test_non_streaming_builds_thinking_block(self):
from models.inference import (
AnthropicMessagesResponse,
AnthropicResponseTextBlock,
AnthropicResponseThinkingBlock,
)
resp = AnthropicMessagesResponse(
model = "m",
content = [
AnthropicResponseThinkingBlock(thinking = "because 2+2"),
AnthropicResponseTextBlock(text = "4"),
],
)
dumped = resp.model_dump()
assert [b["type"] for b in dumped["content"]] == ["thinking", "text"]
assert dumped["content"][0]["thinking"] == "because 2+2"
assert "signature" in dumped["content"][0]
class TestAnthropicReasoningArgs:
"""Accepts the thinking block and x-unsloth reasoning fields instead of ignoring them."""
@staticmethod
def _payload(**kwargs):
from models.inference import AnthropicMessagesRequest
return AnthropicMessagesRequest(
model = "m",
max_tokens = 16,
messages = [{"role": "user", "content": "hi"}],
**kwargs,
)
@pytest.mark.parametrize(
"kwargs, expected",
[
({}, None),
({"thinking": {"type": "enabled", "budget_tokens": 600}}, True),
({"thinking": {"type": "disabled"}}, False),
({"thinking": {"type": "adaptive"}}, True),
({"thinking": {"type": "auto"}}, True),
({"enable_thinking": True}, True),
({"enable_thinking": False}, False),
({"thinking": {"type": "disabled"}, "enable_thinking": True}, True),
],
)
def test_resolved_enable_thinking(self, kwargs, expected):
assert self._payload(**kwargs).resolved_enable_thinking() is expected
def test_reasoning_args_reach_the_generators(self):
from routes.inference import _anthropic_reasoning_args
payload = self._payload(
thinking = {"type": "enabled"},
reasoning_effort = "high",
preserve_thinking = True,
)
assert _anthropic_reasoning_args(payload) == {
"enable_thinking": True,
"reasoning_effort": "high",
"preserve_thinking": True,
}
@pytest.mark.parametrize("thinking_type", ["adaptive", "auto", "high", "future_tier"])
def test_unknown_thinking_type_never_400s(self, thinking_type):
"""A strict Literal here regressed real Claude Code traffic to a 400:
`thinking.type: Input should be 'enabled' or 'disabled'`."""
payload = self._payload(thinking = {"type": thinking_type})
assert payload.resolved_enable_thinking() is True
def test_budget_tokens_accepted_not_rejected(self):
"""Claude Code always sends budget_tokens; it must parse rather than 400."""
payload = self._payload(thinking = {"type": "enabled", "budget_tokens": 4096})
assert payload.thinking.budget_tokens == 4096
assert payload.resolved_enable_thinking() is True
def test_replayed_thinking_blocks_are_accepted(self):
"""Clients replay thinking blocks with tool results; rejecting them 422s turn 2."""
from core.inference.anthropic_compat import anthropic_messages_to_openai
from models.inference import AnthropicMessagesRequest
payload = AnthropicMessagesRequest(
model = "m",
max_tokens = 16,
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "let me check", "signature": ""},
{"type": "redacted_thinking", "data": "opaque"},
{"type": "tool_use", "id": "toolu_1", "name": "ls", "input": {}},
],
},
_tool_result_turn(tool_use_id = "toolu_1", content = "ok"),
],
)
converted = anthropic_messages_to_openai([m.model_dump() for m in payload.messages])
assistant = next(m for m in converted if m["role"] == "assistant")
assert "thinking" not in json.dumps(assistant)
assert assistant["tool_calls"][0]["function"]["name"] == "ls"
def _jpeg_data_url() -> str:
from PIL import Image
img = Image.new("RGB", (2, 2), (255, 0, 0))
buf = _BytesIO()
img.save(buf, format = "JPEG")
b64 = _b64.b64encode(buf.getvalue()).decode("ascii")
return f"data:image/jpeg;base64,{b64}"
class TestNormalizeAnthropicOpenAIImages:
def test_noop_when_no_images(self):
msgs = [{"role": "user", "content": "hi"}]
has_image = _normalize_anthropic_openai_images(msgs, is_vision = False)
assert has_image is False
assert msgs == [{"role": "user", "content": "hi"}]
def test_returns_true_when_image_present(self):
msgs = [
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": _jpeg_data_url()}},
],
}
]
assert _normalize_anthropic_openai_images(msgs, is_vision = True) is True
def test_rejects_image_when_model_not_vision(self):
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "?"},
{
"type": "image_url",
"image_url": {"url": _jpeg_data_url()},
},
],
}
]
with pytest.raises(HTTPException) as exc:
_normalize_anthropic_openai_images(msgs, is_vision = False)
assert exc.value.status_code == 400
def test_forwards_jpeg_data_url_unchanged(self):
original_url = _jpeg_data_url()
msgs = [
{
"role": "user",
"content": [
{"type": "text", "text": "?"},
{"type": "image_url", "image_url": {"url": original_url}},
],
}
]
_normalize_anthropic_openai_images(msgs, is_vision = True)
assert msgs[0]["content"][1]["image_url"]["url"] == original_url
def test_reencodes_webp_data_url_to_png(self):
from PIL import Image
buf = _BytesIO()
Image.new("RGB", (2, 2), (255, 0, 0)).save(buf, format = "WEBP")
url = "data:image/webp;base64," + _b64.b64encode(buf.getvalue()).decode("ascii")
msgs = [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": url}}]}]
_normalize_anthropic_openai_images(msgs, is_vision = True)
assert msgs[0]["content"][0]["image_url"]["url"].startswith("data:image/png;base64,iVBOR")
def test_remote_url_is_replaced_by_the_bytes_we_fetched(self, monkeypatch):
import core.inference.external_provider as ep
monkeypatch.setattr(
ep,
"safe_fetch_remote_image_sync",
lambda *_a, **_k: ("image/png", _jpeg_data_url().partition(",")[2]),
)
msgs = [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": "https://x.example/y.png"},
},
],
}
]
_normalize_anthropic_openai_images(msgs, is_vision = True)
assert msgs[0]["content"][0]["image_url"]["url"] == _jpeg_data_url()
def test_bad_base64_raises_400(self):
msgs = [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": "data:image/jpeg;base64,!!!not-b64!!!"},
},
],
}
]
with pytest.raises(HTTPException) as exc:
_normalize_anthropic_openai_images(msgs, is_vision = True)
assert exc.value.status_code == 400
class TestAnthropicRequestedStudioTools:
def test_recognizes_server_tool_by_type(self):
tools = [{"type": "web_search_20250305", "name": "web_search"}]
assert _anthropic_requested_studio_tools(tools) == {"web_search"}
@pytest.mark.parametrize(
"tool_type",
[
"web_search",
"web_search_20250305",
"web_search_20260209",
"web_search_20260318",
"web_fetch",
"web_fetch_20250910",
"web_fetch_20260209",
"web_fetch_20260309",
"web_fetch_20260318",
],
)
def test_recognizes_every_web_server_tool_version(self, tool_type):
tools = [{"type": tool_type, "name": tool_type.split("_2")[0]}]
assert _anthropic_requested_studio_tools(tools) == {"web_search"}
def test_recognizes_read_skill_server_tool_by_type(self):
tools = [{"type": "read_skill", "name": "read_skill"}]
assert _anthropic_requested_studio_tools(tools) == {"read_skill"}
def test_create_skill_is_not_available_without_a_confirmation_channel(self):
tools = [{"type": "create_skill", "name": "create_skill"}]
assert _anthropic_requested_studio_tools(tools) == set()
def test_bare_name_without_type_is_not_treated_as_server_tool(self):
# Server tools dispatch by `type`; bare-name matching could flip modes.
tools = [{"name": "python"}]
assert _anthropic_requested_studio_tools(tools) == set()
def test_client_tool_named_python_is_not_misclassified(self):
tools = [
{
"name": "python",
"description": "user's own python",
"input_schema": {"type": "object"},
}
]
assert _anthropic_requested_studio_tools(tools) == set()
def test_mixed_request_only_extracts_server_tools(self):
tools = [
{"type": "web_search_20250305", "name": "web_search"},
{"name": "custom_tool", "input_schema": {"type": "object"}},
]
assert _anthropic_requested_studio_tools(tools) == {"web_search"}
def test_pydantic_model_input(self):
tools = [
AnthropicTool(type = "web_fetch_20250910", name = "web_fetch"),
AnthropicTool(name = "x", input_schema = {"type": "object"}),
]
assert _anthropic_requested_studio_tools(tools) == {"web_search"}
def test_empty_and_none(self):
assert _anthropic_requested_studio_tools(None) == set()
assert _anthropic_requested_studio_tools([]) == set()
def _mock_backend(monkeypatch, **overrides):
"""Stubs the backend so generation methods record which path the route entered, then yield one event."""
import routes.inference as inf_mod
# Pinned off so prompt assertions do not depend on the host setting.
monkeypatch.setattr(inf_mod, "current_date_prompt_line", lambda **_kwargs: "")
calls = []
def _gen_plain(**kwargs):
calls.append(("plain", kwargs))
yield "ok"
def _gen_tools(**kwargs):
calls.append(("tools", kwargs))
yield {"type": "content", "text": "ok"}
backend = SimpleNamespace(
is_loaded = True,
is_vision = False,
supports_tools = True,
model_identifier = "test-model",
context_length = 4096,
count_chat_tokens = lambda *args, **kwargs: 2,
generate_chat_completion = _gen_plain,
generate_chat_completion_with_tools = _gen_tools,
calls = calls,
)
backend.__dict__.update(overrides)
monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)
return backend
def _drive(coro):
return asyncio.new_event_loop().run_until_complete(coro)
def _basic_payload(**fields) -> AnthropicMessagesRequest:
base = {
"max_tokens": 16,
"messages": [{"role": "user", "content": "hi"}],
}
base.update(fields)
return AnthropicMessagesRequest(**base)
@pytest.fixture(autouse = True)
def _reset_policy():
reset_tool_policy()
yield
reset_tool_policy()
@pytest.fixture(autouse = True)
def _reset_admission_queues():
# Admission queue is process-global; reset so leftovers cannot stall tests.
from core.inference.llama_admission import reset_llama_admission_queues
reset_llama_admission_queues()
yield
reset_llama_admission_queues()
class TestAnthropicMessagesToolRouting:
class _Request:
state = SimpleNamespace()
url = SimpleNamespace(path = "/v1/messages")
method = "POST"
async def is_disconnected(self):
return False
@staticmethod
def _consume_response(response):
async def _consume():
chunks = []
async for chunk in response.body_iterator:
chunks.append(chunk)
return chunks
return _drive(_consume())
def test_plain_non_streaming_states_the_current_date(self, monkeypatch):
import routes.inference as inf_mod
backend = _mock_backend(monkeypatch, context_length = 2048)
monkeypatch.setattr(
inf_mod,
"current_date_prompt_line",
lambda **_kwargs: "The current date is 2026-08-15.",
)
_drive(anthropic_messages(_basic_payload(), request = self._Request(), current_subject = "t"))
[(_path, kwargs)] = backend.calls
assert kwargs["messages"][0] == {
"role": "system",
"content": "The current date is 2026-08-15.",
}
@pytest.mark.parametrize(
("extra", "expected_path"),
[
({}, "plain"),
({"enable_tools": True, "permission_mode": "off"}, "tools"),
],
ids = ["plain", "server-tools"],
)
def test_seed_reaches_internal_anthropic_generation(self, monkeypatch, extra, expected_path):
backend = _mock_backend(monkeypatch)
_drive(
anthropic_messages(
_basic_payload(seed = 3407, **extra),
request = self._Request(),
current_subject = "t",
)
)
[(path, kwargs)] = backend.calls
assert path == expected_path
assert kwargs["seed"] == 3407
@pytest.mark.parametrize("stream", [False, True])
def test_seed_reaches_anthropic_client_tool_passthrough(self, monkeypatch, stream):
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
_mock_backend(monkeypatch)
captured = {}
async def _passthrough(*args, **kwargs):
captured.update(kwargs)
return JSONResponse({"type": "message", "content": []})
helper = (
"_anthropic_passthrough_stream" if stream else "_anthropic_passthrough_non_streaming"
)
monkeypatch.setattr(inf_mod, helper, _passthrough)
payload = _basic_payload(
seed = 3407,
stream = stream,
tools = [
{
"name": "lookup",
"description": "Look something up",
"input_schema": {"type": "object"},
}
],
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert captured["seed"] == 3407
@pytest.mark.parametrize("stream", [False, True])
def test_client_tool_passthrough_sends_the_resolved_preserve_thinking(
self, monkeypatch, stream
):
# Claude Code brings its own tools, so its turns ride the passthrough: a thinking-off
# follow-up must replay the empty think blocks generation would, or every turn re-reads.
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
_mock_backend(
monkeypatch,
_replay_preserve_thinking = lambda preserve, messages, enable, effort: (
True if enable is False and preserve is not True else preserve
),
)
captured = {}
async def _passthrough(*args, **kwargs):
captured.update(kwargs)
return JSONResponse({"type": "message", "content": []})
helper = (
"_anthropic_passthrough_stream" if stream else "_anthropic_passthrough_non_streaming"
)
monkeypatch.setattr(inf_mod, helper, _passthrough)
payload = _basic_payload(
stream = stream,
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
{"role": "user", "content": "again"},
],
thinking = {"type": "disabled"},
tools = [{"name": "lookup", "input_schema": {"type": "object"}}],
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert captured["preserve_thinking"] is True
_BUDGET_CASES = [
({"thinking": {"type": "enabled", "budget_tokens": 128}}, 128),
({"thinking": {"type": "adaptive", "budget_tokens": 128}}, 128),
({"thinking": {"type": "enabled"}}, None),
({"thinking": {"type": "enabled", "budget_tokens": 0}}, None),
({"thinking": {"type": "disabled", "budget_tokens": 128}}, None),
({"thinking": {"type": "enabled", "budget_tokens": 128}, "enable_thinking": False}, None),
({"thinking": {"type": "enabled", "budget_tokens": 128}, "reasoning_effort": "none"}, None),
({}, None),
]
@pytest.mark.parametrize(("fields", "expected"), _BUDGET_CASES)
@pytest.mark.parametrize(
("extra", "expected_path"),
[
({}, "plain"),
({"enable_tools": True, "permission_mode": "off"}, "tools"),
],
ids = ["plain", "server-tools"],
)
def test_thinking_budget_reaches_internal_anthropic_generation(
self, monkeypatch, extra, expected_path, fields, expected
):
backend = _mock_backend(monkeypatch)
_drive(
anthropic_messages(
_basic_payload(**fields, **extra),
request = self._Request(),
current_subject = "t",
)
)
[(path, kwargs)] = backend.calls
assert path == expected_path
assert kwargs.get("thinking_budget_tokens") == expected
@pytest.mark.parametrize(("fields", "expected"), _BUDGET_CASES)
@pytest.mark.parametrize("stream", [False, True])
def test_thinking_budget_reaches_anthropic_client_tool_passthrough(
self, monkeypatch, stream, fields, expected
):
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
_mock_backend(monkeypatch)
captured = {}
async def _passthrough(*args, **kwargs):
captured.update(kwargs)
return JSONResponse({"type": "message", "content": []})
helper = (
"_anthropic_passthrough_stream" if stream else "_anthropic_passthrough_non_streaming"
)
monkeypatch.setattr(inf_mod, helper, _passthrough)
payload = _basic_payload(
stream = stream,
tools = [{"name": "lookup", "input_schema": {"type": "object"}}],
**fields,
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert captured.get("thinking_budget_tokens") == expected
@pytest.mark.parametrize(
("env", "field", "expected"),
[
(None, None, None),
("truncate_oldest", None, "truncate_oldest"),
(None, "truncate_middle", "truncate_middle"),
("truncate_oldest", "error", None),
],
)
@pytest.mark.parametrize("stream", [False, True])
def test_context_overflow_reaches_anthropic_client_tool_passthrough(
self, monkeypatch, stream, env, field, expected
):
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
if env is None:
monkeypatch.delenv("UNSLOTH_CONTEXT_OVERFLOW", raising = False)
else:
monkeypatch.setenv("UNSLOTH_CONTEXT_OVERFLOW", env)
_mock_backend(monkeypatch)
captured = {}
async def _passthrough(*args, **kwargs):
captured.update(kwargs)
return JSONResponse({"type": "message", "content": []})
helper = (
"_anthropic_passthrough_stream" if stream else "_anthropic_passthrough_non_streaming"
)
monkeypatch.setattr(inf_mod, helper, _passthrough)
payload = _basic_payload(
stream = stream,
tools = [{"name": "lookup", "input_schema": {"type": "object"}}],
**({} if field is None else {"context_overflow": field}),
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert captured.get("context_overflow") == expected
@pytest.mark.parametrize("budget", [None, 128])
@pytest.mark.parametrize("stream", [False, True])
def test_passthrough_puts_thinking_budget_on_the_llama_server_body(
self, monkeypatch, stream, budget
):
import routes.inference as inf_mod
real_builder = inf_mod._build_passthrough_payload
bodies = []
def _builder(*args, **kwargs):
bodies.append(real_builder(*args, **kwargs))
raise RuntimeError("body built")
monkeypatch.setattr(inf_mod, "_build_passthrough_payload", _builder)
backend = SimpleNamespace(base_url = "http://llama.test", context_length = 4096)
messages = [{"role": "user", "content": "hi"}]
common = (messages, [], 0.7, 0.95, 20, 16, "msg_1", "test-model")
if stream:
coro = inf_mod._anthropic_passthrough_stream(
self._Request(), threading.Event(), backend, *common, thinking_budget_tokens = budget
)
else:
coro = inf_mod._anthropic_passthrough_non_streaming(
backend, *common, thinking_budget_tokens = budget
)
with pytest.raises(RuntimeError, match = "body built"):
_drive(coro)
[body] = bodies
assert body.get("thinking_budget_tokens") == budget
def test_client_tool_catalog_without_passthrough_is_rejected(self, monkeypatch):
backend = _mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
payload = _basic_payload(tools = [{"name": "lookup", "input_schema": {"type": "object"}}])
with pytest.raises(HTTPException) as excinfo:
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert excinfo.value.status_code == 400
assert excinfo.value.detail["error"]["type"] == "invalid_request_error"
assert "does not advertise tools" in excinfo.value.detail["error"]["message"]
assert backend.calls == []
def test_replayed_tool_history_without_passthrough_still_answers(self, monkeypatch):
backend = _mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
payload = _basic_payload(
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "t1", "name": "lookup", "input": {}}],
},
_tool_result_turn(role = "user"),
]
)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert response.status_code == 200
[(path, kwargs)] = backend.calls
assert path == "plain"
assert not any(m.get("role") == "tool" for m in kwargs["messages"])
assert any(
m.get("role") == "user" and "tool_response" in (m.get("content") or "")
for m in kwargs["messages"]
)
def test_disabled_tool_choice_still_answers_without_passthrough(self, monkeypatch):
backend = _mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
payload = _basic_payload(
tools = [{"name": "lookup", "input_schema": {"type": "object"}}],
tool_choice = {"type": "none"},
)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert response.status_code == 200
[(path, _kwargs)] = backend.calls
assert path == "plain"
def test_a_plain_turn_survives_a_backend_whose_supports_tools_raises(self, monkeypatch):
from routes.inference import anthropic_count_tokens
import routes.inference as inf_mod
class _Raising:
is_loaded = True
is_vision = False
model_identifier = "half-ready"
context_length = 4096
@property
def supports_tools(self):
raise RuntimeError("not ready")
def count_chat_tokens(self, *args, **kwargs):
return 2
def generate_chat_completion(self, **kwargs):
yield "ok"
def generate_chat_completion_with_tools(self, **kwargs):
yield {"type": "content", "text": "ok"}
monkeypatch.setattr(inf_mod, "current_date_prompt_line", lambda **_kwargs: "")
monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: _Raising())
assert (
_drive(
anthropic_messages(_basic_payload(), request = self._Request(), current_subject = "t")
).status_code
== 200
)
assert (
_drive(
anthropic_count_tokens(
_basic_payload(), request = self._Request(), current_subject = "t"
)
).status_code
== 200
)
def test_count_tokens_prices_the_preserve_thinking_generation_resolves(self, monkeypatch):
from routes.inference import anthropic_count_tokens
import routes.inference as inf_mod
seen: list = []
class _Backend:
is_loaded = True
is_vision = False
model_identifier = "qwen"
context_length = 4096
supports_tools = False
supports_tool_passthrough = False
def _request_reasoning_kwargs(
self, enable_thinking, reasoning_effort, preserve_thinking
):
return {"enable_thinking": enable_thinking, "preserve_thinking": preserve_thinking}
def _replay_preserve_thinking(
self,
preserve,
messages,
enable_thinking = None,
effort = None,
):
return True if preserve is False and enable_thinking is False else preserve
def count_chat_tokens(
self,
*args,
chat_template_kwargs = None,
**kwargs,
):
seen.append(chat_template_kwargs)
return 2
monkeypatch.setattr(inf_mod, "current_date_prompt_line", lambda **_kwargs: "")
monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: _Backend())
payload = _basic_payload(
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
{"role": "user", "content": "again"},
],
thinking = {"type": "disabled"},
preserve_thinking = False,
)
response = _drive(
anthropic_count_tokens(payload, request = self._Request(), current_subject = "t")
)
assert response.status_code == 200
assert seen == [{"enable_thinking": False, "preserve_thinking": True}]
def test_count_tokens_rejects_the_catalog_messages_rejects(self, monkeypatch):
# The template renders no schemas, so the count ignored them.
from routes.inference import anthropic_count_tokens
_mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
payload = _basic_payload(tools = [{"name": "lookup", "input_schema": {"type": "object"}}])
with pytest.raises(HTTPException) as excinfo:
_drive(anthropic_count_tokens(payload, request = self._Request(), current_subject = "t"))
assert excinfo.value.status_code == 400
assert "does not advertise tools" in excinfo.value.detail["error"]["message"]
@pytest.mark.parametrize(
"fields",
[
{},
{
"tools": [{"name": "lookup", "input_schema": {"type": "object"}}],
"tool_choice": {"type": "none"},
},
{
"messages": [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": "t1", "name": "lookup", "input": {}}
],
},
_tool_result_turn(role = "user"),
]
},
],
ids = ["no_tools", "tool_choice_none", "replayed_history"],
)
def test_count_tokens_still_counts_what_messages_still_answers(self, monkeypatch, fields):
from routes.inference import anthropic_count_tokens
_mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
response = _drive(
anthropic_count_tokens(
_basic_payload(**fields), request = self._Request(), current_subject = "t"
)
)
assert response.status_code == 200
@pytest.mark.parametrize(
"tool_choice, expected",
[
({"type": "tool", "name": "other"}, ["other"]),
({"type": "tool", "name": "missing"}, ["lookup", "other"]),
({"type": "auto"}, ["lookup", "other"]),
],
ids = ["forced", "forced_missing", "auto"],
)
def test_count_tokens_prices_the_catalog_a_forced_tool_sends(
self, monkeypatch, tool_choice, expected
):
from routes.inference import anthropic_count_tokens
seen = {}
def _count(messages, system, tools, **kwargs):
seen["tools"] = tools
return 2
_mock_backend(monkeypatch, supports_tool_passthrough = True, count_chat_tokens = _count)
payload = _basic_payload(
tools = [
{"name": name, "input_schema": {"type": "object"}} for name in ("lookup", "other")
],
tool_choice = tool_choice,
)
response = _drive(
anthropic_count_tokens(payload, request = self._Request(), current_subject = "t")
)
assert response.status_code == 200
assert [t["function"]["name"] for t in seen["tools"]] == expected
def test_replayed_thinking_keeps_the_callers_preserve_setting(self, monkeypatch):
# Conversion drops thinking it does not preserve, so the resolver must not read that
# history as reasoning-free and swap real thoughts for empty think blocks.
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
_mock_backend(
monkeypatch,
_replay_preserve_thinking = lambda preserve, messages, enable, effort: True,
)
captured = {}
async def _passthrough(*args, **kwargs):
captured.update(kwargs)
return JSONResponse({"type": "message", "content": []})
monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _passthrough)
payload = _basic_payload(
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "Greet back.", "signature": "s"},
{"type": "text", "text": "hello"},
],
},
{"role": "user", "content": "again"},
],
thinking = {"type": "disabled"},
tools = [{"name": "lookup", "input_schema": {"type": "object"}}],
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert captured["preserve_thinking"] is not True
@pytest.mark.parametrize(
("extra", "expected_path"),
[({}, "plain"), ({"enable_tools": True, "permission_mode": "off"}, "tools")],
ids = ["plain", "server-tools"],
)
@pytest.mark.parametrize("replays_thinking", [True, False])
def test_generation_skips_the_replay_resolver_only_when_thinking_was_dropped(
self, monkeypatch, extra, expected_path, replays_thinking
):
# The generator resolves from the converted history, where dropped thinking reads as none.
backend = _mock_backend(monkeypatch)
answer = [{"type": "text", "text": "hello"}]
if replays_thinking:
answer.insert(0, {"type": "thinking", "thinking": "Greet back.", "signature": "s"})
payload = _basic_payload(
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": answer},
{"role": "user", "content": "again"},
],
thinking = {"type": "disabled"},
**extra,
)
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
[(path, kwargs)] = backend.calls
assert path == expected_path
assert kwargs["replay_preserve_thinking"] is (not replays_thinking)
def test_the_admission_recount_keeps_the_callers_preserve_when_thinking_was_dropped(self):
from routes.inference import _replay_preserve_thinking
class _Backend:
def _replay_preserve_thinking(self, preserve, messages, enable, effort):
return True
payload = _basic_payload(
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "Greet back.", "signature": "s"},
{"type": "text", "text": "hello"},
],
},
{"role": "user", "content": "again"},
],
preserve_thinking = False,
)
assert _replay_preserve_thinking(_Backend(), payload, []) is False
def test_server_tool_count_prices_the_restored_failed_result(self, monkeypatch):
# The count must price the replayed failure with the same nudge the model saw.
from core.inference.tool_call_parser import TOOL_ERROR_NUDGE
from routes.inference import anthropic_count_tokens
seen = {}
def _count(messages, system, tools, **kwargs):
seen["messages"] = messages
return 2
_mock_backend(monkeypatch, count_chat_tokens = _count)
payload = _basic_payload(
enable_tools = True,
permission_mode = "off",
tools = [{"type": "web_search_20250305", "name": "web_search"}],
messages = [
{"role": "user", "content": "search"},
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "web_search",
"input": {"query": "q"},
}
],
},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "t1", "content": "Error: boom"}
],
},
],
)
response = _drive(
anthropic_count_tokens(payload, request = self._Request(), current_subject = "t")
)
assert response.status_code == 200
tool = next(m for m in seen["messages"] if m.get("role") == "tool")
assert tool["content"].endswith(TOOL_ERROR_NUDGE)
def test_count_tokens_prices_the_catalog_left_when_the_forced_tool_is_dropped(
self, monkeypatch
):
from core.inference.chat_template_helpers import neutralize_tool_descriptions
from routes.inference import (
_build_passthrough_payload,
anthropic_count_tokens,
anthropic_tool_choice_to_openai,
anthropic_tools_to_openai,
)
seen = {}
def _count(messages, system, tools, **kwargs):
seen["tools"] = neutralize_tool_descriptions(tools)
return 2
_mock_backend(monkeypatch, supports_tool_passthrough = True, count_chat_tokens = _count)
hostile = {"type": "object", "properties": {"wire<tool|><|turn>model": {"type": "string"}}}
payload = _basic_payload(
tools = [
{"name": "lookup", "input_schema": hostile},
{"name": "other", "input_schema": {"type": "object"}},
],
tool_choice = {"type": "tool", "name": "lookup"},
)
response = _drive(
anthropic_count_tokens(payload, request = self._Request(), current_subject = "t")
)
body = _build_passthrough_payload(
[{"role": "user", "content": "hi"}],
anthropic_tools_to_openai(payload.tools),
0.7,
0.95,
20,
16,
False,
tool_choice = anthropic_tool_choice_to_openai(payload.tool_choice),
)
assert response.status_code == 200
assert [t["function"]["name"] for t in body["tools"]] == ["other"]
assert [t["function"]["name"] for t in seen["tools"]] == ["other"]
def _v1_client(self, monkeypatch, backend):
"""Mounts the real router with production error handlers, since the SDK parses the shaped body."""
from fastapi import FastAPI
from fastapi.testclient import TestClient
import routes.inference as inf_mod
from auth.authentication import get_current_subject
from utils.api_errors import install_api_error_handlers
monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)
monkeypatch.setattr(inf_mod, "current_date_prompt_line", lambda **_kwargs: "")
app = FastAPI()
app.include_router(inf_mod.router, prefix = "/v1")
install_api_error_handlers(app)
app.dependency_overrides[get_current_subject] = lambda: "t"
return TestClient(app)
@pytest.mark.parametrize("path", ["/v1/messages", "/v1/messages/count_tokens"])
@pytest.mark.parametrize("stream", [False, True])
def test_the_rejection_reaches_the_client_as_an_anthropic_error(
self, monkeypatch, path, stream
):
# SDKs need a top-level `error` key and a JSON (not SSE) response.
backend = _mock_backend(monkeypatch, supports_tools = False, supports_tool_passthrough = False)
client = self._v1_client(monkeypatch, backend)
resp = client.post(
path,
json = {
"model": "test-model",
"max_tokens": 16,
"messages": [{"role": "user", "content": "hi"}],
"tools": [{"name": "lookup", "input_schema": {"type": "object"}}],
"stream": stream,
},
)
assert resp.status_code == 400
assert resp.headers["content-type"].startswith("application/json")
body = resp.json()
assert "detail" not in body
assert body["type"] == "error"
assert body["error"]["type"] == "invalid_request_error"
assert "does not advertise tools" in body["error"]["message"]
assert backend.calls == []
def test_plain_non_streaming_records_api_monitor_entry(self, monkeypatch):
import routes.inference as inf_mod
_mock_backend(monkeypatch, context_length = 2048)
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
payload = _basic_payload()
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert response.status_code == 200
[entry] = monitor.snapshot()
assert entry["endpoint"] == "/v1/messages"
assert entry["status"] == "completed"
assert entry["model"] == "test-model"
assert entry["prompt_preview"] == "user: hi"
assert entry["reply_preview"] == "ok"
assert entry["context_length"] == 2048
assert monitor.active_count() == 0
@pytest.mark.parametrize("with_tools", [False, True])
def test_non_streaming_disconnect_stops_generation_and_records_it_cancelled(
self, monkeypatch, with_tools
):
import routes.inference as inf_mod
total = 200
emitted = []
started = threading.Event()
class _LeavingRequest:
state = SimpleNamespace()
url = SimpleNamespace(path = "/v1/messages")
method = "POST"
async def is_disconnected(self):
return started.is_set()
def _emit(kwargs, event):
cancel_event = kwargs["cancel_event"]
for _ in range(total):
if cancel_event.wait(0.005):
return
emitted.append(1)
started.set()
yield event
def _gen_plain(**kwargs):
text = ""
for _ in _emit(kwargs, None):
text += "x"
yield text
def _gen_tools(**kwargs):
yield from _emit(kwargs, {"type": "content", "text": "x"})
_mock_backend(
monkeypatch,
generate_chat_completion = _gen_plain,
generate_chat_completion_with_tools = _gen_tools,
)
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
fields = (
{
"enable_tools": True,
"tools": [{"type": "web_search_20250305", "name": "web_search"}],
}
if with_tools
else {}
)
response = _drive(
anthropic_messages(
_basic_payload(**fields), request = _LeavingRequest(), current_subject = "t"
)
)
assert response.status_code == 200
assert len(emitted) < total, "generation ran to completion after the client left"
[entry] = monitor.snapshot()
assert entry["status"] == "cancelled"
# A cancelled run has no natural stop reason; end_turn would read as a full answer.
assert entry["stop_reason"] is None
assert monitor.active_count() == 0
@pytest.mark.parametrize("stream", [False, True])
@pytest.mark.parametrize("with_tools", [False, True])
def test_reasoning_only_output_is_not_duplicated(self, monkeypatch, stream, with_tools):
reasoning = "The capital of France is Paris."
def _gen_plain(**kwargs):
assert kwargs["promote_reasoning_only"] is False
prov = kwargs.get("reasoning_provenance")
if prov is not None:
prov["wrapped"] = prov.get("wrapped", 0) + 1
yield f"<think>{reasoning}"
yield f"<think>{reasoning}</think>"
def _gen_tools(**kwargs):
assert kwargs["promote_reasoning_only"] is False
prov = kwargs.get("reasoning_provenance")
if prov is not None:
prov["wrapped"] = prov.get("wrapped", 0) + 1
yield {"type": "content", "text": f"<think>{reasoning}"}
yield {"type": "content", "text": f"<think>{reasoning}</think>"}
_mock_backend(
monkeypatch,
generate_chat_completion = _gen_plain,
generate_chat_completion_with_tools = _gen_tools,
)
payload_fields = {"stream": stream}
if with_tools:
payload_fields.update(
{
"enable_tools": True,
"tools": [{"type": "web_search_20250305", "name": "web_search"}],
}
)
payload = _basic_payload(**payload_fields)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
if stream:
body = self._sse_blob(self._consume_response(response))
assert body.count(reasoning) == 1
assert "<think>" not in body
else:
body = json.loads(response.body)
assert body["content"][0]["type"] == "thinking"
assert body["content"][0]["thinking"] == reasoning
@pytest.mark.parametrize("stream", [False, True])
def test_literal_leading_think_without_provenance_stays_text(self, monkeypatch, stream):
literal = "<think>like this</think>"
def _gen_plain(**kwargs):
assert kwargs.get("reasoning_provenance") is not None
yield literal
_mock_backend(monkeypatch, generate_chat_completion = _gen_plain)
payload = _basic_payload(stream = stream)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
if stream:
body = self._sse_blob(self._consume_response(response))
assert '"thinking_delta"' not in body
assert literal in body
else:
body = json.loads(response.body)
assert body["content"][0]["type"] == "text"
assert body["content"][0]["text"] == literal
def test_tool_use_non_streaming_records_api_monitor_reply(self, monkeypatch):
import routes.inference as inf_mod
def _gen_tools(**_kwargs):
yield {
"type": "tool_start",
"tool_call_id": "call_1",
"tool_name": "lookup",
"arguments": {"query": "weather"},
}
_mock_backend(
monkeypatch,
context_length = 2048,
generate_chat_completion_with_tools = _gen_tools,
)
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
payload = _basic_payload(
enable_tools = True,
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert response.status_code == 200
[entry] = monitor.snapshot()
assert entry["status"] == "completed"
assert entry["reply_preview"] == 'Tool call: lookup({"query": "weather"})'
def test_client_tool_non_streaming_expected_cancel_returns_anthropic_499(self, monkeypatch):
import routes.inference as inf_mod
async def _cancelled(*_args, **_kwargs):
raise inf_mod._NonStreamingRequestCancelled("Request cancelled.")
_mock_backend(monkeypatch, base_url = "http://llama.test")
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _cancelled)
payload = _basic_payload(
tools = [
{
"name": "lookup",
"description": "Look something up",
"input_schema": {"type": "object", "properties": {}},
}
]
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert exc.value.status_code == 499
assert exc.value.detail["type"] == "error"
[entry] = monitor.snapshot()
assert entry["status"] == "cancelled"
assert monitor.active_count() == 0
def test_plain_streaming_records_active_and_completed_monitor_entry(self, monkeypatch):
import routes.inference as inf_mod
_mock_backend(monkeypatch, context_length = 2048)
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
payload = _basic_payload(stream = True)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
assert monitor.active_count() == 1
self._consume_response(response)
[entry] = monitor.snapshot()
assert entry["status"] == "completed"
assert entry["reply_preview"] == "ok"
assert entry["prompt_tokens"] == 2
assert entry["context_length"] == 2048
assert monitor.active_count() == 0
def test_plain_streaming_pre_response_cancel_finalizes_monitor(self, monkeypatch):
import routes.inference as inf_mod
async def _cancelled_before_response(*_args, **_kwargs):
raise asyncio.CancelledError()
_mock_backend(monkeypatch, context_length = 2048)
monitor = ApiMonitor(max_entries = 3)
monkeypatch.setattr(inf_mod, "api_monitor", monitor)
monkeypatch.setattr(inf_mod, "_anthropic_plain_stream", _cancelled_before_response)
payload = _basic_payload(stream = True)
with pytest.raises(asyncio.CancelledError):
_drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
[entry] = monitor.snapshot()
assert entry["status"] == "cancelled"
assert monitor.active_count() == 0
@staticmethod
def _sse_blob(chunks):
# StreamingResponse may hand back str or already-encoded bytes.
return "".join(c.decode() if isinstance(c, (bytes, bytearray)) else c for c in chunks)
def test_plain_streaming_unclassified_error_emits_error_event(self, monkeypatch):
def _gen_boom(**_kwargs):
yield "partial"
raise RuntimeError("llama-server crashed mid-decode")
_mock_backend(monkeypatch, generate_chat_completion = _gen_boom)
payload = _basic_payload(stream = True)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
blob = self._sse_blob(self._consume_response(response))
assert "event: error" in blob
assert '"type": "error"' in blob
assert "event: message_stop" not in blob
def test_tool_streaming_unclassified_error_emits_error_event(self, monkeypatch):
def _gen_tools_boom(**_kwargs):
yield {"type": "content", "text": "partial"}
raise RuntimeError("llama-server crashed mid-decode")
_mock_backend(monkeypatch, generate_chat_completion_with_tools = _gen_tools_boom)
payload = _basic_payload(
stream = True,
enable_tools = True,
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
response = _drive(anthropic_messages(payload, request = self._Request(), current_subject = "t"))
blob = self._sse_blob(self._consume_response(response))
assert "event: error" in blob
assert '"type": "error"' in blob
assert "event: message_stop" not in blob
@pytest.mark.parametrize(
"tool_type", ["web_search_20250305", "web_search_20260209", "web_search_20260318"]
)
def test_mixed_server_and_client_tools_rejected_with_400(self, monkeypatch, tool_type):
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{"type": tool_type, "name": "web_search"},
{"name": "custom", "input_schema": {"type": "object"}},
],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "Mixing Anthropic server tools" in exc.value.detail
def test_explicit_server_loop_and_client_tools_rejected_with_400(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(
enable_tools = True,
tools = [{"name": "Write", "input_schema": {"type": "object"}}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "Mixing Anthropic server tools" in exc.value.detail
def test_explicit_server_loop_and_schema_client_tools_rejected_with_400(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(
enable_tools = True,
tools = [{"type": "bash_20250124", "name": "bash"}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "Mixing Anthropic server tools" in exc.value.detail
def test_process_tool_policy_does_not_steal_schema_client_tools(self, monkeypatch):
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
backend = _mock_backend(monkeypatch)
captured = {}
async def _passthrough(*args, **kwargs):
captured["tools"] = args[2]
return JSONResponse(
{
"id": "msg_test",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "ok"}],
"model": "test-model",
"stop_reason": "end_turn",
"stop_sequence": None,
"usage": {"input_tokens": 1, "output_tokens": 1},
}
)
monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _passthrough)
set_tool_policy(True)
payload = _basic_payload(tools = [{"type": "bash_20250124", "name": "bash"}])
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls == []
assert captured["tools"][0]["function"]["name"] == "bash"
@pytest.mark.parametrize("permission_mode", [None, "ask"])
@pytest.mark.parametrize(
("tool_policy", "enable_tools"),
[(True, None), (False, True)],
)
def test_process_tool_policy_does_not_steal_client_tools(
self, monkeypatch, permission_mode, tool_policy, enable_tools
):
"""A server-wide tool default must not replace Claude Code's own tools."""
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
backend = _mock_backend(monkeypatch)
captured = {}
async def _passthrough(*args, **kwargs):
captured["tools"] = args[2]
return JSONResponse(
{
"id": "msg_test",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "ok"}],
"model": "test-model",
"stop_reason": "end_turn",
"stop_sequence": None,
"usage": {"input_tokens": 1, "output_tokens": 1},
}
)
monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _passthrough)
set_tool_policy(tool_policy)
fields = {
"tools": [
{
"name": "Write",
"description": "Write a file",
"input_schema": {
"type": "object",
"properties": {"path": {"type": "string"}},
},
}
],
}
if enable_tools is not None:
fields["enable_tools"] = enable_tools
if permission_mode is not None:
fields["permission_mode"] = permission_mode
payload = _basic_payload(**fields)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls == []
assert captured["tools"][0]["function"]["name"] == "Write"
def test_mixed_rejected_when_client_tool_name_collides_with_server_alias(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{"type": "web_search_20250305", "name": "web_search"},
{"name": "web_search", "input_schema": {"type": "object"}},
],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "Mixing Anthropic server tools" in exc.value.detail
def test_client_tool_missing_input_schema_rejected_with_400(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"name": "my_tool", "description": "oops, schema typo"}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "input_schema" in exc.value.detail
def test_client_tool_missing_name_rejected_with_400(self, monkeypatch):
# A nameless client tool would be silently dropped; reject at the boundary.
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"input_schema": {"type": "object"}}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "name" in exc.value.detail
def test_schema_client_tool_missing_name_rejected_with_400(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(tools = [{"type": "bash_20250124"}])
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "name" in exc.value.detail
def test_client_tool_empty_name_rejected_with_400(self, monkeypatch):
# `name: ""` is dropped by the `if not name` guard; reject at the boundary.
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"name": "", "input_schema": {"type": "object"}}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "name" in exc.value.detail
def test_alias_named_client_tool_without_schema_rejected_with_400(self, monkeypatch):
_mock_backend(monkeypatch)
payload = _basic_payload(tools = [{"name": "python"}])
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "input_schema" in exc.value.detail
def test_unrecognized_server_tool_accepted_as_noop(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"type": "code_execution_20250825", "name": "code_execution"}],
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "plain"
def test_disable_tools_policy_overrides_server_tool_alias(self, monkeypatch):
backend = _mock_backend(monkeypatch)
set_tool_policy(False)
payload = _basic_payload(
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "plain"
@pytest.mark.parametrize(
"tool_type", ["web_search_20250305", "web_search_20260209", "web_search_20260318"]
)
def test_server_tool_alias_enters_tool_path_when_policy_unset(self, monkeypatch, tool_type):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"type": tool_type, "name": "web_search"}],
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
call_kind, kwargs = backend.calls[0]
assert call_kind == "tools"
assert [tool["function"]["name"] for tool in kwargs["tools"]] == ["web_search"]
def test_api_server_tool_request_keeps_the_current_date(self, monkeypatch):
import routes.inference as inf_mod
backend = _mock_backend(monkeypatch)
monkeypatch.setattr(
inf_mod,
"current_date_prompt_line",
lambda **_kwargs: "The current date is 2026-08-15.",
)
monkeypatch.setattr(inf_mod, "_request_is_internal_workflow", lambda _request: False)
class ApiRequest(self._Request):
headers = {"authorization": "Bearer sk-unsloth-test"}
state = SimpleNamespace(skip_api_monitor = True)
payload = _basic_payload(
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
_drive(anthropic_messages(payload, request = ApiRequest(), current_subject = "t"))
call_kind, kwargs = backend.calls[0]
assert call_kind == "tools"
assert kwargs["messages"][0]["content"].startswith("The current date is 2026-08-15.\n\n")
def test_server_tool_choice_alias_uses_the_selected_studio_name(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"type": "web_fetch_20250910", "name": "web_fetch"}],
tool_choice = {"type": "tool", "name": "web_fetch"},
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
call_kind, kwargs = backend.calls[0]
assert call_kind == "tools"
assert kwargs["tool_choice"] == {"type": "function", "function": {"name": "web_search"}}
assert [tool["function"]["name"] for tool in kwargs["tools"]] == ["web_search"]
def test_web_search_domain_limits_reach_the_tool_loop(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{
"type": "web_search_20250305",
"name": "web_search",
"allowed_domains": ["Docs.Python.org"],
"blocked_domains": ["reddit.com"],
"max_uses": 8,
}
],
tool_choice = {"type": "tool", "name": "web_search"},
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
call_kind, kwargs = backend.calls[0]
assert call_kind == "tools"
assert kwargs["website_policy"] == {
"allowedDomains": ["docs.python.org"],
"blockedDomains": ["reddit.com"],
}
def test_web_search_domain_entries_with_a_path_limit_by_domain(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{
"type": "web_search_20250305",
"name": "web_search",
"allowed_domains": ["docs.python.org/3/library", "example.com/*"],
}
],
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][1]["website_policy"] == {
"allowedDomains": ["docs.python.org", "example.com"],
"blockedDomains": [],
}
def test_web_search_without_domain_limits_is_unrestricted(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(tools = [{"type": "web_search_20250305", "name": "web_search"}])
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][1]["website_policy"] is None
@pytest.mark.parametrize("allowed", [["https://docs.python.org"], "", {}])
def test_invalid_web_search_domain_is_rejected_before_generation(self, monkeypatch, allowed):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{
"type": "web_search_20250305",
"name": "web_search",
"allowed_domains": allowed,
}
],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert exc.value.detail["error"]["type"] == "invalid_request_error"
assert backend.calls == []
def test_count_tokens_rejects_an_invalid_web_search_domain(self, monkeypatch):
from routes.inference import anthropic_count_tokens
_mock_backend(monkeypatch)
payload = _basic_payload(
tools = [
{
"type": "web_search_20250305",
"name": "web_search",
"allowed_domains": ["https://docs.python.org"],
}
],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_count_tokens(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
def test_server_tool_choice_must_be_in_the_selected_catalog(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
tools = [{"type": "web_search_20250305", "name": "web_search"}],
tool_choice = {"type": "tool", "name": "python"},
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "python" in exc.value.detail["error"]["message"]
assert backend.calls == []
def test_confirm_tool_calls_rejected_for_server_tools(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
confirm_tool_calls = True,
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "confirm_tool_calls is not supported" in exc.value.detail["error"]["message"]
assert backend.calls == []
def test_permission_mode_gating_for_server_tools(self, monkeypatch):
import routes.inference as inf_mod
monkeypatch.setattr(
inf_mod,
"_enabled_agent_skills",
lambda: [{"name": "skill-creator", "description": "Create a skill."}],
)
# ask needs a per-call pause this channel cannot honor.
safe_tools = [{"type": "web_search_20250305", "name": "web_search"}]
backend = _mock_backend(monkeypatch)
payload = _basic_payload(tools = safe_tools, permission_mode = "ask")
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "no confirmation channel" in exc.value.detail["error"]["message"]
assert backend.calls == []
for extra in ({"permission_mode": "auto"}, {}):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(tools = safe_tools, **extra)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "tools"
# Reading this conversation's archive is read-only and must not trip the gate.
for extra in ({"permission_mode": "auto"}, {}):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
enable_tools = True, enabled_tools = ["search_conversation"], **extra
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "tools"
for local_payload in (
_basic_payload(tools = [{"type": "terminal", "name": "terminal"}]),
_basic_payload(
tools = [{"type": "terminal", "name": "terminal"}], permission_mode = "auto"
),
_basic_payload(tools = safe_tools, enable_tools = True, enabled_tools = ["python"]),
):
backend = _mock_backend(monkeypatch)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(local_payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "terminal" in exc.value.detail["error"]["message"]
assert backend.calls == []
# Explicit confirm opt-out wins over the mode, even under ask.
for extra in (
{"tools": safe_tools, "permission_mode": "off"},
{"tools": safe_tools, "permission_mode": "full"},
{"tools": safe_tools, "enabled_tools": ["python"], "confirm_tool_calls": False},
{"tools": safe_tools, "permission_mode": "ask", "confirm_tool_calls": False},
{
"tools": [{"type": "terminal", "name": "terminal"}],
"permission_mode": "ask",
"confirm_tool_calls": False,
},
):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(**extra)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "tools"
def test_the_process_tool_default_alone_is_not_a_server_tool_selection(self, monkeypatch):
"""A process-wide tool default is not a per-request server tool selection, in either direction."""
import routes.inference as inf_mod
from fastapi.responses import JSONResponse
backend = _mock_backend(monkeypatch)
async def _passthrough(*args, **kwargs):
return JSONResponse({"type": "message", "content": []})
monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _passthrough)
set_tool_policy(True)
_drive(anthropic_messages(_basic_payload(), request = None, current_subject = "t"))
assert backend.calls, "a plain chat request must still be served"
path, kwargs = backend.calls[0]
assert path == "plain", (
"a tool-free request took the server-tool loop on the process default alone, so "
"the model can call terminal/python with no confirmation channel"
)
assert not kwargs.get("tools")
backend = _mock_backend(monkeypatch)
_drive(
anthropic_messages(
_basic_payload(enable_tools = True, permission_mode = "off"),
request = None,
current_subject = "t",
)
)
assert backend.calls[0][0] == "tools"
# This route does not wire MCP discovery, so ignore mcp_enabled.
reset_tool_policy()
backend = _mock_backend(monkeypatch)
_drive(
anthropic_messages(
_basic_payload(mcp_enabled = True, permission_mode = "off"),
request = None,
current_subject = "t",
)
)
assert backend.calls[0][0] == "plain"
assert not backend.calls[0][1].get("tools")
for fields in (
{"enable_tools": True},
{"enable_tools": True, "permission_mode": "ask"},
{"tools": [{"type": "terminal", "name": "terminal"}]},
):
with pytest.raises(HTTPException) as exc:
_drive(
anthropic_messages(_basic_payload(**fields), request = None, current_subject = "t")
)
assert exc.value.status_code == 400
assert "no confirmation channel" in exc.value.detail["error"]["message"]
def test_render_html_gated_for_server_tools(self, monkeypatch):
# A networked render_html prompts in auto, which this channel cannot show.
rh = {"enable_tools": True, "enabled_tools": ["render_html"]}
for mode in ("ask", "auto", None):
backend = _mock_backend(monkeypatch)
fields = dict(rh)
if mode is not None:
fields["permission_mode"] = mode
payload = _basic_payload(**fields)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert "no confirmation channel" in exc.value.detail["error"]["message"]
assert backend.calls == []
for extra in (
{"permission_mode": "off"},
{"permission_mode": "full"},
{"confirm_tool_calls": False},
):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(**{**rh, **extra})
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "tools"
def test_permission_mode_rejected_before_auto_switch(self, monkeypatch):
# Reject before _maybe_auto_switch_model so the resident model is not evicted.
import routes.inference as inf_mod
switch_calls = []
async def _rec_switch(*_args, **_kwargs):
switch_calls.append(1)
monkeypatch.setattr(inf_mod, "_maybe_auto_switch_model", _rec_switch)
safe_tools = [{"type": "web_search_20250305", "name": "web_search"}]
local_tools = [{"type": "terminal", "name": "terminal"}]
for payload in (
_basic_payload(tools = safe_tools, permission_mode = "ask"),
_basic_payload(tools = local_tools, permission_mode = "auto"),
_basic_payload(tools = local_tools),
):
switch_calls.clear()
_mock_backend(monkeypatch)
with pytest.raises(HTTPException) as exc:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert exc.value.status_code == 400
assert switch_calls == [], "rejection must precede the auto-switch"
switch_calls.clear()
_mock_backend(monkeypatch)
payload = _basic_payload(tools = safe_tools, permission_mode = "off")
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert switch_calls == [1]
def test_per_request_enable_tools_false_blocks_server_tool_alias(self, monkeypatch):
backend = _mock_backend(monkeypatch)
payload = _basic_payload(
enable_tools = False,
tools = [{"type": "web_search_20250305", "name": "web_search"}],
)
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "plain"
def test_resumed_session_thinking_and_null_content_do_not_400():
# Resumed sessions replay thinking or null content; both must be accepted, not 400.
from pydantic import ValidationError
req = AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{"role": "user", "content": "hi"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "secret reasoning", "signature": "s"},
{"type": "text", "text": "the answer"},
{"type": "tool_use", "id": "t1", "name": "f", "input": {}},
],
},
{"role": "assistant", "content": None},
],
)
assert type(req.messages[1].content[0]).__name__ == "AnthropicThinkingBlock"
assert type(req.messages[1].content[1]).__name__ == "AnthropicTextBlock"
assert req.messages[2].content == ""
openai = anthropic_messages_to_openai([m.model_dump() for m in req.messages])
assistant = next(m for m in openai if m["role"] == "assistant" and m.get("content"))
assert assistant["content"] == "the answer"
# The current turn's thinking rides as reasoning, never as visible content.
assert assistant["reasoning_content"] == "secret reasoning"
assert all("secret reasoning" not in (m.get("content") or "") for m in openai)
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "assistant", "content": [{"type": "tool_use", "name": "f"}]}],
)
def test_user_thinking_block_rejected_not_silently_dropped():
from pydantic import ValidationError
for btype in ("thinking", "redacted_thinking"):
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "user", "content": [{"type": btype}]}],
)
def test_think_markup_split_preserves_text_verbatim():
from routes.inference import _think_markup_to_blocks
[block] = _think_markup_to_blocks(" indented\n lines\n")
assert block.text == " indented\n lines\n"
thinking, text = _think_markup_to_blocks("<think>why</think>\n\n answer\n")
assert thinking.thinking == "why"
assert text.text == "\n\n answer\n"
[only_thinking] = _think_markup_to_blocks("<think>why</think>\n\n")
assert only_thinking.thinking == "why"
def test_user_null_content_rejected():
from pydantic import ValidationError
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "user", "content": None}],
)
def test_user_unknown_block_rejected_not_silently_dropped():
# A user turn with only unknown blocks would forward nothing; reject it.
from pydantic import ValidationError
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{"role": "user", "content": [{"type": "container_upload", "file_id": "f"}]},
],
)
@pytest.mark.parametrize(
"source",
[
{"type": "base64", "media_type": "application/pdf", "data": "JVBERi0="},
{"type": "url", "url": "https://example.com/a.pdf"},
{"type": "file", "file_id": "file_1"},
],
)
def test_user_unreadable_document_rejected_outside_tool_results(source):
from pydantic import ValidationError
document = {"type": "document", "source": source}
with pytest.raises(ValidationError, match = "unsupported document source type"):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "user", "content": [{"type": "text", "text": "Read"}, document]}],
)
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "t1", "content": [document]}],
}
],
)
def test_user_translatable_blocks_still_accepted():
req = AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is this?"},
{
"type": "image",
"source": {"type": "base64", "media_type": "image/png", "data": "AA"},
},
{"type": "tool_result", "tool_use_id": "t1", "content": "ok"},
],
}
],
)
assert [type(b).__name__ for b in req.messages[0].content] == [
"AnthropicTextBlock",
"AnthropicImageBlock",
"AnthropicToolResultBlock",
]
openai = anthropic_messages_to_openai([m.model_dump() for m in req.messages])
assert any(m["role"] == "tool" and m["tool_call_id"] == "t1" for m in openai)
def test_user_malformed_known_block_still_rejected():
from pydantic import ValidationError
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{"role": "user", "content": [{"type": "tool_result", "content": "x"}]},
],
)
def test_user_content_block_non_string_type_rejected_cleanly():
# Non-string `type` must fail validation, not raise TypeError as a 500.
from pydantic import ValidationError
for bad_type in ([], {}, 5):
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "user", "content": [{"type": bad_type}]}],
)
def test_assistant_missing_content_key_still_rejected():
from pydantic import ValidationError
with pytest.raises(ValidationError):
AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [{"role": "assistant"}],
)
req = AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": None},
],
)
assert req.messages[1].content == ""
def test_resumed_null_assistant_between_users_coalesced_on_messages_route(monkeypatch):
# After dropping the null assistant turn, adjacent user turns must merge.
backend = _mock_backend(monkeypatch, context_length = 2048)
class _Req:
state = SimpleNamespace()
url = SimpleNamespace(path = "/v1/messages")
method = "POST"
async def is_disconnected(self):
return False
payload = AnthropicMessagesRequest(
model = "x",
max_tokens = 16,
messages = [
{"role": "user", "content": "first question"},
{"role": "assistant", "content": None},
{"role": "user", "content": "please continue"},
],
)
response = _drive(anthropic_messages(payload, request = _Req(), current_subject = "t"))
assert response.status_code == 200
[(_path, kwargs)] = backend.calls
user_turns = [m for m in kwargs["messages"] if m.get("role") == "user"]
assert len(user_turns) == 1
merged = user_turns[0]["content"]
if isinstance(merged, list):
merged = " ".join(p.get("text", "") for p in merged if isinstance(p, dict))
assert "first question" in merged and "please continue" in merged
def test_disable_parallel_tool_use_forwards_heartbeats_while_dropping():
"""Heartbeats from a dropped tool call must be forwarded, or the client sees a silent window."""
import threading as _threading
from routes.inference import (
_OPENAI_PASSTHROUGH_SSE_KEEPALIVE,
_anthropic_tool_stream,
)
def run_gen():
def gen():
yield _tool_event(type = "tool_start")
yield {"type": "heartbeat"}
yield _tool_result_event(result = "r1")
yield _tool_event(tool_call_id = "call_1")
yield {"type": "heartbeat"}
yield {
"type": "tool_output",
"tool_name": "python",
"tool_call_id": "call_1",
"text": "x",
}
yield {"type": "heartbeat"}
yield _tool_result_event(tool_call_id = "call_1", result = "r2")
yield {"type": "content", "text": "final answer"}
return gen()
async def _drive():
async def _is_disconnected():
return False
request = SimpleNamespace(is_disconnected = _is_disconnected)
resp = await _anthropic_tool_stream(
request,
_threading.Event(),
run_gen,
"msg_hb",
"m",
disable_parallel_tool_use = True,
)
return [chunk async for chunk in resp.body_iterator]
chunks = asyncio.run(_drive())
keepalives = [c for c in chunks if c == _OPENAI_PASSTHROUGH_SSE_KEEPALIVE]
assert len(keepalives) >= 3
tool_use_starts = [c for c in chunks if "content_block_start" in c and '"tool_use"' in c]
assert len(tool_use_starts) == 1
def test_dropped_tool_output_events_emit_rate_limited_keepalives(monkeypatch):
"""Dropped tool output emits rate-limited keepalives, or an idle proxy may kill the stream."""
import threading as _threading
import routes.inference as inf_mod
from routes.inference import (
_OPENAI_PASSTHROUGH_SSE_KEEPALIVE,
_anthropic_tool_stream,
)
# Deterministic clock: every keepalive here comes from the drop branch.
_real_time = inf_mod.time
_tick = {"v": 0.0}
def _fast_monotonic():
_tick["v"] += 100.0
return _tick["v"]
fake_time = SimpleNamespace(
monotonic = _fast_monotonic,
sleep = _real_time.sleep,
time = _real_time.time,
perf_counter = _real_time.perf_counter,
)
monkeypatch.setattr(inf_mod, "time", fake_time)
n_output = 4
def run_gen():
def gen():
yield _tool_event(type = "tool_start")
for i in range(n_output):
yield {
"type": "tool_output",
"tool_name": "python",
"tool_call_id": "call_0",
"text": f"line {i}\n",
}
yield _tool_result_event(type = "tool_end")
yield {"type": "content", "text": "final answer"}
return gen()
async def _drive():
async def _is_disconnected():
return False
request = SimpleNamespace(is_disconnected = _is_disconnected)
resp = await _anthropic_tool_stream(
request,
_threading.Event(),
run_gen,
"msg_drop_ka",
"m",
)
return [chunk async for chunk in resp.body_iterator]
chunks = asyncio.run(_drive())
keepalives = [c for c in chunks if c == _OPENAI_PASSTHROUGH_SSE_KEEPALIVE]
assert len(keepalives) == n_output
assert any("final answer" in c for c in chunks)
def test_parallel_disabled_dropped_call_output_emits_rate_limited_keepalives(monkeypatch):
"""Keepalives run before the drop skip, since a dropped call's output window can last minutes."""
import threading as _threading
import routes.inference as inf_mod
from routes.inference import (
_OPENAI_PASSTHROUGH_SSE_KEEPALIVE,
_anthropic_tool_stream,
)
_real_time = inf_mod.time
_tick = {"v": 0.0}
def _fast_monotonic():
_tick["v"] += 100.0
return _tick["v"]
fake_time = SimpleNamespace(
monotonic = _fast_monotonic,
sleep = _real_time.sleep,
time = _real_time.time,
perf_counter = _real_time.perf_counter,
)
monkeypatch.setattr(inf_mod, "time", fake_time)
n_output = 4
def run_gen():
def gen():
yield _tool_event(type = "tool_start")
yield _tool_result_event(result = "r1")
yield _tool_event(tool_call_id = "call_1")
for i in range(n_output):
yield {
"type": "tool_output",
"tool_name": "python",
"tool_call_id": "call_1",
"text": f"line {i}\n",
}
yield _tool_result_event(tool_call_id = "call_1", result = "r2")
yield {"type": "content", "text": "final answer"}
return gen()
async def _drive():
async def _is_disconnected():
return False
request = SimpleNamespace(is_disconnected = _is_disconnected)
resp = await _anthropic_tool_stream(
request,
_threading.Event(),
run_gen,
"msg_drop_ka2",
"m",
disable_parallel_tool_use = True,
)
return [chunk async for chunk in resp.body_iterator]
chunks = asyncio.run(_drive())
keepalives = [c for c in chunks if c == _OPENAI_PASSTHROUGH_SSE_KEEPALIVE]
assert len(keepalives) == n_output
tool_use_starts = [c for c in chunks if "content_block_start" in c and '"tool_use"' in c]
assert len(tool_use_starts) == 1
assert any("final answer" in c for c in chunks)
def test_plain_stream_emits_keepalive_during_prompt_stall(monkeypatch):
"""The no-tool Anthropic stream needs SSE keepalives during a long prompt prefill, since an
unbounded to_thread(next, ...) could sit silent past a proxy idle cap."""
import threading as _threading
import time as _time
from routes import inference as inf_mod
from routes.inference import _OPENAI_PASSTHROUGH_SSE_KEEPALIVE, _anthropic_plain_stream
monkeypatch.setattr(inf_mod, "_LOCAL_TOOL_STREAM_STALL_KEEPALIVE_S", 0.05)
def run_gen():
def gen():
_time.sleep(0.24)
yield "hello world"
return gen()
async def _drive():
async def _is_disconnected():
return False
request = SimpleNamespace(is_disconnected = _is_disconnected)
resp = await _anthropic_plain_stream(
request, _threading.Event(), run_gen, "msg_plain_ka", "m"
)
return [chunk async for chunk in resp.body_iterator]
chunks = asyncio.run(_drive())
keepalives = [c for c in chunks if c == _OPENAI_PASSTHROUGH_SSE_KEEPALIVE]
assert len(keepalives) >= 2
assert any("hello world" in c for c in chunks)
def test_plain_stream_closes_generator_on_disconnect():
"""A disconnect must drain the worker and close the generator, which the old finally leaked."""
import threading as _threading
from routes.inference import _anthropic_plain_stream
closed = _threading.Event()
class _FakeGen:
def __init__(self):
self._items = iter(["tok0", "tok1", "tok2", "tok3"])
def __next__(self):
return next(self._items)
def close(self):
closed.set()
def run_gen():
return _FakeGen()
state = {"disconnected": False}
async def _drive():
async def _is_disconnected():
return state["disconnected"]
request = SimpleNamespace(is_disconnected = _is_disconnected)
resp = await _anthropic_plain_stream(
request, _threading.Event(), run_gen, "msg_plain_close", "m"
)
out = []
async for chunk in resp.body_iterator:
out.append(chunk)
if "tok0" in chunk:
state["disconnected"] = True
return out
asyncio.run(_drive())
assert closed.is_set()
def test_display_strip_keeps_provenance_trace_intact():
# Without provenance, a quoted </think> makes the split eat the real answer.
from routes.inference import _ReasoningSpanGuard, _split_think_segments
trace = (
'emit </think> then <tool_call>{"name": "get_weather", "arguments": {}}</tool_call> ends it'
)
raw = f"<think>{trace}</think>It is sunny."
prov = {"wrapped": 1, "wraps": [{"len": len(trace)}]}
clean = _ReasoningSpanGuard(prov).strip(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
)
assert clean == raw
emitter = AnthropicStreamEmitter(think_provenance = prov)
events = emitter.start("msg_1", "m")
events += emitter.feed({"type": "content", "text": clean})
events += emitter.finish()
assert _emitter_client_thinking(events) == trace
assert _emitter_client_text(events) == "It is sunny."
assert _split_think_segments(clean, prov["wraps"][0]) == [
("thinking", trace),
("text", "It is sunny."),
]
def test_display_strip_protects_the_wrap_of_each_tool_turn():
# Each synthesis turn uses its own wrap entry, so the guard must advance.
from routes.inference import _ReasoningSpanGuard
first = "short first turn"
second = (
'emit </think> then <tool_call>{"name": "get_weather", "arguments": {}}</tool_call> ends it'
)
prov = {"wrapped": 2, "wraps": [{"len": len(first)}, {"len": len(second)}]}
guard = _ReasoningSpanGuard(prov)
names = {"get_weather"}
turn1 = f"<think>{first}</think>Looking it up."
assert guard.strip(turn1, auto_heal_tool_calls = True, enabled_tool_names = names) == turn1
guard.tool_end()
turn2 = f"<think>{second}</think>It is sunny."
assert guard.strip(turn2, auto_heal_tool_calls = True, enabled_tool_names = names) == turn2
def test_display_strip_still_cleans_tool_xml_after_the_trace():
from routes.inference import _ReasoningSpanGuard, _strip_tool_xml_for_display
prov = {"wrapped": 1, "wraps": [{"len": len("plain reasoning")}]}
raw = '<think>plain reasoning</think>Done <tool_call>{"name": "get_weather", "arguments": {}}</tool_call>ok'
clean = _ReasoningSpanGuard(prov).strip(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
)
assert clean == "<think>plain reasoning</think>Done ok"
assert _ReasoningSpanGuard(None).strip(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
) == _strip_tool_xml_for_display(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
)
def test_display_strip_leaves_unwrapped_leading_tag_to_the_cleaner():
from routes.inference import _ReasoningSpanGuard, _strip_tool_xml_for_display
raw = '<think>literal</think>Done <tool_call>{"name": "get_weather", "arguments": {}}</tool_call>ok'
for prov in ({"wrapped": 0, "wraps": []}, None):
assert _ReasoningSpanGuard(prov).strip(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
) == _strip_tool_xml_for_display(
raw, auto_heal_tool_calls = True, enabled_tool_names = {"get_weather"}
)
def _parse_anthropic_sse(lines):
"""Turn emitter output into (event_name, data) pairs, asserting well-formedness."""
events = []
blob = "".join(lines)
assert "\r" not in blob, "carriage return in SSE: strict parsers split events on \\n\\n"
for chunk in blob.split("\n\n"):
if not chunk.strip():
continue
name = data = None
for line in chunk.split("\n"):
if line.startswith("event: "):
name = line[len("event: ") :]
elif line.startswith("data: "):
data = json.loads(line[len("data: ") :])
assert name is not None and data is not None, f"malformed SSE chunk {chunk!r}"
events.append((name, data))
return events
def assert_anthropic_stream_conformant(lines):
"""Checks message_start first, gapless block indices, message_stop last."""
events = _parse_anthropic_sse(lines)
assert events, "no events emitted"
assert events[0][0] == "message_start", f"first event was {events[0][0]}"
assert events[-1][0] == "message_stop", f"last event was {events[-1][0]}"
blocks = {}
open_index = None
next_index = 0
closed = []
saw_message_delta = False
for name, data in events[1:]:
assert data.get("type") == name, f"event {name} carries data.type {data.get('type')}"
if name in ("ping", "tool_result"):
# SDKs ignore Unsloth's tool_result event, which must never claim a block index.
assert "index" not in data, "tool_result must not consume a block index"
continue
if name == "content_block_start":
assert not saw_message_delta, "content_block_start after message_delta"
assert open_index is None, f"block {open_index} still open"
assert (
data["index"] == next_index
), f"index {data['index']} out of sequence, expected {next_index}"
block = data["content_block"]
if block["type"] == "thinking":
assert block.get("thinking") == ""
assert "signature" in block, "thinking block start needs a signature field"
open_index = data["index"]
next_index += 1
blocks[open_index] = {"type": block["type"], "text": ""}
elif name == "content_block_delta":
assert open_index is not None, "delta with no open block"
assert data["index"] == open_index, f"delta index {data['index']} != {open_index}"
delta = data["delta"]
expected = {
"text_delta": "text",
"thinking_delta": "thinking",
"signature_delta": "thinking",
"input_json_delta": "tool_use",
}[delta["type"]]
assert (
blocks[open_index]["type"] == expected
), f"{delta['type']} inside a {blocks[open_index]['type']} block"
blocks[open_index]["text"] += delta.get("text") or delta.get("thinking") or ""
elif name == "content_block_stop":
assert open_index is not None, "content_block_stop with no open block"
assert data["index"] == open_index
assert open_index not in closed, f"block {open_index} closed twice"
closed.append(open_index)
open_index = None
elif name == "message_delta":
assert not saw_message_delta, "two message_delta events"
assert open_index is None, f"message_delta while block {open_index} is open"
assert "stop_reason" in data["delta"] and "usage" in data
saw_message_delta = True
elif name == "message_stop":
assert saw_message_delta, "message_stop without message_delta"
else:
raise AssertionError(f"unexpected event {name}")
assert open_index is None, f"stream ended with block {open_index} open"
assert saw_message_delta, "no message_delta"
assert sorted(closed) == list(range(len(closed))), f"indices not gapless from 0: {closed}"
return [(blocks[i]["type"], blocks[i]["text"]) for i in sorted(blocks)]
def _drive_emitter(
chunks,
provenance = None,
parse_think = True,
extra = (),
):
"""Feed cumulative content deltas (plus any extra events) through the emitter."""
emitter = AnthropicStreamEmitter(parse_think = parse_think, think_provenance = provenance)
lines = list(emitter.start("msg_1", "test-model", input_tokens = 3))
cumulative = ""
for chunk in chunks:
cumulative += chunk
lines.extend(emitter.feed({"type": "content", "text": cumulative}))
for event in extra:
lines.extend(emitter.feed(event))
lines.extend(emitter.finish())
return lines
class TestAnthropicStreamGrammar:
"""Every block index comes from _alloc_block_index, since the SDK accumulates deltas by index."""
@pytest.mark.parametrize(
"text, provenance",
[
("plain answer", None),
("<think>trace</think>answer", {"wrapped": 1, "wraps": [{"len": 5}]}),
("<think>unclosed trace", {"wrapped": 1, "wraps": [{"len": 14}]}),
("<think></think>answer", {"wrapped": 1, "wraps": [{"len": 0}]}),
("<think>a </think> b</think>answer", {"wrapped": 1, "wraps": [{"len": 11}]}),
("answer then <think>quoted</think>", {"wrapped": 0, "wraps": []}),
("<think>思考\U0001f9e0</think>答案", {"wrapped": 1, "wraps": [{"len": 3}]}),
],
)
@pytest.mark.parametrize("split", ["whole", "chars", "halves"])
@pytest.mark.parametrize("parse_think", [True, False])
def test_grammar_holds_for_every_shape(self, text, provenance, split, parse_think):
chunks = {
"whole": [text],
"chars": list(text),
"halves": [text[: len(text) // 2], text[len(text) // 2 :]],
}[split]
assert_anthropic_stream_conformant(
_drive_emitter(
[c for c in chunks if c], json.loads(json.dumps(provenance)), parse_think
)
)
def test_a_reply_with_no_content_is_still_a_valid_stream(self):
"""start() no longer opens a block, so an empty generation emits none."""
blocks = assert_anthropic_stream_conformant(_drive_emitter([]))
assert blocks == []
@pytest.mark.parametrize(
"extra_first",
[False, True],
)
def test_tool_blocks_keep_the_index_sequence(self, extra_first):
"""A tool call and later text each take the next index, even when text came first."""
tool = [
{
"type": "tool_start",
"tool_call_id": "call_1",
"name": "get_weather",
"arguments": '{"city": "SF"}',
},
{
"type": "tool_end",
"tool_call_id": "call_1",
"name": "get_weather",
"result": "sunny",
},
]
chunks = ["checking"] if extra_first else []
blocks = assert_anthropic_stream_conformant(_drive_emitter(chunks, extra = tool))
assert [b[0] for b in blocks] == (["text", "tool_use"] if extra_first else ["tool_use"])
def test_thinking_then_tool_then_text_keeps_thinking_first(self):
"""Anthropic orders thinking ahead of the tool call it justifies."""
provenance = {"wrapped": 1, "wraps": [{"len": len("need the weather")}]}
emitter = AnthropicStreamEmitter(parse_think = True, think_provenance = provenance)
lines = list(emitter.start("msg_1", "test-model"))
lines.extend(emitter.feed({"type": "content", "text": "<think>need the weather</think>"}))
lines.extend(
emitter.feed(
{
"type": "tool_start",
"tool_call_id": "call_1",
"name": "get_weather",
"arguments": "{}",
}
)
)
lines.extend(
emitter.feed(
{
"type": "tool_end",
"tool_call_id": "call_1",
"name": "get_weather",
"result": "sunny",
}
)
)
lines.extend(emitter.feed({"type": "content", "text": "It is sunny"}))
lines.extend(emitter.finish())
blocks = assert_anthropic_stream_conformant(lines)
assert blocks == [
("thinking", "need the weather"),
("tool_use", ""),
("text", "It is sunny"),
]
class TestThinkTagSplitAcrossDeltas:
"""A <think> tag can be cut at any offset by tokenisation, so the emitter holds back a partial
tag; only sweeping every split proves it never leaks or eats prose."""
TEXT = "<think>abc</think>def"
@pytest.mark.parametrize("cut", range(len(TEXT) + 1))
def test_two_way_split_at_every_offset(self, cut):
provenance = {"wrapped": 1, "wraps": [{"len": 3}]}
blocks = assert_anthropic_stream_conformant(
_drive_emitter([c for c in (self.TEXT[:cut], self.TEXT[cut:]) if c], provenance)
)
assert blocks == [("thinking", "abc"), ("text", "def")]
@pytest.mark.parametrize("first", range(len(TEXT) + 1))
def test_three_way_split_at_every_offset(self, first):
for second in range(first, len(self.TEXT) + 1):
chunks = [
c for c in (self.TEXT[:first], self.TEXT[first:second], self.TEXT[second:]) if c
]
blocks = assert_anthropic_stream_conformant(
_drive_emitter(chunks, {"wrapped": 1, "wraps": [{"len": 3}]})
)
assert blocks == [
("thinking", "abc"),
("text", "def"),
], f"split at {first},{second} produced {blocks}"
@pytest.mark.parametrize("cut", range(1, len("<think>")))
def test_a_partial_tag_at_end_of_stream_is_literal_text(self, cut):
"""A partial tag cut at stream end is literal output, not markup, and must be kept."""
held = "<think>"[:cut]
blocks = assert_anthropic_stream_conformant(
_drive_emitter(["answer " + held], {"wrapped": 0, "wraps": []})
)
assert blocks == [("text", "answer " + held)]
class TestWhitespaceOnlyThinkingIsNotABlock:
"""Whitespace-only reasoning opens no thinking block, as the non-streaming reducer does."""
@pytest.mark.parametrize("trace", ["", " ", "\n\n", " \n \t "])
def test_streaming_drops_it(self, trace):
text = f"<think>{trace}</think>The answer"
provenance = {"wrapped": 1, "wraps": [{"len": len(trace)}]}
blocks = assert_anthropic_stream_conformant(_drive_emitter([text], provenance))
assert blocks == [("text", "The answer")]
@pytest.mark.parametrize("trace", ["", " ", "\n\n", " \n \t "])
def test_streaming_and_non_streaming_agree(self, trace):
from routes.inference import _anthropic_plain_response_from_events
text = f"<think>{trace}</think>The answer"
provenance = {"wrapped": 1, "wraps": [{"len": len(trace)}]}
streamed = assert_anthropic_stream_conformant(
_drive_emitter([text], json.loads(json.dumps(provenance)))
)
response = _anthropic_plain_response_from_events(
iter([text]),
"msg_1",
"test-model",
parse_think = True,
think_provenance = json.loads(json.dumps(provenance)),
)
reduced = [
(b["type"], b.get("thinking") or b.get("text", ""))
for b in json.loads(response.body)["content"]
]
assert streamed == reduced
def test_a_trace_that_merely_starts_with_whitespace_is_kept_verbatim(self):
"""Only a fully blank trace is dropped; real reasoning keeps its leading newlines."""
trace = "\n\nWorking through it"
provenance = {"wrapped": 1, "wraps": [{"len": len(trace)}]}
blocks = assert_anthropic_stream_conformant(
_drive_emitter(list(f"<think>{trace}</think>Answer"), provenance)
)
assert blocks == [("thinking", trace), ("text", "Answer")]
class TestReasoningSurvivesTheWholeChain:
"""Generator and emitter are tested together, since each is only correct with the other."""
@staticmethod
def _backend(monkeypatch, chunks):
import contextlib
from core.inference.llama_cpp import LlamaCppBackend
backend = LlamaCppBackend.__new__(LlamaCppBackend)
backend._process = object()
backend._healthy = True
backend._port = 48851
backend._api_key = None
backend._effective_context_length = 4096
backend._supports_reasoning = True
backend._reasoning_always_on = False
backend._reasoning_style = "enable_thinking"
backend._supports_preserve_thinking = False
@contextlib.contextmanager
def fake_stream(
_client,
_url,
payload,
_cancel,
headers = None,
first_token_deadline = None,
):
yield type("R", (), {"status_code": 200, "chunks": chunks})()
monkeypatch.setattr(backend, "_stream_with_retry", fake_stream)
monkeypatch.setattr(
backend,
"_iter_text_cancellable",
lambda response, _c, first_token_deadline = None: iter(response.chunks),
)
monkeypatch.setattr(backend, "_maybe_recover_from_mtp_crash", lambda *a, **k: False)
return backend
@staticmethod
def _sse(delta):
return "data: " + json.dumps({"choices": [{"index": 0, "delta": delta}]}) + "\n"
def _run(self, monkeypatch, deltas):
backend = self._backend(monkeypatch, [self._sse(d) for d in deltas] + ["data: [DONE]\n"])
provenance = {"wrapped": 0}
emitter = AnthropicStreamEmitter(parse_think = True, think_provenance = provenance)
lines = list(emitter.start("msg_1", "test-model"))
for cumulative in backend.generate_chat_completion(
messages = [{"role": "user", "content": "hi"}],
reasoning_provenance = provenance,
):
if isinstance(cumulative, str):
lines.extend(emitter.feed({"type": "content", "text": cumulative}))
lines.extend(emitter.finish())
return assert_anthropic_stream_conformant(lines)
def test_reasoning_then_answer_becomes_a_thinking_block_and_a_text_block(self, monkeypatch):
blocks = self._run(
monkeypatch,
[
{"reasoning_content": "First I "},
{"reasoning_content": "check the map."},
{"content": "It is "},
{"content": "north."},
],
)
assert blocks == [("thinking", "First I check the map."), ("text", "It is north.")]
def test_a_reasoning_only_reply_is_not_empty(self, monkeypatch):
blocks = self._run(monkeypatch, [{"reasoning_content": "just thinking"}])
assert blocks and blocks[0][0] in ("thinking", "text")
assert "just thinking" in blocks[0][1]
def test_a_literal_think_tag_in_content_stays_text(self, monkeypatch):
"""No reasoning_content, so the generator wraps nothing and provenance
stays at zero -- the model merely quoted the tag."""
blocks = self._run(
monkeypatch,
[
{"content": "<think>"},
{"content": " is the tag"},
],
)
assert blocks == [("text", "<think> is the tag")]
def test_a_close_tag_quoted_inside_the_trace_does_not_end_it(self, monkeypatch):
blocks = self._run(
monkeypatch,
[
{"reasoning_content": "the closer is </think>"},
{"reasoning_content": " and it is quoted"},
{"content": "done"},
],
)
assert blocks == [
("thinking", "the closer is </think> and it is quoted"),
("text", "done"),
]
def test_an_old_llama_server_that_never_sends_reasoning_content_is_unchanged(self, monkeypatch):
"""Pre-reasoning_content builds only ever populate `content`."""
blocks = self._run(monkeypatch, [{"content": "plain "}, {"content": "answer"}])
assert blocks == [("text", "plain answer")]
def test_a_blank_reasoning_trace_does_not_open_a_thinking_block(self, monkeypatch):
"""Qwen3 with thinking off renders "<think>\\n\\n</think>" and llama-server
reports it as reasoning_content, on every single reply."""
blocks = self._run(
monkeypatch,
[
{"reasoning_content": "\n\n"},
{"content": "The answer"},
],
)
assert blocks == [("text", "The answer")]
class TestReasoningProvenanceIsBoundToItsSynthesisTurn:
"""Attribution is per turn: block order over the final aggregate let early literal tags steal wraps."""
TOOLS = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Weather.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}
]
@staticmethod
def _sse(delta):
return "data: " + json.dumps({"choices": [{"index": 0, "delta": delta}]}) + "\n"
@classmethod
def _tool_call_sse(cls, call_id, city):
return cls._sse(
{
"tool_calls": [
{
"index": 0,
"id": call_id,
"type": "function",
"function": {
"name": "get_weather",
"arguments": json.dumps({"city": city}),
},
}
]
}
)
@classmethod
def _backend(cls, monkeypatch, streams):
import contextlib
from core.inference.llama_cpp import LlamaCppBackend
backend = LlamaCppBackend.__new__(LlamaCppBackend)
backend._process = object()
backend._healthy = True
backend._port = 48853
backend._api_key = None
backend._effective_context_length = 4096
backend._supports_reasoning = True
backend._reasoning_always_on = False
backend._reasoning_style = "enable_thinking"
backend._supports_preserve_thinking = False
_pending = list(streams)
@contextlib.contextmanager
def fake_stream(
_client,
_url,
_payload,
_cancel,
headers = None,
**_kw,
):
yield type("R", (), {"status_code": 200, "chunks": _pending.pop(0)})()
monkeypatch.setattr(backend, "_stream_with_retry", fake_stream)
monkeypatch.setattr(
backend,
"_iter_text_cancellable",
lambda response, _c, first_token_deadline = None: iter(response.chunks),
)
monkeypatch.setattr(backend, "_maybe_recover_from_mtp_crash", lambda *a, **k: False)
monkeypatch.setattr(
"core.inference.tools.execute_tool", lambda name, arguments, **k: "sunny"
)
return backend
def _generator(self, monkeypatch, streams, provenance):
backend = self._backend(monkeypatch, streams)
return backend.generate_chat_completion_with_tools(
messages = [{"role": "user", "content": "weather?"}],
tools = self.TOOLS,
max_tool_iterations = len(streams),
reasoning_provenance = provenance,
)
def _streamed(self, monkeypatch, streams):
provenance = {"wrapped": 0}
emitter = AnthropicStreamEmitter(parse_think = True, think_provenance = provenance)
lines = list(emitter.start("msg_1", "test-model"))
for event in self._generator(monkeypatch, streams, provenance):
lines.extend(emitter.feed(event))
lines.extend(emitter.finish())
return assert_anthropic_stream_conformant(lines)
def _reduced(self, monkeypatch, streams):
from routes.inference import (
_anthropic_tool_response_from_events,
_collect_anthropic_events,
)
provenance = {"wrapped": 0}
gen = self._generator(monkeypatch, streams, provenance)
events = _collect_anthropic_events(lambda: gen, provenance)
response = _anthropic_tool_response_from_events(
events,
"msg_1",
"test-model",
openai_tools = self.TOOLS,
parse_think = True,
think_provenance = provenance,
)
blocks = []
for block in json.loads(response.body)["content"]:
if block["type"] == "thinking":
blocks.append(("thinking", block["thinking"]))
elif block["type"] == "tool_use":
blocks.append(("tool_use", block["name"]))
else:
blocks.append(("text", block["text"]))
return blocks
@staticmethod
def _norm(blocks):
"""The stream conformance helper accumulates a tool_use block's
input_json_delta, the reducer reports its name; compare the rest."""
return [(t, "" if t == "tool_use" else v) for t, v in blocks]
def _literal_then_genuine(self):
return [
[
self._sse({"content": "<think>\n\n</think>\n\nLet me check."}),
self._tool_call_sse("call_1", "SF"),
"data: [DONE]\n",
],
[
self._sse({"reasoning_content": "The tool said sunny, so"}),
self._sse({"content": "It is sunny in SF."}),
"data: [DONE]\n",
],
]
def test_a_literal_leading_tag_does_not_steal_a_later_turns_wrap(self, monkeypatch):
blocks = self._reduced(monkeypatch, self._literal_then_genuine())
assert blocks == [
("text", "<think>\n\n</think>\n\nLet me check."),
("tool_use", "get_weather"),
("thinking", "The tool said sunny, so"),
("text", "It is sunny in SF."),
]
def test_streaming_and_non_streaming_agree_on_a_mixed_tool_loop(self, monkeypatch):
streamed = self._streamed(monkeypatch, self._literal_then_genuine())
reduced = self._reduced(monkeypatch, self._literal_then_genuine())
assert self._norm(streamed) == self._norm(reduced)
def test_a_literal_tag_between_two_genuine_turns_keeps_both_traces(self, monkeypatch):
"""Three turns: wrap, literal, wrap; block-order attribution gave the literal turn the
second wrap and left the third turn's trace as tagged text."""
streams = [
[
self._sse({"reasoning_content": "first I check"}),
self._tool_call_sse("call_1", "SF"),
"data: [DONE]\n",
],
[
self._sse({"content": "<think>\n\n</think>\n\nOne more."}),
self._tool_call_sse("call_2", "LA"),
"data: [DONE]\n",
],
[
self._sse({"reasoning_content": "both are sunny"}),
self._sse({"content": "Both cities are sunny."}),
"data: [DONE]\n",
],
]
expected = [
("thinking", "first I check"),
("tool_use", "get_weather"),
("text", "<think>\n\n</think>\n\nOne more."),
("tool_use", "get_weather"),
("thinking", "both are sunny"),
("text", "Both cities are sunny."),
]
assert self._reduced(monkeypatch, streams) == expected
assert self._norm(self._streamed(monkeypatch, streams)) == self._norm(expected)
def test_two_genuine_turns_still_map_in_order(self, monkeypatch):
"""The ordinary case must be untouched: each turn keeps its own trace."""
streams = [
[
self._sse({"reasoning_content": "look it up"}),
self._tool_call_sse("call_1", "SF"),
"data: [DONE]\n",
],
[
self._sse({"reasoning_content": "report it"}),
self._sse({"content": "Sunny."}),
"data: [DONE]\n",
],
]
expected = [
("thinking", "look it up"),
("tool_use", "get_weather"),
("thinking", "report it"),
("text", "Sunny."),
]
assert self._reduced(monkeypatch, streams) == expected
assert self._norm(self._streamed(monkeypatch, streams)) == self._norm(expected)
class TestALiteralTagAfterBlankSpaceIsNotDuplicated:
"""A blank lead before a literal think tag is emitted once, not again by the literal-text fallback."""
@pytest.mark.parametrize("lead", ["\n", "\n\n", " ", " \n\t"])
@pytest.mark.parametrize("split", ["one", "chars"])
def test_the_leading_run_is_delivered_exactly_once(self, lead, split):
text = f"{lead}<think> is the tag you asked about."
chunks = [text] if split == "one" else list(text)
blocks = assert_anthropic_stream_conformant(
_drive_emitter(chunks, {"wrapped": 0, "wraps": []})
)
assert blocks == [("text", text)]
def test_a_genuine_trace_after_a_blank_line_still_parses(self):
"""Only the literal branch doubled; the provenance branch already dropped the run."""
provenance = {"wrapped": 1, "wraps": [{"len": 5}]}
blocks = assert_anthropic_stream_conformant(
_drive_emitter(["\n\n<think>trace</think>Answer"], provenance)
)
assert blocks == [("text", "\n\n"), ("thinking", "trace"), ("text", "Answer")]
class TestPreserveThinkingHonoursTheBackendDefault:
"""An omitted preserve_thinking must keep the loaded template's default, not coerce to False."""
TRACE = "The user prefers metric units."
MESSAGES = [
{"role": "user", "content": "How far is the moon?"},
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": TRACE, "signature": ""},
{"type": "text", "text": "About 384,400 km."},
],
},
{"role": "user", "content": "And the sun?"},
]
@staticmethod
def _payload(**fields):
base = {"max_tokens": 16, "messages": TestPreserveThinkingHonoursTheBackendDefault.MESSAGES}
base.update(fields)
return AnthropicMessagesRequest(**base)
@staticmethod
def _backend(default):
from types import SimpleNamespace
return SimpleNamespace(
supports_preserve_thinking = True,
preserve_thinking_default = default,
)
@pytest.mark.parametrize(
"default, override, expected",
[
# Omitted on a preserve-by-default model must keep the block.
(True, None, True),
(False, None, False),
(True, True, True),
(True, False, False),
(False, True, True),
(False, False, False),
],
)
def test_resolver_is_three_valued(self, default, override, expected):
from routes.inference import _anthropic_preserve_thinking
resolved = _anthropic_preserve_thinking(
self._backend(default), self._payload(preserve_thinking = override)
)
assert resolved is expected
def test_none_and_explicit_false_are_distinguishable(self):
"""bool() made None and False identical; on a default-true backend they must differ."""
from routes.inference import _anthropic_preserve_thinking
backend = self._backend(True)
assert _anthropic_preserve_thinking(backend, self._payload()) is True
assert (
_anthropic_preserve_thinking(backend, self._payload(preserve_thinking = False)) is False
)
def test_a_backend_without_the_attribute_stays_off(self):
"""Test doubles and non-reasoning backends lack the property."""
from types import SimpleNamespace
from routes.inference import _anthropic_preserve_thinking
assert _anthropic_preserve_thinking(SimpleNamespace(), self._payload()) is False
@pytest.mark.parametrize("default", [True, False])
@pytest.mark.parametrize("override", [None, True, False])
def test_conversion_and_counting_cannot_drift(self, default, override):
"""Both routes share one helper, so the counted prompt is the one generation renders."""
from routes.inference import _anthropic_preserve_thinking
payload = self._payload(preserve_thinking = override)
backend = self._backend(default)
resolved = _anthropic_preserve_thinking(backend, payload)
converted = anthropic_messages_to_openai(
[m.model_dump() for m in payload.messages],
payload.system,
preserve_thinking = resolved,
)
counted = anthropic_messages_to_openai(
[m.model_dump() for m in payload.messages],
payload.system,
preserve_thinking = _anthropic_preserve_thinking(backend, payload),
)
assert converted == counted
assistant = next(m for m in converted if m["role"] == "assistant")
expected_kept = override if override is not None else default
assert ("reasoning_content" in assistant) is expected_kept
if expected_kept:
assert assistant["reasoning_content"] == self.TRACE
assert assistant["content"] == "About 384,400 km."
@pytest.mark.parametrize(
"default, override, expect_block",
[(True, None, True), (False, None, False), (True, False, False), (False, True, True)],
)
def test_the_route_hands_the_block_to_the_generator(
self, monkeypatch, default, override, expect_block
):
"""End to end over /v1/messages and /v1/messages/count_tokens against a
deterministic backend double: what reaches the generator is what the
counter prices."""
import asyncio
from types import SimpleNamespace
import routes.inference as inf_mod
from routes.inference import anthropic_count_tokens, anthropic_messages
seen = {}
def _gen(**kwargs):
seen["messages"] = kwargs["messages"]
seen["preserve_thinking"] = kwargs.get("preserve_thinking")
yield "ok"
def _count(
messages,
system,
tools,
strict = False,
chat_template_kwargs = None,
):
seen["count_messages"] = messages
return sum(len(str(m.get("reasoning_content") or "")) for m in messages) + 1
backend = SimpleNamespace(
is_loaded = True,
is_vision = False,
supports_tools = False,
supports_tool_passthrough = False,
model_identifier = "unsloth/Qwen3.8-8B-GGUF",
context_length = 4096,
supports_reasoning = True,
reasoning_always_on = False,
reasoning_default = True,
supports_preserve_thinking = True,
preserve_thinking_default = default,
count_chat_tokens = _count,
generate_chat_completion = _gen,
generate_chat_completion_with_tools = _gen,
effective_parallel_slots = 4,
base_url = "http://llama.preserve.test:9999",
_request_reasoning_kwargs = lambda et, re_, pt: (
{
k: v
for k, v in (("enable_thinking", et), ("preserve_thinking", pt))
if v is not None
}
or None
),
)
monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)
class _Request:
def __init__(self):
self.state = SimpleNamespace()
self.url = SimpleNamespace(path = "/v1/messages")
self.method = "POST"
async def is_disconnected(self):
return False
payload = self._payload(preserve_thinking = override)
asyncio.run(anthropic_messages(payload, request = _Request(), current_subject = "t"))
gen_messages = seen["messages"]
assert any(m.get("reasoning_content") for m in gen_messages) is expect_block
# Omitted stays None so llama-server uses the launch-time kwarg.
assert seen["preserve_thinking"] == override
asyncio.run(anthropic_count_tokens(payload, request = _Request(), current_subject = "t"))
assert seen["count_messages"] == gen_messages
def test_effort_resolved_from_output_config():
assert (
_basic_payload(
output_config = {"effort": "high"}, thinking = {"type": "adaptive"}
).reasoning_effort
== "high"
)
def test_explicit_reasoning_effort_outranks_output_config():
assert (
_basic_payload(output_config = {"effort": "low"}, reasoning_effort = "max").reasoning_effort
== "max"
)
def test_unknown_output_config_effort_is_ignored():
adaptive = {"type": "adaptive"}
assert (
_basic_payload(output_config = {"effort": "turbo"}, thinking = adaptive).reasoning_effort
is None
)
assert (
_basic_payload(
output_config = {"format": {"type": "json"}}, thinking = adaptive
).reasoning_effort
is None
)
# Claude Code always sends output_config.effort, so gate it on thinking.
_EFFORT_THINKING_OFF_SHAPES = [
({}, None),
({"thinking": {"type": "disabled"}}, False),
({"enable_thinking": False}, False),
]
@pytest.mark.parametrize("fields, expected_enable", _EFFORT_THINKING_OFF_SHAPES)
def test_output_config_effort_never_switches_thinking_back_on(fields, expected_enable):
from routes.inference import _anthropic_reasoning_args
payload = _basic_payload(output_config = {"effort": "high"}, **fields)
assert payload.reasoning_effort is None
args = _anthropic_reasoning_args(payload)
assert args["enable_thinking"] is expected_enable
assert args["reasoning_effort"] is None
def test_x_unsloth_effort_still_outranks_thinking_when_sent_explicitly():
from routes.inference import _anthropic_reasoning_args
args = _anthropic_reasoning_args(
_basic_payload(thinking = {"type": "disabled"}, reasoning_effort = "high")
)
assert args["enable_thinking"] is True
assert args["reasoning_effort"] == "high"
_CLAUDE_CODE_UA = {"user-agent": "claude-cli/2.1.322 (external, sdk-cli)"}
_CLAUDE_CODE_BETA = {"anthropic-beta": "interleaved-thinking-2025-05-14, claude-code-20250219"}
class _HeaderRequest:
state = SimpleNamespace()
url = SimpleNamespace(path = "/v1/messages")
method = "POST"
def __init__(self, headers):
self.headers = headers
async def is_disconnected(self):
return False
@pytest.mark.parametrize(
"headers, expected",
[
(_CLAUDE_CODE_UA, True),
(_CLAUDE_CODE_BETA, True),
({"user-agent": "Anthropic/Python 0.70.0"}, False),
({"anthropic-beta": "interleaved-thinking-2025-05-14"}, False),
({}, False),
],
)
def test_only_claude_code_marks_an_omitted_thinking_as_off(headers, expected):
from routes.inference import _mark_claude_code_thinking
payload = _basic_payload(output_config = {"effort": "high"})
_mark_claude_code_thinking(payload, _HeaderRequest(headers))
assert payload.resolved_enable_thinking() is (False if expected else None)
@pytest.mark.parametrize(
"fields, expected_enable, expected_effort",
[
({}, False, None),
({"thinking": None}, False, None),
({"thinking": {"type": "adaptive", "display": "omitted"}}, True, None),
({"thinking": {"type": "disabled"}}, False, None),
({"enable_thinking": True}, True, None),
({"reasoning_effort": "high"}, True, "high"),
],
)
def test_claude_code_omitted_thinking_is_off_below_explicit_controls(
fields, expected_enable, expected_effort
):
from routes.inference import _anthropic_reasoning_args, _mark_claude_code_thinking
payload = _basic_payload(**fields)
_mark_claude_code_thinking(payload, _HeaderRequest(_CLAUDE_CODE_UA))
args = _anthropic_reasoning_args(payload)
assert args["enable_thinking"] is expected_enable
assert args["reasoning_effort"] == expected_effort
@pytest.mark.parametrize("headers, expected", [(_CLAUDE_CODE_UA, False), ({}, None)])
def test_claude_code_thinking_off_reaches_generation_and_counting(monkeypatch, headers, expected):
from routes.inference import anthropic_count_tokens, anthropic_messages
counted: list = []
backend = _mock_backend(
monkeypatch,
_request_reasoning_kwargs = lambda enable, effort, preserve: (
None if enable is None else {"enable_thinking": enable}
),
)
backend.count_chat_tokens = lambda *args, chat_template_kwargs = None, **kwargs: (
counted.append(chat_template_kwargs) or 2
)
_drive(
anthropic_messages(
_basic_payload(output_config = {"effort": "high"}),
request = _HeaderRequest(headers),
current_subject = "t",
)
)
_drive(
anthropic_count_tokens(
_basic_payload(output_config = {"effort": "high"}),
request = _HeaderRequest(headers),
current_subject = "t",
)
)
[(_path, kwargs)] = backend.calls
assert kwargs["enable_thinking"] is expected
assert kwargs["reasoning_effort"] is None
template_kwargs = None if expected is None else {"enable_thinking": False}
assert counted and all(kwargs == template_kwargs for kwargs in counted)
def test_the_anthropic_paths_promote_a_replayed_mcp_envelope():
"""Replayed MCP image envelopes must be promoted, or the model reads base64 as text."""
import inspect
from routes import inference
generate = inspect.getsource(inference.anthropic_messages)
assert (
"_promote_mcp_history_images_async(" in generate
), "the /v1/messages path never promotes the replayed envelope"
assert (
"replayed_image_parts = tuple(_anthropic_replayed_image_parts)" in generate
), "the loop's conversation cap has to start from what promotion put back"
count = inspect.getsource(inference.anthropic_count_tokens)
assert "await _promote_mcp_history_images_async(" in count, (
"the count endpoint prices the base64 the completion never sends, and must "
"not do the Pillow work inline on the event loop"
)
assert "_anthropic_admission_body(openai_messages" in generate, (
"admission has to reserve against the translated list, or an Anthropic "
"tool_result block prices its envelope as dense text"
)
def test_an_anthropic_replay_hands_the_model_the_picture_not_the_base64():
import asyncio
import base64
import io
import json
from PIL import Image
from core.inference import mcp_images
from routes.inference import _promote_mcp_history_images_async
buffer = io.BytesIO()
Image.new("RGB", (12, 12), (24, 90, 219)).save(buffer, format = "PNG")
payload = base64.b64encode(buffer.getvalue()).decode()
envelope = json.dumps([{"data": payload, "mimeType": "image/png"}])
messages = [
{"role": "user", "content": "take a screenshot"},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "toolu_0",
"type": "function",
"function": {"name": "mcp__shot__capture", "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "toolu_0",
"name": "mcp__shot__capture",
"content": "[1 image returned]\n" + mcp_images.SENTINEL + envelope,
},
{"role": "user", "content": "what colour was it"},
]
promoted: list = []
out = asyncio.run(
_promote_mcp_history_images_async(messages, vision = True, promoted_out = promoted)
)
text = "".join(
message["content"] if isinstance(message.get("content"), str) else "" for message in out
) + "".join(
part.get("text", "")
for message in out
if isinstance(message.get("content"), list)
for part in message["content"]
if part.get("type") == "text"
)
assert mcp_images.SENTINEL not in text
assert payload not in text, "the base64 reached the model as prompt text"
assert len(promoted) == 1
assert (
sum(
1
for message in out
if isinstance(message.get("content"), list)
for part in message["content"]
if part.get("type") == "image_url"
)
== 1
)
def test_a_text_only_anthropic_model_is_not_shown_the_envelope_either():
import asyncio
import json
from core.inference import mcp_images
from routes.inference import _promote_mcp_history_images_async
envelope = json.dumps([{"data": "QUJD", "mimeType": "image/png"}])
messages = [
{
"role": "tool",
"tool_call_id": "toolu_0",
"name": "mcp__shot__capture",
"content": "[1 image returned]\n" + mcp_images.SENTINEL + envelope,
}
]
out = asyncio.run(_promote_mcp_history_images_async(messages, vision = False))
assert mcp_images.SENTINEL not in json.dumps(out)
assert not any(isinstance(message.get("content"), list) for message in out)
def test_the_anthropic_count_refuses_a_promoted_image_rather_than_undercount():
"""The count refuses a promoted image: the template would undercount its projector tokens."""
import inspect
from routes import inference
count = inspect.getsource(inference.anthropic_count_tokens)
refusal = count.index(
"asyncio.to_thread(_messages_have_promotable_mcp_images, openai_messages)"
)
promotion = count.index("_promote_mcp_history_images_async(")
assert refusal < promotion, (
"the refusal has to come BEFORE promotion, or the envelope is already "
"image parts by the time it is checked"
)
assert "Cannot count tokens for messages containing images." in count
assert (
"llama_backend.is_vision\n"
" and _messages_mention_mcp_images(openai_messages)\n"
" and await asyncio.to_thread(_messages_have_promotable_mcp_images, openai_messages)"
) in count, (
"a text-only model has the envelope stripped and sends no pixels, so it "
"must still be counted rather than refused"
)
def test_the_anthropic_envelope_is_promoted_before_tool_roles_are_folded_away():
"""Promote the envelope before tool roles are folded into user messages, or promotion never sees it."""
import inspect
from routes import inference
for name, source in (
("generation", inspect.getsource(inference.anthropic_messages)),
("count", inspect.getsource(inference.anthropic_count_tokens)),
):
promote = source.index("_promote_mcp_history_images_async(")
sanitize = source.index("_sanitize_anthropic_openai_messages(openai_messages")
assert (
promote < sanitize
), f"{name}: the fold runs first and the envelope never reaches promotion"
named = source.index("_named_anthropic_tool_results(openai_messages)")
assert named < promote, f"{name}: provenance has to be restored first"
def test_an_anthropic_client_tool_is_not_trusted_as_an_mcp_image_source():
"""A client tool_result carries no name, so an absent name must not be trusted as MCP history."""
import base64
import io
import json
from PIL import Image
from core.inference import mcp_images
from routes.inference import _named_anthropic_tool_results
buffer = io.BytesIO()
Image.new("RGB", (8, 8), (10, 120, 200)).save(buffer, format = "PNG")
envelope = json.dumps(
[{"data": base64.b64encode(buffer.getvalue()).decode(), "mimeType": "image/png"}]
)
translated = [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "toolu_0",
"type": "function",
"function": {"name": "read_file", "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "toolu_0",
"content": "here you go\n" + mcp_images.SENTINEL + envelope,
},
]
named = _named_anthropic_tool_results(translated)
assert named[1]["name"] == "read_file", "the result was not correlated to its call"
out = mcp_images.promote_history(named, vision = True)
assert not any(
isinstance(m.get("content"), list) for m in out
), "a non-mcp__ tool was promoted as trusted image input"
assert mcp_images.SENTINEL not in json.dumps(out)
def test_a_real_mcp_tool_still_promotes_through_the_anthropic_naming():
import base64
import io
import json
from PIL import Image
from core.inference import mcp_images
from routes.inference import _named_anthropic_tool_results
buffer = io.BytesIO()
Image.new("RGB", (8, 8), (10, 120, 200)).save(buffer, format = "PNG")
envelope = json.dumps(
[{"data": base64.b64encode(buffer.getvalue()).decode(), "mimeType": "image/png"}]
)
translated = [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "toolu_0",
"type": "function",
"function": {"name": "mcp__shot__capture", "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "toolu_0",
"content": "[1 image returned]\n" + mcp_images.SENTINEL + envelope,
},
]
out = mcp_images.promote_history(_named_anthropic_tool_results(translated), vision = True)
assert (
sum(
1
for message in out
if isinstance(message.get("content"), list)
for part in message["content"]
if part.get("type") == "image_url"
)
== 1
)
def test_repeated_anthropic_call_ids_are_paired_positionally():
"""A conversation-wide id map renamed every earlier result with that id after the
newest call, suppressing an earlier MCP picture or trusting an earlier non-MCP one."""
from routes.inference import _named_anthropic_tool_results
def _call(name):
return {
"role": "assistant",
"content": "",
"tool_calls": [
{"id": "call_0", "type": "function", "function": {"name": name, "arguments": "{}"}}
],
}
named = _named_anthropic_tool_results(
[
_call("mcp__shot__capture"),
{"role": "tool", "tool_call_id": "call_0", "content": "first"},
_call("read_file"),
{"role": "tool", "tool_call_id": "call_0", "content": "second"},
]
)
assert [m.get("name") for m in named if m["role"] == "tool"] == [
"mcp__shot__capture",
"read_file",
]
def test_promoted_parts_take_the_anthropic_normalizer_off_the_loop():
"""_anthropic_has_image was read off the original blocks, so a replay-only request
took the synchronous branch and re-decoded up to eight promoted PNGs on the loop."""
import inspect
from routes import inference
src = inspect.getsource(inference.anthropic_messages)
assert "if _anthropic_has_image or _anthropic_replayed_image_parts:" in src
def test_the_anthropic_count_refusal_is_name_aware():
"""Names are stamped from the calls just before the check, so a client tool whose output merely
ends in a valid envelope must not turn a countable prompt into a 400."""
import inspect
import json
from core.inference import mcp_images
from routes import inference
from routes.inference import _messages_have_promotable_mcp_images, _named_anthropic_tool_results
src = inspect.getsource(inference.anthropic_count_tokens)
assert "asyncio.to_thread(_messages_have_promotable_mcp_images, openai_messages)" in src
assert "_messages_have_mcp_image_envelope(openai_messages)" not in src
envelope = json.dumps([{"data": "QUJD", "mimeType": "image/png"}])
def _translated(tool):
return _named_anthropic_tool_results(
[
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "toolu_0",
"type": "function",
"function": {"name": tool, "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "toolu_0",
"content": "out\n" + mcp_images.SENTINEL + envelope,
},
]
)
assert not _messages_have_promotable_mcp_images(_translated("read_file"))
assert _messages_have_promotable_mcp_images(_translated("mcp__shot__capture"))
assert _messages_have_promotable_mcp_images(
[{"role": "tool", "tool_call_id": "x", "content": "out\n" + mcp_images.SENTINEL + envelope}]
)
_COUNT_TOOL = {"name": "lookup", "description": "d", "input_schema": {"type": "object"}}
_COUNT_OPENAI_TOOL = {
"type": "function",
"function": {"name": "lookup", "description": "d", "parameters": {"type": "object"}},
}
_COUNT_USER = {"role": "user", "content": "hi"}
_COUNT_OTHER = {"name": "other", "input_schema": {"type": "object"}}
_COUNT_OPENAI_OTHER = {
"type": "function",
"function": {"name": "other", "description": "", "parameters": {"type": "object"}},
}
_COUNT_FORCED = {"type": "function", "function": {"name": "lookup"}}
_IP_WEB_SEARCH = {"type": "web_search_20250305", "name": "web_search"}
class TestInProcessCountTokens:
"""/v1/messages/count_tokens on a resident MLX model prices what chat's count prices."""
def _count(
self,
monkeypatch,
route,
payload,
*,
is_mlx = True,
):
from starlette.requests import Request
import routes.inference as inf
calls = []
class _Resident:
active_model_name = "mlx/m"
load_generation = 0
models = {"mlx/m": {"is_mlx": is_mlx, "is_vision": False}}
def count_chat_tokens(self, messages, system, **kwargs):
calls.append((messages, system, kwargs))
return 7, "mlx/m"
async def _switch(*_args, **_kwargs):
return None
monkeypatch.setattr(inf, "get_inference_backend", lambda: _Resident())
monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False))
monkeypatch.setattr(inf, "_maybe_auto_switch_model", _switch)
monkeypatch.setattr(
inf, "_detect_safetensors_features", lambda *a, **k: {"supports_tools": True}
)
scope = {"type": "http", "method": "POST", "path": "/", "headers": [], "query_string": b""}
response = asyncio.run(route(payload = payload, request = Request(scope), current_subject = "t"))
return json.loads(response.body), calls
@pytest.mark.parametrize("policy", [None, True])
@pytest.mark.parametrize(
"anthropic, chat, tools",
[
(
{"system": "be brief", "messages": [{"role": "user", "content": "hi"}]},
{
"messages": [
{"role": "system", "content": "be brief"},
{"role": "user", "content": "hi"},
],
"tool_choice": "none",
},
[],
),
(
{
"messages": [_COUNT_USER],
"tools": [_COUNT_TOOL, _COUNT_OTHER],
"tool_choice": {"type": "tool", "name": "lookup"},
},
{
"messages": [_COUNT_USER],
"tools": [_COUNT_OPENAI_TOOL, _COUNT_OPENAI_OTHER],
"tool_choice": _COUNT_FORCED,
},
["lookup"],
),
(
{"messages": [_COUNT_USER], "tools": [_IP_WEB_SEARCH]},
{"messages": [_COUNT_USER], "enable_tools": True, "enabled_tools": ["web_search"]},
["web_search"],
),
],
ids = ["plain", "forced_client_tool", "server_tool"],
)
def test_counts_what_chat_counts_for_the_translated_request(
self, monkeypatch, policy, anthropic, chat, tools
):
import routes.inference as inf
from models.inference import ChatCountTokensRequest
set_tool_policy(policy)
try:
body, calls = self._count(
monkeypatch, inf.anthropic_count_tokens, AnthropicMessagesRequest(**anthropic)
)
_, chat_calls = self._count(
monkeypatch, inf.chat_count_tokens, ChatCountTokensRequest(**chat)
)
finally:
reset_tool_policy()
assert body == {"input_tokens": 7}
assert calls == chat_calls
# A hard --enable-tools policy prices no catalog the Messages route did not select.
assert [t["function"]["name"] for t in calls[0][2]["tools"] or []] == tools
def test_an_archived_thread_prices_only_the_selected_server_tools(self, monkeypatch):
import routes.inference as inf
# Chat's own selection adds search_conversation for an archived thread.
monkeypatch.setattr(inf, "_thread_has_conversation_archive", lambda _thread: True)
payload = AnthropicMessagesRequest(
messages = [_COUNT_USER], tools = [_IP_WEB_SEARCH], thread_id = "t"
)
_, calls = self._count(monkeypatch, inf.anthropic_count_tokens, payload)
assert [t["function"]["name"] for t in calls[0][2]["tools"]] == ["web_search"]
@pytest.mark.parametrize(
"fields, is_mlx, status, detail",
[
({"enable_tools": True}, False, 503, "No GGUF model loaded"),
],
ids = ["not_mlx"],
)
def test_what_it_cannot_price_is_refused(self, monkeypatch, fields, is_mlx, status, detail):
import routes.inference as inf
payload = AnthropicMessagesRequest(messages = [{"role": "user", "content": "hi"}], **fields)
with pytest.raises(HTTPException) as exc:
self._count(monkeypatch, inf.anthropic_count_tokens, payload, is_mlx = is_mlx)
assert exc.value.status_code == status
assert detail in json.dumps(exc.value.detail)
_IP_CHUNKS = [{"delta": {"content": "hi"}}, {"delta": {}, "finish_reason": "stop"}]
_IP_SSE = (
"".join(f"data: {json.dumps({'choices': [c]})}\n\n" for c in _IP_CHUNKS) + "data: [DONE]\n\n"
)
_IP_JSON = {"choices": [{"message": {"content": "hi"}, "finish_reason": "stop"}]}
_IP_SOURCE = {"type": "base64", "media_type": "image/png", "data": "QUJD"}
_IP_IMAGE = {"type": "image", "source": _IP_SOURCE}
_IP_FORMAT = {"output_config": {"format": {"type": "json_schema", "schema": {}}}}
class TestInProcessMessages:
"""/v1/messages on an in-process model: chat's output through the passthrough translators."""
def _serve(
self,
monkeypatch,
stream,
*timing,
sse = _IP_SSE,
**fields,
):
stop_at, taken = timing or (None, -1)
from starlette.requests import Request
from starlette.responses import JSONResponse
import routes.inference as inf
seen = self.seen = {"closed": [], "unstarted": 0, "monitor": []}
async def fake_chat(chat_req, request, subject):
seen.update(req = chat_req, skip = request.state.skip_api_monitor)
seen["ui"] = inf._ui_stream_events_enabled(request)
seen["cap"] = inf._delegated_tool_names.get()
if not chat_req.stream:
if stop_at is not None:
request.state.generation_cancel_event.set()
return JSONResponse(_IP_JSON)
async def body():
try:
for i, line in enumerate(sse.split("\n\n")[:-1]):
if i == stop_at: # a Stop by the Messages id
seen["stopped"] = inf._cancel_by_keys([seen["message_id"]])
seen["runs"] = inf.active_generations.snapshot()
if raise_on_stop: # as the managed vLLM / SGLang tool path does
raise asyncio.CancelledError()
yield line + "\n\n"
except BaseException as exc:
seen["closed"].append(type(exc).__name__ + "!" * seen.get("late", 0))
raise
unstarted = lambda: seen.update(unstarted = 1) or asyncio.sleep(0) # noqa: E731
return inf._SameTaskStreamingResponse(body(), unstarted_cleanup = unstarted)
raise_on_stop = fields.pop("raise_on_stop", False)
entry = {"is_mlx": True, "is_vision": fields.pop("vision", False)}
if fields.pop("speech", False):
entry.update(is_audio = True, audio_type = "csm")
backend = SimpleNamespace(active_model_name = "m", models = {"m": entry})
backend.count_chat_tokens = lambda *a, **k: (5, "m")
switch = lambda *a, **k: seen.update(switch = k) or asyncio.sleep(0) # noqa: E731
for name, value in [
("openai_chat_completions", fake_chat),
("get_inference_backend", lambda: backend),
("get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False)),
("_maybe_auto_switch_model", switch),
("_detect_safetensors_features", lambda *a, **k: {}),
]:
monkeypatch.setattr(inf, name, value)
monkeypatch.setattr(inf.api_monitor, "finish", lambda _id, *a: seen["monitor"].append(a))
scope = {"type": "http", "method": "POST", "path": "/v1/messages", "headers": []}
request = seen["request"] = Request(scope)
fields.setdefault("messages", [_COUNT_USER])
payload = AnthropicMessagesRequest(model = "m", max_tokens = 16, stream = bool(stream), **fields)
async def run():
response = await inf.anthropic_messages(payload, request, "t")
if not stream or taken is None:
return response.body.decode() if not stream else await response._unstarted_cleanup()
out, it = [], response.body_iterator
async for part in it:
out.append(part)
if "message_start" in part:
seen["message_id"] = json.loads(part.split("data: ", 1)[1])["message"]["id"]
if len(out) == taken:
with pytest.raises(asyncio.CancelledError):
await it.athrow(asyncio.CancelledError())
seen["late"] = 1 # closing only at loop shutdown is a leak
return "".join(out)
return asyncio.run(run()), seen
@pytest.mark.parametrize(
"stream, stop_at, taken, closed",
[
(1, 1, -1, ["GeneratorExit"]),
(1, None, None, []),
(1, None, 2, ["CancelledError"]),
(0, 0, -1, []),
],
ids = ["stop_by_message_id", "never_started", "outer_cancel", "stopped_json"],
)
def test_stop_ends_as_on_llama_server(self, monkeypatch, stream, stop_at, taken, closed):
import routes.inference as inf
with pytest.raises(asyncio.CancelledError) if not stream else contextlib.nullcontext():
out, seen = self._serve(monkeypatch, stream, stop_at, taken, session_id = "s1")
if not stream:
return # the llama-server JSON path never answers a stopped run either
assert seen["skip"] is True and seen["req"].session_id == "s1"
assert seen["closed"] == closed and seen["unstarted"] == (taken is None)
assert ("cancelled",) in seen["monitor"]
assert not [k for k in inf._CANCEL_REGISTRY if "msg_" in str(k)]
if stop_at is not None:
assert seen["stopped"] == 1 and "message_stop" not in out
assert seen["runs"] == [] # chat's own tracker records the run
assert seen["request"].scope.get("_unsloth_keepwarm_response_failed")
@pytest.mark.parametrize(
"fields, reason, multiple",
[
({}, None, None),
(_IP_FORMAT, None, None),
({"enable_tools": True, "permission_mode": "off"}, None, None),
(
{"messages": [{"role": "user", "content": [_IP_IMAGE, _IP_IMAGE]}], "vision": True},
None,
True,
),
({"messages": []}, "non-system", None),
],
ids = ["plain", "json_format", "server_tools", "two_images", "no_turn"],
)
def test_refused_before_a_load(self, monkeypatch, fields, reason, multiple):
with pytest.raises(HTTPException) if reason else contextlib.nullcontext():
self._serve(monkeypatch, 0, **fields)
sw = self.seen["switch"]
assert sw["gguf_only"] is (reason is not None)
assert reason in sw["gguf_only_reason"] if reason else sw["gguf_only_reason"] is None
assert (sw["image_preflight"] or {}).get("multiple") is multiple
def test_a_source_raising_on_stop_still_ends_the_stream_cleanly(self, monkeypatch):
out, seen = self._serve(monkeypatch, 1, 1, -1, raise_on_stop = True)
assert seen["stopped"] == 1 and "message_stop" not in out
assert not [
k for k in __import__("routes.inference").inference._CANCEL_REGISTRY if "msg_" in str(k)
]
@pytest.mark.parametrize("stream", [0, 1])
def test_a_sampling_value_chat_rejects_is_a_400_before_a_load(self, monkeypatch, stream):
# Messages bounds top_k less tightly than chat; a GGUF target still takes it.
with pytest.raises(HTTPException) as err:
self._serve(monkeypatch, stream, top_k = 200)
assert err.value.status_code == 400 and "top_k" in json.dumps(err.value.detail)
assert self.seen["switch"]["gguf_only"] is True and "req" not in self.seen
@pytest.mark.parametrize("stream", [0, 1])
def test_a_resident_speech_model_is_not_served_through_chat(self, monkeypatch, stream):
# Chat answers a TTS model with a finished clip, never a text reply.
with pytest.raises(HTTPException) as err:
self._serve(monkeypatch, stream, speech = True)
assert "req" not in self.seen
assert "GGUF" in json.dumps(err.value.detail)
@pytest.mark.parametrize("vision", [True, False])
def test_an_image_on_a_text_only_model_names_its_block(self, monkeypatch, vision):
turns = [
{"role": "user", "content": "Hi."},
{"role": "assistant", "content": "Hello."},
{"role": "user", "content": [{"type": "text", "text": "What?"}, _IP_IMAGE]},
]
with contextlib.nullcontext() if vision else pytest.raises(HTTPException) as err:
self._serve(monkeypatch, 0, messages = turns, vision = vision)
assert ("req" in self.seen) is vision
if not vision:
assert err.value.detail.startswith("messages.2.content.1.image: ")
@pytest.mark.parametrize("choice, kept", [(None, True), ({"type": "auto"}, False)])
def test_a_json_output_format_reaches_chat_unless_a_tool_may_run(
self, monkeypatch, choice, kept
):
tool = {"name": "lookup", "input_schema": {"type": "object"}}
tools = {"tools": [tool], "tool_choice": choice} if choice else {}
self._serve(monkeypatch, 0, **_IP_FORMAT, **tools)
assert (self.seen["req"].response_format is not None) is kept
@pytest.mark.parametrize("stream", [0, 1])
def test_web_search_domain_limits_reach_chats_loop(self, monkeypatch, stream):
search = {**_IP_WEB_SEARCH, "allowed_domains": ["docs.python.org/3"]}
_out, seen = self._serve(monkeypatch, stream, tools = [search])
assert seen["req"]._website_policy == {
"allowedDomains": ["docs.python.org"],
"blockedDomains": [],
}
_out, seen = self._serve(monkeypatch, stream, tools = [_IP_WEB_SEARCH])
assert seen["req"]._website_policy is None
@pytest.mark.parametrize("stream", [1, 0])
def test_server_tools_run_in_chats_loop_and_answer_as_on_llama_server(
self, monkeypatch, stream
):
frames = [
{"choices": [{"delta": {"reasoning_content": "plan"}}]},
{"choices": [{"delta": {"content": "Searching."}}]},
{
"type": "tool_start",
"tool_name": "web_search",
"tool_call_id": "c1",
"arguments": {},
},
{"type": "tool_status", "content": "Searching"},
{"type": "tool_end", "tool_name": "web_search", "tool_call_id": "c1", "result": "r"},
{"choices": [{"delta": {"reasoning_content": "ok"}}]},
{"choices": [{"delta": {"content": "Done."}, "finish_reason": "stop"}]},
{"choices": [], "usage": {"prompt_tokens": 9, "completion_tokens": 4}},
]
sse = "".join(f"data: {json.dumps(f)}\n\n" for f in frames) + "data: [DONE]\n\n"
choice = {"type": "tool", "name": "web_search_20250305", "disable_parallel_tool_use": True}
out, seen = self._serve(
monkeypatch, stream, sse = sse, tools = [_IP_WEB_SEARCH], tool_choice = choice
)
req = seen["req"]
assert seen["ui"] is True and seen["cap"] == {"web_search"}
assert req.enabled_tools == ["web_search"]
assert req.tool_choice == {"type": "function", "function": {"name": "web_search"}}
assert req.confirm_tool_calls is False and req.parallel_tool_calls is False
if stream:
starts = [
json.loads(e.split("data: ")[1]) for e in out.split("\n\n") if "block_start" in e
]
kinds = [s["content_block"]["type"] for s in starts]
assert out.endswith('{"type": "message_stop"}\n\n')
else:
body = json.loads(out)
kinds = [b["type"] for b in body["content"]]
assert body["usage"]["input_tokens"] == 9 and body["stop_reason"] == "end_turn"
tool_blocks = ["server_tool_use", "web_search_tool_result"]
assert kinds == ["thinking", "text", *tool_blocks, "thinking", "text"]