"""serve/frontend.py - plan v0.3 P8: the request/response layer between API clients and the engine.
Everything here is text: no model, no GPU. The engine consumes token ids and produces text deltas; this module
* normalizes OpenAI Chat Completions and Anthropic Messages requests into the chat template's message form,
* renders the model's own Jinja chat template (pack `tokenizer/chat_template.jinja`) exactly as Hugging Face
does (checked against the pack's `chat_golden.json`, 10 cases incl. thinking and tools),
* parses the streamed output incrementally into reasoning (`...`), content and tool calls in
the template's XML form (`VALUE...`),
never emitting a partial tag to the client.
Design note: plan v0.3 describes a single C++ process. The frontend is Python first because the template is Jinja
and the formats change often; the engine boundary is token ids in, text deltas out, which keeps a later C++ port
(or a pybind wrapper) a local change.
"""
from __future__ import annotations
import json
import logging
import os
import re
import uuid
from dataclasses import dataclass, field
from pathlib import Path
import jinja2
from jinja2.sandbox import ImmutableSandboxedEnvironment
LOGGER = logging.getLogger(__name__)
# ------------------------------------------------------------------------------------------------ template
class TemplateRequestError(jinja2.exceptions.TemplateError, ValueError):
"""The template refused the request's messages (e.g. "No user query found in messages."): a ValueError, so the
client gets a 400 with the template's message instead of a dropped connection (#365)."""
class ChatTemplate:
"""The model's chat template, rendered with the same Jinja settings as transformers' apply_chat_template."""
def __init__(self, path: str | Path):
def raise_exception(message):
raise TemplateRequestError(message)
def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False):
return json.dumps(x, ensure_ascii=ensure_ascii, indent=indent, separators=separators, sort_keys=sort_keys)
env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.loopcontrols"])
env.filters["tojson"] = tojson
env.globals["raise_exception"] = raise_exception
self.source = Path(path).read_text(encoding="utf-8")
self.template = env.from_string(self.source)
self.caps = self._detect_caps()
def render(self, messages: list[dict], tools: list[dict] | None = None, add_generation_prompt: bool = True,
**kwargs) -> str:
# Do not use completed empty assistant turns as examples for the next reply. Keep the final message.
# (#843. STRATA_KEEP_EMPTY_TURNS=1 renders them as before.)
messages = [m for i, m in enumerate(messages)
if i == len(messages) - 1 or os.environ.get("STRATA_KEEP_EMPTY_TURNS") == "1" or not (isinstance(m, dict) and m.get("role") == "assistant"
and not _text_of(m.get("content")).strip()
and not _has_image(m.get("content")) and not m.get("tool_calls"))]
return self.template.render(messages=messages, tools=tools, add_generation_prompt=add_generation_prompt,
**kwargs)
def _detect_caps(self) -> dict[str, bool]:
"""llama.cpp's capability names, checked at load time against this template and Strata's tool-call format.
These are rendering hints, not a guarantee that the model will follow a request."""
def render(messages, tools=None):
try:
return self.render(messages, tools=tools)
except Exception as exc: # noqa: BLE001 - a hint only: never stop the server for a probe
# A template may reject a role or feature (a custom one may fail in any way): the feature is off,
# discovery still answers for the others, and the start-up goes on.
LOGGER.debug("chat template capability probe failed: %s", exc, exc_info=True)
return ""
user = {"role": "user", "content": "strata_caps_user"}
tools = [{"name": f"strata_caps_call_{i}", "description": "strata_caps_description",
"parameters": {"type": "object", "properties": {"arg": {"type": "string"}}}}
for i in range(2)]
tool_prompt = render([user], tools)
def calls_supported(count):
calls = [{"name": f"strata_caps_call_{i}", "arguments": {"arg": f"strata_caps_arg_{i}"}}
for i in range(count)]
messages = [user, {"role": "assistant", "content": "",
"tool_calls": [{"function": call} for call in calls]}]
replies = [f"strata_caps_result_{i}" for i in range(count)]
messages += [{"role": "tool", "content": reply} for reply in replies] + [user]
prompt = render(messages, tools)
# Instructions include example XML and literal tag names, which are not model output. Parse only
# complete calls to our probe functions, using the same body parser as OutputParser.
bodies = re.findall(r"\s*(.*?)\s*",
prompt, re.S)
try:
parsed = [parse_tool_call(body) for body in bodies]
except ValueError:
return False
return [{"name": call.name, "arguments": call.arguments} for call in parsed] == calls \
and all(reply in prompt for reply in replies)
history = [user, {"role": "assistant", "content": "strata_caps_answer",
"reasoning_content": "strata_caps_reasoning"}, user]
return {"supports_tools": all(s in tool_prompt for s in
("strata_caps_call_0", "strata_caps_description", "", " str:
if content is None:
return ""
if isinstance(content, str):
return content
return "".join(part.get("text", "") for part in content if isinstance(part, dict) and part.get("type") in
("text", "input_text", None))
IMAGE_PARTS = ("image_url", "input_image", "image")
# Thinking levels. The model's template knows low, medium and xhigh (its default; "high" means xhigh), and
# enable_thinking=false for none. Clients spell these many ways; everything maps onto those four.
EFFORT = {"none": None, "off": None, "minimal": None, "disabled": None, "false": None,
"low": "low", "medium": "medium", "high": "xhigh", "xhigh": "xhigh", "max": "xhigh", "maximum": "xhigh"}
def effort_kwargs(value) -> dict:
"""A reasoning effort as given by a client -> the template's kwargs. Unknown values are a 400, not a crash."""
if value is None or value == "":
return {}
if value is False:
return {"enable_thinking": False}
key = str(value).strip().lower()
if key not in EFFORT:
raise ValueError(f"unknown reasoning effort {value!r}: use none, low, medium or high")
level = EFFORT[key]
return {"enable_thinking": False} if level is None else {"reasoning_effort": level}
def budget_effort(tokens) -> dict:
"""Anthropic's thinking budget (budget_tokens) -> a level: under 2K low, under 8K medium, else high."""
try:
n = int(tokens)
except (TypeError, ValueError):
return {}
return {"reasoning_effort": "low" if n < 2048 else "medium" if n < 8192 else "xhigh"}
def _has_image(content) -> bool:
return isinstance(content, list) and any(isinstance(p, dict) and p.get("type") in IMAGE_PARTS for p in content)
def _image_source(part: dict) -> str:
"""An image part's source as one string: a data: URL, an http(s) URL or a local file path.
OpenAI: {"type": "image_url", "image_url": {"url": ...}} (or "image_url": "..."), Responses-style
{"type": "input_image", "image_url": ...}; Anthropic: {"type": "image", "source": {"type": "base64",
"media_type": ..., "data": ...}} or {"source": {"type": "url", "url": ...}}."""
if part.get("type") == "image":
src = part.get("source") or {}
if src.get("type") == "base64":
return f"data:{src.get('media_type', 'image/png')};base64,{src.get('data', '')}"
return src.get("url") or src.get("path") or ""
url = part.get("image_url")
if isinstance(url, dict):
url = url.get("url")
return url or ""
def _parts_of(content):
"""Message content for the template: a string when there is no image (unchanged behaviour), otherwise the
template's list form - text items and image items, in order - whose image items carry their source."""
if not _has_image(content):
return _text_of(content)
items = []
for part in content:
if not isinstance(part, dict):
continue
if part.get("type") in IMAGE_PARTS:
items.append({"type": "image", "source": _image_source(part)})
elif part.get("type") in ("text", "input_text", None) and "text" in part:
items.append({"type": "text", "text": part.get("text", "")})
return items
def images_of(messages: list[dict]) -> list[str]:
"""The image sources of the rendered conversation, in prompt order (the template renders one
<|vision_start|><|image_pad|><|vision_end|> per image item, message by message)."""
return [item["source"] for m in messages if isinstance(m.get("content"), list)
for item in m["content"] if item.get("type") == "image"]
# #537: a literal / inside a message's text is plain text, not the model's reasoning markers. The
# tokenizer matches those two strings as their special tokens everywhere (GGUF token type 4, as llama.cpp does), so
# a user quoting "" used to hand the model a real end-of-reasoning token. Before the template is rendered
# they are swapped for these private-use characters, and the server encodes the spans they mark as ordinary text.
THINK_TAGS = {"": "\U000F0E01", "": "\U000F0E02"}
# #554: the vision markers the same way. They are special tokens only where the template writes them for an image
# item; the same strings in a message's text (an agent reading chat_template.jinja, a tool result quoting it) became
# the same special ids, so text with <|vision_start|><|image_pad|> before a picture took that picture's embeddings
VISION_TAGS = {"<|vision_start|>": "\U000F0E03", "<|image_pad|>": "\U000F0E04", "<|vision_end|>": "\U000F0E05",
"<|video_pad|>": "\U000F0E06"}
LITERAL_TAGS = {**THINK_TAGS, **VISION_TAGS}
THINK_MARKS = {v: k for k, v in LITERAL_TAGS.items()}
CONTROL_MARK0 = 0xF0E10 # the control tokens' marks start here, clear of the two sets above
def literal_tags(controls) -> dict[str, str]:
"""LITERAL_TAGS plus a mark for each control token's text (`controls`: the tokenizer's CONTROL literals,
<|im_start|>, <|im_end|>, <|endoftext|>, ...). The rendered prompt is encoded with those literals parsed, so one
written inside a message - a file an agent reads, a pasted chat template - opened or ended a turn there. Longest
first, as the tokenizer matches them: a literal inside a longer one is not marked before it."""
tags = {**LITERAL_TAGS, **{c: chr(CONTROL_MARK0 + k) for k, c in enumerate(c for c in controls
if c not in LITERAL_TAGS)}}
return dict(sorted(tags.items(), key=lambda t: -len(t[0])))
def _mark(text: str, tags: dict[str, str] = LITERAL_TAGS) -> str:
for tag, mark in tags.items():
text = text.replace(tag, mark)
return text
def _mark_deep(v, tags: dict[str, str] = LITERAL_TAGS):
if isinstance(v, str):
return _mark(v, tags)
if isinstance(v, dict):
return {k: _mark_deep(x, tags) for k, x in v.items()}
if isinstance(v, list):
return [_mark_deep(x, tags) for x in v]
return v
def _has_tag(v, tags: dict[str, str] = LITERAL_TAGS) -> bool:
if isinstance(v, str):
return any(tag in v for tag in tags)
if isinstance(v, dict):
return any(_has_tag(x, tags) for x in v.values())
if isinstance(v, list):
return any(_has_tag(x, tags) for x in v)
return False
def mark_think_literals(messages: list[dict], tools: list[dict] | None, tags: dict[str, str] = LITERAL_TAGS):
"""#537: (messages, tools) with every literal / (#554: and vision marker) in their text swapped for
LITERAL_TAGS' marks (or `tags`': literal_tags() adds the control tokens' texts), and
whether there was one (None: no change, the same objects back - a prompt without them renders as it always did).
An assistant message whose content opens with a whole ... block (clients that send the reasoning
inline) keeps that one block as the model's markers, as before."""
if not _has_tag(messages, tags) and not _has_tag(tools, tags):
return messages, tools, False
out = []
for m in messages:
m = dict(m)
content = m.get("content")
for k, v in m.items():
if k != "role":
m[k] = _mark_deep(v, tags)
if m.get("role") == "assistant" and isinstance(content, str) and content.lstrip().startswith("") \
and "" in content:
i, j = content.index("") + len(""), content.index("")
m["content"] = (content[:i] + _mark(content[i:j], tags) + "" +
_mark(content[j + len(""):], tags))
out.append(m)
return out, _mark_deep(tools, tags), True
_THINK_MARK_RE = re.compile("|".join(THINK_MARKS))
def unmark_think_literals(prompt: str, tags: dict[str, str] = LITERAL_TAGS) -> tuple[str, list[tuple[int, int]]]:
"""The rendered prompt with THINK_TAGS' marks turned back into the tags' text, and the (start, end) spans of those
tags in it: the server encodes them as ordinary text (the tokenizer's encode_plain_spans)."""
marks = THINK_MARKS if tags is LITERAL_TAGS else {v: k for k, v in tags.items()}
mark_re = _THINK_MARK_RE if tags is LITERAL_TAGS else re.compile("|".join(map(re.escape, marks)))
out, spans, pos, n = [], [], 0, 0
for m in mark_re.finditer(prompt):
out.append(prompt[pos:m.start()])
n += m.start() - pos
tag = marks[m.group(0)]
out.append(tag)
spans.append((n, n + len(tag)))
n += len(tag)
pos = m.end()
out.append(prompt[pos:])
return "".join(out), spans
def _late_system_to_user(messages: list[dict]) -> list[dict]:
"""The chat template takes a system message only at the start ("System message must be at the beginning").
Clients also send them mid-conversation - Claude Code's hook context as {"role": "system"} after the first user
turn, some OpenAI clients a late "developer" message (issue #56) - so those become user messages, in place:
merging them into the first one would change the prompt's start and cost the conversation cache every turn."""
return [dict(m, role="user") if m.get("role") == "system" and i > 0 else m for i, m in enumerate(messages)]
def _object_list(value, name: str) -> list[dict]:
"""#460: a request's "messages" (or a message's "tool_calls") as a list of objects. Some clients send the array
double-encoded, as a JSON string, which used to be iterated character by character and crashed on m.get: such a
string is decoded. Anything that is still not a list of objects is a ValueError, which the server answers with
a 400 naming the field. None (or no field) is an empty list, as a missing field always was."""
if value is None:
return []
if isinstance(value, str):
try:
value = json.loads(value)
except ValueError:
raise ValueError(f"{name} must be a list of objects (a string was sent that is not JSON)") from None
if not isinstance(value, list) or not all(isinstance(m, dict) for m in value):
raise ValueError(f"{name} must be a list of objects")
return value
def _tool_list(value, wrapper: str | None) -> list[dict]:
"""#592: a request's "tools" as a list of tool objects, each with a name - in the OpenAI shape
{"type": "function", "function": {"name": ...}} (`wrapper` "function"; a bare {"name": ...} is still taken), or
in the Anthropic shape {"name": ...} (`wrapper` None). A value that is not (a string such as "auto", a list of
names, an object without a name) is a ValueError - a 400 naming the field - where it used to take the request
thread down with no reply at all. No value (or an empty one) is no tools, as always. A tool's schema -
"parameters" in the OpenAI shape, "input_schema" in the Anthropic one - is an object when it is there, for the
same reason: a string or a list passed and raised later instead, on the model's first call of that tool."""
if not value:
return []
shape = ('{"type": "function", "function": {"name": ..., "parameters": {...}}}' if wrapper else
'{"name": ..., "input_schema": {...}}')
try:
tools = _object_list(value, "tools")
except ValueError:
raise ValueError(f"tools must be a list of tool objects, each {shape}") from None
key = "parameters" if wrapper else "input_schema"
for i, t in enumerate(tools):
fn = t.get(wrapper, t) if wrapper and t.get("type") == wrapper else t
if not isinstance(fn, dict) or not isinstance(fn.get("name"), str) or not fn["name"]:
raise ValueError(f"tools[{i}] has no name: each tool must be {shape}")
schema = fn.get(key)
if schema is not None and not isinstance(schema, dict):
# it reached parse_tool_call and the stream parser as the tool's schema, whose .get("properties") raised
# AttributeError in the request thread - after the 200 and whatever the model had said before the call
raise ValueError(f'tools[{i}] ({fn["name"]}): "{key}" must be an object (the JSON schema of its '
f"parameters), not {type(schema).__name__}")
return tools
def tool_arguments(raw) -> dict:
"""A call's arguments from a client's history -> the mapping the template renders (both APIs). A JSON object
(as text or already parsed) is used as it is; empty is {}; anything else (cut-off or malformed JSON, a list, a
number) is kept as text under "arguments" instead of refusing the whole conversation (#510)."""
if raw is None or (isinstance(raw, str) and not raw.strip()):
return {}
if isinstance(raw, dict):
return raw
if isinstance(raw, str):
try:
value = json.loads(raw)
except ValueError:
return {"arguments": raw}
return value if isinstance(value, dict) else {"arguments": raw}
return {"arguments": json.dumps(raw, ensure_ascii=False)}
def openai_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
"""OpenAI Chat Completions -> (template messages, template tools, template kwargs)."""
messages = []
for m in _object_list(req.get("messages"), "messages"):
role = m.get("role")
if role == "developer":
role = "system"
out = {"role": role, "content": _parts_of(m.get("content")) if role in ("user", "tool", "assistant") else _text_of(m.get("content"))}
if m.get("reasoning_content"):
out["reasoning_content"] = m["reasoning_content"]
if m.get("tool_calls"):
calls = []
for c in _object_list(m["tool_calls"], "tool_calls"):
fn = c.get("function", c)
if not isinstance(fn, dict):
raise ValueError("tool_calls must be a list of objects (each with a \"function\" object)")
calls.append({"function": {"name": fn.get("name"), "arguments": tool_arguments(fn.get("arguments"))}})
out["tool_calls"] = calls
messages.append(out)
tools = [t.get("function", t) if t.get("type") == "function" else t
for t in _tool_list(req.get("tools"), "function")] or None
kwargs = {}
# OpenAI Chat Completions: "reasoning_effort"; Responses style: "reasoning": {"effort": ...}
reasoning = req.get("reasoning") if isinstance(req.get("reasoning"), dict) else {}
kwargs.update(effort_kwargs(req.get("reasoning_effort") or reasoning.get("effort")))
# the vLLM / llama.cpp convention: {"chat_template_kwargs": {"enable_thinking": false, "reasoning_effort": "low"}}
for k, v in (req.get("chat_template_kwargs") or {}).items():
if k == "enable_thinking" and not v:
kwargs = {"enable_thinking": False}
elif k == "reasoning_effort" and "enable_thinking" not in kwargs:
kwargs.update(effort_kwargs(v))
return _late_system_to_user(messages), tools, kwargs
BILLING_HEADER = "x-anthropic-billing-header:"
def pin_billing_stamp(system: str) -> str:
"""Claude Code starts its system prompt with `x-anthropic-billing-header: cc_version=2.1.170.bf4;
cc_entrypoint=sdk-cli; cch=b145e;`. cch changes on EVERY request and the version's 4th part on every session, so
the prompt changed ~22K tokens in (after the tool list) on every turn and the conversation cache could only reuse
up to its last 16K checkpoint: half of every agent prompt was read again. Both stamps are pinned to f's, as
llama.cpp does (ggml-org/llama.cpp#21793); only a header at the very start of the system text is touched, and
only inside its first 160 characters."""
if not system.startswith(BILLING_HEADER):
return system
s = list(system)
cch = system.find("cch=", len(BILLING_HEADER))
if 0 <= cch <= 160:
v, end = cch + 4, system.find(";", cch + 4)
if end > v and end - v <= 16:
s[v:end] = "f" * (end - v)
cv = system.find("cc_version=")
if 0 <= cv <= 160:
v, end = cv + len("cc_version="), system.find(";", cv)
if v < end and end - v <= 64:
parts = system[v:end].split(".")
if len(parts) > 3:
tail = v + len(".".join(parts[:3])) + 1
s[tail:end] = "f" * (end - tail)
return "".join(s)
def anthropic_to_messages(req: dict, think_unasked: bool = True) -> tuple[list[dict], list[dict] | None, dict]:
"""Anthropic Messages -> (template messages, template tools, template kwargs). `think_unasked`: a request
without "thinking", an effort or a budget gets the template's default (it thinks), as through 0.1.31; False
renders it without thinking (#278, the config's "anthropic_thinking": "on_request")."""
messages = []
system = req.get("system")
if system:
messages.append({"role": "system", "content": pin_billing_stamp(_text_of(system))})
for m in _object_list(req.get("messages"), "messages"):
content = m.get("content")
if isinstance(content, str):
messages.append({"role": m["role"], "content": content})
continue
if m.get("role") == "user" and _has_image(content) and not any(
isinstance(b, dict) and b.get("type") == "tool_result" for b in content):
messages.append({"role": "user", "content": _parts_of(content)})
continue
text, reasoning, calls, parts = [], [], [], []
for block in content or []:
kind = block.get("type")
if kind == "text":
text.append(block.get("text", ""))
parts.append(block)
elif kind in IMAGE_PARTS:
parts.append(block)
elif kind == "thinking":
reasoning.append(block.get("thinking", ""))
elif kind == "tool_use":
calls.append({"function": {"name": block.get("name"), "arguments": tool_arguments(block.get("input"))}})
elif kind == "tool_result":
# A tool's image (Claude Code's Read of a picture) reaches the encoder like a user's, as the OpenAI
# path's tool messages already do; a text-only result is one string, as before.
messages.append({"role": "tool", "content": _parts_of(block.get("content"))})
if text or calls or reasoning or parts:
# an image sent beside tool results stays in this turn instead of being dropped
out = {"role": m["role"], "content": _parts_of(parts) if _has_image(parts) else "".join(text)}
if reasoning:
out["reasoning_content"] = "".join(reasoning)
if calls:
out["tool_calls"] = calls
messages.append(out)
tools = [{"name": t["name"], "description": t.get("description", ""), "parameters": t.get("input_schema", {})}
for t in _tool_list(req.get("tools"), None)] or None
kwargs = {}
# Anthropic: "thinking": {"type": "disabled"} or {"type": "enabled", "budget_tokens": N};
# "output_config": {"effort": "low" | "medium" | "high"}
thinking = req.get("thinking")
effort = (req.get("output_config") or {}).get("effort") if isinstance(req.get("output_config"), dict) else None
if isinstance(thinking, dict) and thinking.get("type") == "disabled":
kwargs["enable_thinking"] = False
elif effort:
kwargs.update(effort_kwargs(effort))
elif isinstance(thinking, dict) and thinking.get("budget_tokens"):
kwargs.update(budget_effort(thinking["budget_tokens"]))
elif thinking is None and not req.get("reasoning_budget_tokens") and not think_unasked:
# Opt-in (the config's "anthropic_thinking": "on_request"; the default thinks as 0.1.31 did, since a
# client that never asks would otherwise lose the thinking on every turn). Anthropic's thinking is
# opt-in there. Claude Code's helper calls (a session title, a topic check) ask for none
# and allow a few dozen tokens, which the model otherwise spent thinking and answered with no text at all.
# A config's reasoning_effort still applies: Service.with_shared sets output_config before this runs. A
# request that gives its own reasoning_budget_tokens (#123) asks for thinking, so it thinks as before.
kwargs["enable_thinking"] = False
return _late_system_to_user(messages), tools, kwargs
# ------------------------------------------------------------------------------------------------ output parser
@dataclass
class ToolCall:
name: str
arguments: dict
id: str = field(default_factory=lambda: "call_" + uuid.uuid4().hex[:24])
@dataclass
class Event:
kind: str # "reasoning" | "content" | "tool_call"
text: str = ""
call: ToolCall | None = None
THINK_END = ""
CALL_START = ""
CALL_END = ""
PARAM_END = ""
FUNC_START = " int:
"""Where a parameter value in `text` ends: the first `` followed (after whitespace) by the next
`` - the same text inside a value (a file that documents the call format, #210) is
part of the value. -1: none yet; -2: a candidate whose follower has not arrived (streaming; `final` accepts it)."""
at = text.find(PARAM_END)
while at >= 0:
after = text[at + len(PARAM_END):].lstrip()
if after.startswith((" int:
"""Where a tool call's body ends (#210): the `` after the call's own ``, found by walking
its parameters with param_end, so a value may contain either tag. -1: not complete yet. A body that is not in
the call format ends at the first ``, as before."""
s = text.lstrip()
pos = len(text) - len(s)
if not s.startswith("", pos)
if gt < 0:
return -1
pos = gt + 1
while True:
rest = text[pos:]
s = rest.lstrip()
pos += len(rest) - len(s)
if s.startswith("", pos)
if gt < 0:
return -1
end = param_end(text[gt + 1:])
if end < 0:
return -1
pos = gt + 1 + end + len(PARAM_END)
elif s.startswith(FUNC_END):
rest = text[pos + len(FUNC_END):]
s = rest.lstrip()
if s.startswith(CALL_END):
return pos + len(FUNC_END) + len(rest) - len(s)
return -1 if CALL_END.startswith(s) else text.find(CALL_END, pos)
elif not s or " tuple[str, str | None]:
"""A client's `tool_choice` in either API's shape -> ("auto" | "none" | "required" | "named", name). OpenAI:
"auto" / "none" / "required", {"type": "function", "function": {"name": N}} or the flat {"type": "function",
"name": N}; Anthropic: {"type": "auto" | "any" | "none"} and {"type": "tool", "name": N}. A value it does not
know is ("unknown", None)."""
if tool_choice is None:
return "auto", None
if isinstance(tool_choice, str):
return (tool_choice, None) if tool_choice in ("auto", "none", "required") else ("unknown", None)
if isinstance(tool_choice, dict):
kind = tool_choice.get("type")
if kind in ("auto", "none"):
return kind, None
if kind in ("any", "required"):
return "required", None
if kind in ("function", "tool"):
fn = tool_choice.get("function")
name = (fn.get("name") if isinstance(fn, dict) else None) or tool_choice.get("name")
if isinstance(name, str) and name:
return "named", name
return "unknown", None
def forced_call(tool_choice, tools: list[dict] | None) -> str | None:
"""`tool_choice` -> the text that opens the call the reply must make, or None (the model decides).
There is no grammar here: the server writes this opening itself, so the model can only go on with a call.
"required" / Anthropic "any": any of the tools; a named function: that one. A value it cannot honour (an
unknown shape, a name that is not one of the tools, "required" with no tools) is logged and acts as "auto",
not a 400: a client's odd choice must not stop its request. "none" is handled by the caller (no tools)."""
kind, name = tool_choice_of(tool_choice)
names = {t.get("name") for t in tools or [] if isinstance(t, dict)}
if kind in ("auto", "none"):
return None
if kind == "required" and len(names) == 1: # one tool to call: name it, the model cannot invent another
kind, name = "named", next(iter(names))
if kind == "required" and names:
return CALL_START + "\n\n"
print(f"[strata] tool_choice {json.dumps(tool_choice)[:200]} is not supported here (or names no tool of the "
"request): the model decides, as with \"auto\"", flush=True)
return None
PARAM_START = " int:
"""Where a `...` at the start of `text` ends (just past ``), walking its
parameters with param_end like call_end does; -1: not complete (or not in the call format)."""
s = text.lstrip()
pos = len(text) - len(s)
if not s.startswith(FUNC_START):
return -1
gt = text.find(">", pos)
if gt < 0:
return -1
pos = gt + 1
while True:
rest = text[pos:]
s = rest.lstrip()
pos += len(rest) - len(s)
if s.startswith(PARAM_START):
gt = text.find(">", pos)
if gt < 0:
return -1
end = param_end(text[gt + 1:])
if end < 0:
return -1
pos = gt + 1 + end + len(PARAM_END)
elif s.startswith(FUNC_END):
return pos + len(FUNC_END)
else:
return -1
def json_tool_call(body: str, schemas: dict) -> ToolCall | None:
"""`{"name": N, "arguments": {...}}` (or "parameters", or the arguments as a JSON string) of a declared tool -> its
call; anything else -> None."""
try:
obj = json.loads(body)
except ValueError:
return None
if not isinstance(obj, dict) or not isinstance(obj.get("name"), str) or obj["name"] not in schemas:
return None
args = obj.get("arguments", obj.get("parameters", {}))
if isinstance(args, str):
try:
args = json.loads(args)
except ValueError:
return None
if args is None:
args = {}
return ToolCall(name=obj["name"], arguments=args) if isinstance(args, dict) else None
def parse_tool_call(body: str, schema: dict | None = None) -> ToolCall:
"""`\\n\\nVALUE\\n...` -> ToolCall. Values are JSON-decoded
when the tool's schema says the parameter is not a string (or, without a schema, when they parse as JSON
objects/arrays/numbers/booleans)."""
body = body.strip()
if not body.startswith("" not in body:
raise ValueError("malformed tool call: " + body[:80])
name = body[len("")]
rest = body[body.index(">") + 1:]
props = ((schema or {}).get("parameters") or {}).get("properties") or {}
args = {}
while "")]
rest = rest[rest.index(">") + 1:]
end = param_end(rest, final=True)
value = rest[:end] if end >= 0 else rest
rest = rest[end + len(PARAM_END):] if end >= 0 else ""
if value.startswith("\n"):
value = value[1:]
if value.endswith("\n"):
value = value[:-1]
declared = (props.get(pname) or {}).get("type")
if declared == "string":
args[pname] = value
else:
try:
args[pname] = json.loads(value)
except ValueError:
args[pname] = value
return ToolCall(name=name, arguments=args)
RCALL_MAX = 32768 # the most of a `` inside the reasoning that is held while waiting for its end (#804)
class OutputParser:
"""Incremental parser of the model's text. Feed deltas; get events. A tag split across deltas is held back
until it is complete, so clients never see ``. The
# reasoning text before them is tracked (code fence, inline code, current line) to tell an act from a quote.
self.pending: list[list] = []
self.rescued = 0 # calls delivered from the reasoning
self.refused = 0 # declared calls kept as reasoning text (quoted, or the turn was cut)
self.fence, self.line, self.ticks = "", "", 0
# recover (opt-in, the config's "tool_call_recovery"): a declared tool's call written next to the template's
# form is the call - `` as the opener, JSON inside , a bare at the
# start of a line outside code, or a second call in the same (which otherwise merges into one).
self.recover = recover
self.bare = False # the call being read opened with around it)
self.batch = False # the call being finished is followed by another in the same wrapper
self._reset_scan()
def _ok_at(self, p: int) -> bool:
"""self.buf[p] would open a call: at the start of a line, outside a code fence and inline code."""
snap = (self.fence, self.line, self.ticks)
self._track(self.buf[:p])
ok = self._opener_ok()
self.fence, self.line, self.ticks = snap
return ok
def _follower(self, after: str) -> str:
"""recover: what a `` followed (after whitespace) by `after` opens: "call" (``), "json" (`{`), "wait" (not decided yet) or "text"."""
if after.startswith(FUNC_START):
return "call"
if after.startswith(PARAM_START):
gt = after.find(">")
if gt < 0:
return "wait" if "\n" not in after else "text"
return "param" if after[len(PARAM_START):gt] in self.schemas else "text"
if after.startswith("{"):
return "json" if self.schemas else "text"
if not after or FUNC_START.startswith(after) or PARAM_START.startswith(after):
return "wait"
return "text"
def _bare_opener(self) -> tuple[int, bool]:
"""recover: the first ` (position, decided); (-1, False) for none. A name still arriving is undecided."""
p = self.buf.find(FUNC_START)
while p >= 0:
if self._ok_at(p):
gt = self.buf.find(">", p)
name = self.buf[p + len(FUNC_START):gt if gt >= 0 else len(self.buf)]
if gt < 0 and not any(c.isspace() for c in name):
return p, False
if gt >= 0 and name in self.schemas:
return p, True
p = self.buf.find(FUNC_START, p + 1)
return -1, False
def _track(self, text: str) -> str:
"""Follow the reasoning text that has gone out: the open code fence, the current line, and the backticks of
the current paragraph. Returns the text."""
parts = text.split("\n")
for k, part in enumerate(parts):
if k < len(parts) - 1:
line, self.line = self.line + part, ""
s = line.lstrip()
if self.fence:
if s.startswith(self.fence * 3):
self.fence = ""
elif s[:3] in ("```", "~~~") and (s[0] * 3) not in s[3:]:
self.fence = s[0]
elif not s:
self.ticks = 0
else:
self.ticks += line.count("`")
else:
self.line += part
return text
def _opener_ok(self) -> bool:
"""The reasoning text so far puts a `` at the start of a line, outside a code fence and outside
inline code."""
return not self.fence and not self.line.strip() and self.ticks % 2 == 0
def _in_code(self) -> bool:
"""The text so far leaves the next character inside a code fence or inline code."""
return bool(self.fence) or (self.ticks + self.line.count("`")) % 2 == 1
def _release(self, deliver: bool) -> list[Event]:
"""Settle the calls waiting in self.pending: events for real calls, or all of it back as reasoning text."""
out = []
for raw, call in self.pending:
if call is not None and deliver:
out.append(Event("tool_call", call=call))
self.rescued += 1
elif call is None or not deliver:
if call is not None:
self.refused += 1
if raw:
out.append(Event("reasoning", self._track(raw)))
self.pending = []
return out
def _reset_scan(self):
self.sp = 0 # how much of self.buf (the call body) the scanner has consumed
self.ss = "name" # name -> between -> str|raw -> ... -> done
self.scall = None # the ToolCall being streamed (its id is reused by the final event)
self.sfirst = True
self.sval_started = False
self.sdeclared = {}
def _scan(self) -> list[Event]:
"""Advance the streaming view of the call body in self.buf (see stream_tools)."""
out = []
def args(s):
if s:
out.append(Event("tool_args", s, call=self.scall))
while True:
rest = self.buf[self.sp:]
if self.ss == "name":
a = rest.find("", a + 10) if a >= 0 else -1
if b < 0:
return out
name = rest[a + 10:b]
self.scall = ToolCall(name=name, arguments={})
props = ((self.schemas.get(name) or {}).get("parameters") or {}).get("properties") or {}
self.sdeclared = {k: (v or {}).get("type") for k, v in props.items()}
out.append(Event("tool_start", call=self.scall))
args("{")
self.sp += b + 1
self.ss = "between"
elif self.ss == "between":
stripped = rest.lstrip()
self.sp += len(rest) - len(stripped)
if stripped.startswith("")
if b < 0:
return out
pname = stripped[11:b]
args(("" if self.sfirst else ",") + json.dumps(pname) + ":")
self.sfirst = False
self.sp += b + 1
if self.sdeclared.get(pname) == "string":
args('"')
self.ss, self.sval_started = "str", False
else:
self.ss = "raw"
elif stripped.startswith(""):
args("}")
self.sp += len("")
self.ss = "done"
else:
return out # a tag still arriving (or trailing text): wait
elif self.ss == "str":
if not self.sval_started:
if not rest:
return out
if rest[0] == "\n": # the value's leading newline is not part of it
self.sp += 1
rest = rest[1:]
self.sval_started = True
end = param_end(rest)
if end >= 0:
value = rest[:end]
if value.endswith("\n"):
value = value[:-1]
args(json.dumps(value)[1:-1] + '"')
self.sp += end + len(PARAM_END)
self.ss = "between"
continue
# an undecided (-2) is held from its start, like a tag still arriving
safe = rest.find(PARAM_END) if end == -2 else len(rest) - self._hold(rest, (PARAM_END,))
if safe > 0 and rest[safe - 1] == "\n": # may be the trailing newline before
safe -= 1
if safe > 0:
args(json.dumps(rest[:safe])[1:-1])
self.sp += safe
return out
elif self.ss == "raw":
end = param_end(rest)
if end < 0:
return out
value = rest[:end]
if value.startswith("\n"):
value = value[1:]
if value.endswith("\n"):
value = value[:-1]
try:
v = json.loads(value)
except ValueError:
v = value
args(json.dumps(v, ensure_ascii=False))
self.sp += end + len(PARAM_END)
self.ss = "between"
else:
return out
def _close_scan(self) -> list[Event]:
"""A streamed call that ended without a clean : close its JSON so clients can still parse it."""
out = []
if self.scall is None or self.ss == "done":
return out
tail = ""
if self.ss == "str":
tail += '"'
elif self.ss == "raw":
tail += json.dumps(self.buf[self.sp:].strip("\n"))
tail += "}"
out.append(Event("tool_args", tail, call=self.scall))
self.ss = "done"
return out
def _hold(self, text: str, tags: tuple[str, ...]) -> int:
"""Length of the longest suffix of `text` that is a proper prefix of one of `tags`."""
best = 0
for tag in tags:
for n in range(1, len(tag)):
if text.endswith(tag[:n]):
best = max(best, n)
return best
def feed(self, delta: str) -> list[Event]:
self.buf += delta
out: list[Event] = []
while True:
if self.state == "rcall":
# #804: a `` inside the reasoning, with tools declared. It is held whole (never streamed
# as a call) until it ends: a declared name is then a tool call, anything else stays reasoning text.
body = self.buf[len(CALL_START):]
end, think = call_end(body), body.find(THINK_END)
if end >= 0 and (think < 0 or think >= end):
call = None
try:
name = body[:end].strip()[len("", 1)[0]
if name in self.schemas:
call = parse_tool_call(body[:end], self.schemas.get(name))
except ValueError:
pass
raw = self.buf[:len(CALL_START) + end + len(CALL_END)]
if call is not None or self.pending:
self.pending.append([raw, call]) # settled by what follows it (see __init__)
else:
out.append(Event("reasoning", self._track(raw)))
self.buf = body[end + len(CALL_END):]
self.state = "reasoning"
elif think >= 0: # the thinking ended inside it: it never was a call
out += self._release(False)
out.append(Event("reasoning", self._track(self.buf[:len(CALL_START) + think])))
self.buf = body[think:]
self.state = "reasoning"
elif len(body) > RCALL_MAX: # a tag in the prose that never closes: stop holding the thinking back
out += self._release(False)
out.append(Event("reasoning", self._track(self.buf)))
self.buf = ""
self.state = "reasoning"
else:
return out
elif self.state == "reasoning":
if self.pending:
# calls wait for what follows: more calls or whitespace keep them, `` makes them acts,
# any other text means they were quoted
stripped = self.buf.lstrip()
if len(stripped) < len(self.buf):
self.pending[-1][0] += self.buf[:len(self.buf) - len(stripped)]
self.buf = stripped
if not self.buf:
return out
if self.buf.startswith(CALL_START):
self.state = "rcall"
elif self.buf.startswith(THINK_END):
out += self._release(True)
elif CALL_START.startswith(self.buf) or THINK_END.startswith(self.buf):
return out
else:
out += self._release(False)
continue
i = self.buf.find(THINK_END)
tool = self.buf.find(CALL_START) if self.schemas else -1
if tool >= 0 and (i < 0 or tool < i):
if tool:
out.append(Event("reasoning", self._track(self.buf[:tool])))
if self._opener_ok():
self.buf = self.buf[tool:]
self.state = "rcall"
else: # mid-sentence, in a fence or in inline code: a quote
out.append(Event("reasoning", self._track(CALL_START)))
self.buf = self.buf[tool + len(CALL_START):]
continue
if i < 0:
keep = self._hold(self.buf, (THINK_END, CALL_START) if self.schemas else (THINK_END,))
if len(self.buf) > keep:
out.append(Event("reasoning", self._track(self.buf[:len(self.buf) - keep])))
self.buf = self.buf[len(self.buf) - keep:]
return out
if i:
out.append(Event("reasoning", self._track(self.buf[:i])))
self.buf = self.buf[i + len(THINK_END):]
self.state, self.lead = "content", True
self.fence, self.line, self.ticks = "", "", 0 # the answer's own text starts here
elif self.state == "content":
if self.lead: # newlines right after or a call
stripped = self.buf.lstrip("\n")
if not stripped:
self.buf = ""
return out
self.buf, self.lead = stripped, False
i = self.buf.find(CALL_START)
b, decided = self._bare_opener() if self.recover and self.schemas else (-1, False)
if b >= 0 and (i < 0 or b < i):
# recover: alone at the start of a line - the call without its .
# Until the name has arrived it is held.
j = b
while j > 0 and self.buf[j - 1] == "\n":
j -= 1
if j > 0 and self.buf[:j].strip():
out.append(Event("content", self._track(self.buf[:j])))
if not decided:
self.buf = self.buf[j:]
return out
self.buf = self.buf[b:]
self.state, self.bare = "call", True
continue
if i < 0:
# Hold a partial tag AND the newlines before it: if a tool call follows, they are dropped,
# so emitting them early would make streamed and whole outputs differ.
j = len(self.buf) - self._hold(self.buf, (CALL_START,))
if self.recover and self.schemas: # a line that may be becoming a bare 0 and self.buf[j - 1] == "\n":
j -= 1
if j > 0:
out.append(Event("content", self._track(self.buf[:j])))
self.buf = self.buf[j:]
return out
# #1058: an opener inside a code fence or inline code is text (a quoted example), never a call. The
# text before it decides, so streamed and whole outputs agree. Mid-sentence openers still count.
snap = (self.fence, self.line, self.ticks)
self._track(self.buf[:i])
in_code = self._in_code()
self.fence, self.line, self.ticks = snap
if in_code:
out.append(Event("content", self._track(self.buf[:i + len(CALL_START)])))
self.buf = self.buf[i + len(CALL_START):]
continue
# A call is `` and then (after whitespace) ` block") - content, not a malformed call
# that ends the request. Until its follower has arrived it is held, like a partial tag.
after = self.buf[i + len(CALL_START):].lstrip()
kind = self._follower(after) if self.recover else None
if kind in ("param", "json"):
if i and self.buf[:i].strip():
out.append(Event("content", self._track(self.buf[:i].rstrip("\n"))))
rest = self.buf[i + len(CALL_START):]
if kind == "param": # as the opener: the call's
at = len(rest) - len(after)
rest = rest[:at] + FUNC_START + after[len(PARAM_START):]
self.buf = rest
self.state = "call" if kind == "param" else "json"
continue
if kind == "wait":
after = ""
if after and not after.startswith(FUNC_START) and not FUNC_START.startswith(after):
out.append(Event("content", self._track(self.buf[:i + len(CALL_START)])))
self.buf = self.buf[i + len(CALL_START):]
continue
if not after.startswith(FUNC_START):
j = i
while j > 0 and self.buf[j - 1] == "\n":
j -= 1
if j > 0:
out.append(Event("content", self._track(self.buf[:j])))
self.buf = self.buf[j:]
return out
if i and self.buf[:i].strip():
out.append(Event("content", self._track(self.buf[:i].rstrip("\n"))))
self.buf = self.buf[i + len(CALL_START):]
self.state = "call"
elif self.state == "json":
# recover: `{"name": ..., "arguments": {...}}` inside the wrapper is the call once its has
# arrived and it names a declared tool; anything else is the text it is
i = self.buf.find(CALL_END)
if i < 0:
return out
call = json_tool_call(self.buf[:i].strip(), self.schemas)
if call is None:
out.append(Event("content", self._track(CALL_START + self.buf[:i + len(CALL_END)])))
else:
out.append(Event("tool_call", call=call))
self.buf = self.buf[i + len(CALL_END):]
self.state, self.lead = "content", call is not None
else:
drop = None
if self.bare: # no wrapper: the call ends at its ; a stray
i = function_end(self.buf) # after it is the wrapper's closer, dropped
if i >= 0:
after = self.buf[i:].lstrip()
if after.startswith(CALL_END):
drop = len(self.buf[i:]) - len(after) + len(CALL_END)
elif after and not CALL_END.startswith(after):
drop = 0
else:
i = call_end(self.buf)
if i >= 0:
drop = len(CALL_END)
if self.recover: # a batch in one wrapper: the next call follows this one's
f = function_end(self.buf) # (or opens as )
nxt = self.buf[f:].lstrip() if f >= 0 else ""
if nxt.startswith(PARAM_START) and self._follower(nxt) == "param":
at = len(self.buf) - len(nxt)
self.buf = self.buf[:at] + FUNC_START + nxt[len(PARAM_START):]
nxt = self.buf[at:]
if nxt.startswith(FUNC_START):
i, drop, self.batch = f, 0, True
if self.stream_tools:
if i >= 0:
whole, self.buf = self.buf, self.buf[:i] # scan only the body
out += self._scan()
out += self._close_scan()
self.buf = whole
else:
out += self._scan()
if i < 0 or drop is None:
return out
body = self.buf[:i]
self.buf = self.buf[i + drop:]
batch, self.batch = self.batch, False
out += self._finish_call(body)
if batch and out[-1].kind == "tool_call":
self.state, self.lead = "call", False
def finish(self, reason: str | None = None) -> list[Event]:
"""End of generation: flush whatever is held (an unterminated tool call is returned as content; one that was
already announced stays unfinished: its JSON is not closed and no "tool_call" follows it, #211). `reason` is
how the turn ended: calls waiting from the reasoning become real calls only on a natural stop (None = stop);
a turn cut by max tokens (or cancelled, or failed) keeps them as reasoning text (#1058)."""
out = []
if self.pending:
out += self._release(reason in (None, "stop") and self.state == "reasoning" and not self.buf)
if self.state == "call" and self.bare and function_end(self.buf) >= 0:
i = function_end(self.buf) # recover: a bare call is whole at its
body, rest = self.buf[:i], self.buf[i:].strip()
self.buf = ""
out += self._finish_call(body)
if rest and not CALL_END.startswith(rest):
out.append(Event("content", self._track(rest)))
return out
if self.state == "call" and self.stream_tools and self.scall is not None:
out += self._scan() # the output ended inside a call that was already announced
if self.ss == "done": # only its is missing: the call itself is whole
out.append(Event("tool_call", call=self.scall))
self.buf = ""
self._reset_scan()
return out
if self.buf:
# an unfinished call inside the reasoning (#804) is reasoning text, never a call
kind = {"reasoning": "reasoning", "rcall": "reasoning", "content": "content"}.get(self.state, "content")
text = CALL_START + self.buf if self.state in ("call", "json") and not self.bare else self.buf
out.append(Event(kind, text))
self.buf = ""
return out
def _finish_call(self, body: str) -> list[Event]:
"""A whole call body in the template's form -> its tool_call event. With recover, one that still does not
parse is the text it is instead of an error that ends the request."""
name = body.strip()[len(FUNC_START):].split(">", 1)[0]
bare, self.bare = self.bare, False
try:
call = parse_tool_call(body, self.schemas.get(name))
except ValueError:
if not self.recover:
raise
self._reset_scan()
self.state = "content"
return [Event("content", self._track(body if bare else CALL_START + body + CALL_END))]
if self.scall is not None:
call.id = self.scall.id
self._reset_scan()
self.state, self.lead = "content", True
return [Event("tool_call", call=call)]