Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
feat(serve): forward the per-call controls /v1/systemone already forwards on /v1/systemone/batch
Aashish89f15bd35fbe
diff --git a/laya/serve.py b/laya/serve.pyindex 59a30753..36eb147d 100644--- a/laya/serve.py+++ b/laya/serve.py@@ -88,6 +88,20 @@ DEFAULT_MAX_TOKEN_BUDGET = 8192BODY_CONTROLS = ("model", "max_len", "head_max_len", "task", "lang", "lang_guess", "min_confidence")BODY_REFUSALS = ("hooks", "on_predict_start", "on_predict_end", "hooks_raise", "hooks_timeout")+# ``Router.predict_batch`` reads two different kinds of control: call-level keyword arguments that+# apply to the whole batch (chunking, padding, the abstention threshold) and per-request keys that+# it lifts off each item of ``requests`` (checkpoint, task, language hint, token budget). The batch+# endpoint keeps them in separate tuples so a caller-visible JSON body -- one set of controls that+# applies to every state it sent -- is turned into both. ``tests/test_serve.py`` pins the two lists+# against ``inspect.signature(Router.predict_batch)`` (call-level) and against the MCP tool's own+# item key list (per-request), so a control added to either side has to be placed on one side of+# this split -- forwarded, refused, or moved to the item tuple -- before the suite goes green.+# ``hooks_timeout`` is refused on the batch path the same way it is on the single path: it belongs+# to ``predict_batch``'s call-level args but governs how the *deployment's* hooks execute, so a+# caller cannot be allowed to shorten or lengthen that deadline from an HTTP body.+BATCH_BODY_CALL_CONTROLS = ("batch_size", "min_confidence", "sort_by_length")+BATCH_BODY_ITEM_CONTROLS = ("max_len", "head_max_len", "task", "lang", "lang_guess")+def _env_bool(name: str, default: bool) -> bool:v = os.environ.get(name)@@ -239,6 +253,52 @@ def _validate_min_confidence(body: Dict[str, Any]) -> Optional[float]:raise HTTPException(status_code=422, detail=str(error)) from None+def _validate_batch_size_param(body: Dict[str, Any]) -> Optional[int]:+ """Validate the optional ``batch_size`` call argument on the batch endpoint (422).++ ``Router.predict_batch`` reads it as the maximum number of states per Agent forward-pass batch,+ so a non-positive or fractional value would either mean "one state per pass" or crash torch.+ A boolean would be truthy and coerce to 1/0, which is not what the caller meant; refusing here+ matches what the MCP tool's own validator does at ``laya/mcp/tools.py:_validate_batch_size``,+ so the same typo is rejected the same way on both surfaces.+ """+ from fastapi import HTTPException++ if "batch_size" not in body:+ return None+ val = body["batch_size"]+ if val is None:+ return None+ if not isinstance(val, int) or isinstance(val, bool):+ raise HTTPException(status_code=422, detail="batch_size must be an integer")+ if val < 1:+ raise HTTPException(status_code=422,+ detail="batch_size must be a positive integer, got %r" % (val,))+ return val+++def _validate_sort_by_length_param(body: Dict[str, Any]) -> Optional[bool]:+ """Validate the optional ``sort_by_length`` call argument on the batch endpoint (422).++ ``Router.predict_batch`` forwards this verbatim to every ``Agent.predict_batch`` call, and an+ agent that predates the knob drops it. Only the boolean is a meaningful value here -- a string+ would be truthy and take the sort path silently. When the caller asks for ``False`` we still+ forward ``None`` rather than ``False``, because ``predict_batch``'s own default is ``False``:+ an absent argument means "the caller did not ask" and cannot override a deployment that built+ its Router with the knob already on.+ """+ from fastapi import HTTPException++ if "sort_by_length" not in body:+ return None+ val = body["sort_by_length"]+ if val is None:+ return None+ if not isinstance(val, bool):+ raise HTTPException(status_code=422, detail="sort_by_length must be a boolean")+ return val++def _resolve_max_loaded() -> Optional[int]:"""Resident-checkpoint cap from ``LAYA_MAX_LOADED``; ``None`` leaves it to ``Router``.@@ -798,7 +858,55 @@ def create_app(router: Optional[Any] = None):states = body["states"]questions = body["questions"]_check_batch_limits(states, questions)+ # Same refusal policy as the single endpoint: hooks and the two hook-execution knobs+ # belong to the deployment, not the caller. `predict_batch` does take `hooks_timeout`,+ # so without this call a batch body would silently hand a caller a shorter deadline for+ # the operator's own hooks.+ _refuse_body_refusals(body)model = _resolve_model(body.get("model"))+ max_budget_cap = _resolve_max_token_budget()+ max_len = _validate_budget_param(body, "max_len", max_budget_cap)+ head_max_len = _validate_budget_param(body, "head_max_len", max_budget_cap)+ # Every control is sent only when the caller sent it, matching `_systemone_inner`: an+ # absent argument means "inherit what the Router was built with", so passing None would+ # override a deployment's own `Router(lang_guess=...)` or abstention threshold.+ # ``Router.predict_batch`` reads task / lang / lang_guess / max_len / head_max_len off+ # each item of ``requests`` (see its docstring), so they go into every synthesized+ # request dict; the HTTP body has one control set for the whole batch, which the Router+ # still honours -- it groups the requests by these values for the forward pass.+ item_overrides: Dict[str, Any] = {}+ if max_len is not None:+ item_overrides["max_len"] = max_len+ if head_max_len is not None:+ item_overrides["head_max_len"] = head_max_len+ # An unknown task is left to `route_batch`, which normalises it through `normalise_name`+ # and raises; the `except ValueError` below turns that into a 422 naming the task, so the+ # accepted set is core's and not a list restated here.+ if body.get("task") is not None:+ item_overrides["task"] = body["task"]+ for key in ("lang", "lang_guess"):+ value = _validate_language_param(body, key)+ if value is not None:+ item_overrides[key] = value+ # Call-level: `predict_batch` takes min_confidence / batch_size / sort_by_length as kwargs.+ call_kwargs: Dict[str, Any] = {}+ min_confidence = _validate_min_confidence(body)+ if min_confidence is not None:+ call_kwargs["min_confidence"] = min_confidence+ batch_size = _validate_batch_size_param(body)+ if batch_size is not None:+ call_kwargs["batch_size"] = batch_size+ sort_by_length = _validate_sort_by_length_param(body)+ if sort_by_length:+ # Only `True` is forwarded: `predict_batch`'s own default is `False`, and an attached+ # agent whose `predict_batch` predates the knob (#294) silently drops it. The MCP+ # tool makes the same choice for the same reason (see ``laya/mcp/tools.py``).+ call_kwargs["sort_by_length"] = True+ # The predict() fallback shape: `Router.predict` reads all six controls as call kwargs,+ # so the item dict is flattened back and `min_confidence` joins it.+ predict_kwargs: Dict[str, Any] = dict(item_overrides)+ if min_confidence is not None:+ predict_kwargs["min_confidence"] = min_confidenceif gate is None:gate = asyncio.Lock()try:@@ -807,8 +915,12 @@ def create_app(router: Optional[Any] = None):def _do_batch():if hasattr(router, "predict_batch"):- reqs = [{"state": s, "questions": questions, "model": model} for s in states]- results = router.predict_batch(reqs)+ reqs = [dict(state=s, questions=questions, model=model, **item_overrides)+ for s in states]+ results = router.predict_batch(reqs, **call_kwargs)+ elif predict_kwargs:+ results = [router.predict(s, questions, model=model, **predict_kwargs)+ for s in states]else:results = [router.predict(s, questions, model=model) for s in states]total_tokens = sum(r.get("usage", {}).get("input_tokens", 0) for r in results)diff --git a/tests/test_serve.py b/tests/test_serve.pyindex e41dc9f8..5bc55d80 100644--- a/tests/test_serve.py+++ b/tests/test_serve.py@@ -19,6 +19,8 @@ from fastapi.testclient import TestClient # noqa: E402ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))from laya.serve import ( # noqa: E402+ BATCH_BODY_CALL_CONTROLS,+ BATCH_BODY_ITEM_CONTROLS,BODY_CONTROLS,BODY_REFUSALS,DEFAULT_MAX_TOKEN_BUDGET,@@ -1747,3 +1749,302 @@ def test_http_api_page_documents_exactly_the_forwarded_controls():prose = page[page.index("`model`, `task`, `lang`, `lang_guess`"):]for key in BODY_REFUSALS:assert "`%s`" % key in prose, "%s is not named where the refusal is explained" % key+++# --------------------------------------------------------------------- batch endpoint per-call controls+#+# `Router.predict_batch` reads two kinds of control: call-level kwargs that apply to the whole+# batch (chunking, padding, abstention) and per-request keys it lifts off each item of ``requests``+# (checkpoint, task, language, token budget). Before this PR, the HTTP batch endpoint read neither+# -- it always synthesized a plain ``{state, questions, model}`` dict and called ``predict_batch``+# with no kwargs, so a caller could not send any of the controls the single endpoint already+# forwards. The suite below pins the two split lists against core's own signature, verifies every+# control reaches the right side of the Router call, and refuses a hook control the same way+# ``/v1/systemone`` already does.+++class BatchRecordingRouter(FakeRouter):+ """Records ``predict_batch``'s full call: the requests list and every call kwarg."""++ def __init__(self):+ super().__init__()+ self.batch_calls = []++ def predict_batch(self, requests, **kwargs):+ self.batch_calls.append(([dict(r) for r in requests], dict(kwargs)))+ return [+ {+ "model": "laya-rl-agent",+ "answers": {+ "dept": {+ "type": "choice",+ "choice": "billing",+ "probabilities": {"billing": 0.94, "tech": 0.06},+ "confidence": 0.94,+ }+ },+ "usage": {"input_tokens": 42, "output_tokens": 0},+ "routing": {"model": "english", "reason": "English Latin text"},+ }+ for _ in requests+ ]+++class StrictBatchRouter(BatchRecordingRouter):+ """``predict_batch(self, requests)`` -- takes no call kwargs.++ Witness against an always-forward mutation: if the handler unconditionally passed ``batch_size``+ or ``min_confidence``, this Router would raise ``TypeError`` and 500 on a bare body.+ """++ def predict_batch(self, requests): # noqa: D401 -- strict on purpose+ return super().predict_batch(requests)+++BATCH_CALL_VALUES = {"batch_size": 4, "min_confidence": 0.9, "sort_by_length": True}+BATCH_ITEM_VALUES = {"max_len": 64, "head_max_len": 32, "task": "typed-decisions",+ "lang": "de", "lang_guess": "de"}+++def _batch_client(monkeypatch):+ monkeypatch.delenv("LAYA_API_KEY", raising=False)+ fake = BatchRecordingRouter()+ return TestClient(create_app(router=fake)), fake+++def test_batch_call_controls_are_exactly_predict_batch_kwargs_or_refusal():+ """A control added to ``Router.predict_batch`` must be placed on one side of the line.++ Either serve forwards it (``BATCH_BODY_CALL_CONTROLS``) or it refuses it (``hooks_timeout``,+ the one hook-execution arg that ``predict_batch`` takes). Silently ignoring a new kwarg is the+ failure mode this pin exists to catch: the same reasoning that made ``/v1/systemone`` refuse+ ``hooks_timeout`` in #724 applies to the batch path -- a caller cannot shorten the deadline+ on which the operator's own hooks execute.+ """+ from laya.router import Router++ taken = set(inspect.signature(Router.predict_batch).parameters) - {"self", "requests"}+ declared = set(BATCH_BODY_CALL_CONTROLS) | {"hooks_timeout"}+ assert taken == declared, "predict_batch() takes %s; serve declares %s" % (+ sorted(taken), sorted(declared))+++def test_batch_controls_cover_the_single_endpoints_set_plus_batch_only():+ """Everything the single endpoint forwards must reach the batch path too, and vice versa.++ The batch forwards the same per-call controls ``/v1/systemone`` does -- split across the item+ dict (``Router.predict_batch`` reads them per item) and the call kwargs (min_confidence) --+ plus the batch-only chunking and padding knobs (``batch_size``, ``sort_by_length``) that+ ``predict_batch`` accepts and ``predict`` does not. ``model`` still goes into every item, as+ it has since the batch endpoint shipped. If a control moves across either boundary, this+ test names it.+ """+ batch_side = set(BATCH_BODY_ITEM_CONTROLS) | set(BATCH_BODY_CALL_CONTROLS)+ single_side = (set(BODY_CONTROLS) | {"batch_size", "sort_by_length"}) - {"model"}+ assert batch_side == single_side, (+ "batch forwards %s; single endpoint + batch-only says %s"+ % (sorted(batch_side), sorted(single_side)))++[email protected]("key", sorted(BATCH_ITEM_VALUES))+def test_each_batch_item_control_reaches_every_request(monkeypatch, key):+ client, fake = _batch_client(monkeypatch)+ r = client.post("/v1/systemone/batch", json={**BATCH_REQ, key: BATCH_ITEM_VALUES[key]})+ assert r.status_code == 200, r.text+ requests, _ = fake.batch_calls[0]+ assert len(requests) == len(BATCH_REQ["states"])+ for item in requests:+ assert item.get(key) == BATCH_ITEM_VALUES[key], (key, item)++[email protected]("key", sorted(BATCH_CALL_VALUES))+def test_each_batch_call_control_reaches_predict_batch_kwargs(monkeypatch, key):+ client, fake = _batch_client(monkeypatch)+ r = client.post("/v1/systemone/batch", json={**BATCH_REQ, key: BATCH_CALL_VALUES[key]})+ assert r.status_code == 200, r.text+ _, kwargs = fake.batch_calls[0]+ assert kwargs.get(key) == BATCH_CALL_VALUES[key], (key, kwargs)+++def test_batch_unset_controls_are_not_sent_as_none(monkeypatch):+ """An absent control must stay absent in either the item dict or the call kwargs.++ Core reads an absent argument as "inherit what the Router was built with", so sending+ ``min_confidence=None`` would override a deployment's ``Router(min_confidence=...)``, and+ sending a per-item key with value ``None`` would break a Router whose ``predict_batch``+ predates the key (#294 shape).+ """+ client, fake = _batch_client(monkeypatch)+ assert client.post("/v1/systemone/batch", json=BATCH_REQ).status_code == 200+ requests, kwargs = fake.batch_calls[0]+ assert kwargs == {}+ for item in requests:+ for key in BATCH_BODY_ITEM_CONTROLS:+ assert key not in item, (key, item)++[email protected]("key", sorted(set(BATCH_ITEM_VALUES) | set(BATCH_CALL_VALUES)))+def test_batch_explicit_null_is_no_control(monkeypatch, key):+ """``{"lang_guess": null}`` means "no hint" -- same shape the single endpoint honours.++ A Jev client that serialises its absent fields must keep working; only a value says the+ caller asked for something.+ """+ client, fake = _batch_client(monkeypatch)+ assert client.post("/v1/systemone/batch", json={**BATCH_REQ, key: None}).status_code == 200+ requests, kwargs = fake.batch_calls[0]+ assert "min_confidence" not in kwargs and "batch_size" not in kwargs+ assert "sort_by_length" not in kwargs+ for item in requests:+ assert key not in item+++def test_batch_min_confidence_zero_is_still_a_threshold(monkeypatch):+ """``0.0`` is falsy but is a value the caller chose; ``if min_confidence:`` would drop it."""+ client, fake = _batch_client(monkeypatch)+ assert client.post("/v1/systemone/batch",+ json={**BATCH_REQ, "min_confidence": 0.0}).status_code == 200+ _, kwargs = fake.batch_calls[0]+ assert kwargs["min_confidence"] == 0.0+++def test_batch_sort_by_length_false_is_not_forwarded(monkeypatch):+ """``predict_batch``'s own default is ``False``, so ``False`` is what the caller did NOT ask.++ Forwarding ``False`` explicitly would still work on ``Router`` but silently disappear on any+ attached agent whose ``predict_batch`` predates the knob (#294) -- which is why the MCP tool+ makes the same choice. The endpoint does accept the value; it just does not translate it into+ a kwarg.+ """+ client, fake = _batch_client(monkeypatch)+ assert client.post("/v1/systemone/batch",+ json={**BATCH_REQ, "sort_by_length": False}).status_code == 200+ _, kwargs = fake.batch_calls[0]+ assert "sort_by_length" not in kwargs+++def test_batch_strict_predict_batch_router_answers_a_quiet_body(monkeypatch):+ """An attached Router whose ``predict_batch(self, requests)`` takes nothing else must still work.++ The endpoint forwards kwargs only when the caller asks. If the handler always passed+ ``batch_size=None`` (or any call kwarg) unconditionally, this Router would raise ``TypeError``+ and the endpoint would 500 on a body that sent nothing -- which is exactly the mutation this+ witness kills.+ """+ monkeypatch.delenv("LAYA_API_KEY", raising=False)+ fake = StrictBatchRouter()+ client = TestClient(create_app(router=fake))+ r = client.post("/v1/systemone/batch", json=BATCH_REQ)+ assert r.status_code == 200, r.text+ assert len(fake.batch_calls) == 1+ _, kwargs = fake.batch_calls[0]+ assert kwargs == {}++[email protected]("key,value", [+ ("lang", True),+ ("lang", 5),+ ("lang_guess", ["de"]),+ ("lang_guess", {"code": "de"}),+ ("max_len", 0),+ ("max_len", -1),+ ("max_len", True),+ ("max_len", "64"),+ ("head_max_len", "32"),+ ("min_confidence", 1.5),+ ("min_confidence", -0.1),+ ("min_confidence", "0.9"),+ ("batch_size", 0),+ ("batch_size", -3),+ ("batch_size", True),+ ("batch_size", 1.5),+ ("batch_size", "4"),+ ("sort_by_length", "yes"),+ ("sort_by_length", 1),+])+def test_bad_control_on_batch_is_refused_before_inference(monkeypatch, key, value):+ client, fake = _batch_client(monkeypatch)+ r = client.post("/v1/systemone/batch", json={**BATCH_REQ, key: value})+ assert r.status_code == 422, (key, value, r.text)+ assert not fake.batch_calls, "a refused body must not reach predict_batch"++[email protected]("key", BODY_REFUSALS)+def test_batch_hook_control_is_refused_not_dropped(monkeypatch, key):+ """The batch endpoint must honour the same refusal policy the single endpoint already states.++ Without ``_refuse_body_refusals``, a batch body would silently hand a caller a shorter+ deadline for the operator's hooks -- ``predict_batch`` *does* take ``hooks_timeout`` as a+ call arg, so this is not a theoretical refusal here. The other four are refused for the same+ reason they are on ``/v1/systemone``: they run inside the server process.+ """+ value = {"hooks": [], "on_predict_start": "cache", "on_predict_end": "audit",+ "hooks_raise": False, "hooks_timeout": 5.0}[key]+ client, fake = _batch_client(monkeypatch)+ r = client.post("/v1/systemone/batch", json={**BATCH_REQ, key: value})+ assert r.status_code == 422, r.text+ detail = r.json()["detail"]+ assert key in detail+ assert "cannot be sent to this endpoint" in detail+ assert not fake.batch_calls, "the refusal must come before inference"+++def test_batch_predict_fallback_forwards_every_control(monkeypatch):+ """When the attached Router has no ``predict_batch``, every control must reach ``predict()``.++ The batch path synthesizes per-state ``predict()`` calls with the same kwargs the single+ endpoint uses -- ``max_len``/``head_max_len``/``task``/``lang``/``lang_guess``/``min_confidence``+ all apply per call. A caller that sends ``min_confidence`` on a batch body must see the+ abstention gate apply per item on the fallback path, not silently skip it because+ ``predict_batch`` was unavailable.+ """+ monkeypatch.delenv("LAYA_API_KEY", raising=False)+ fake = BudgetRouter()+ client = TestClient(create_app(router=fake))+ body = {**BATCH_REQ, "max_len": 64, "head_max_len": 32, "task": "typed-decisions",+ "lang": "de", "lang_guess": "de", "min_confidence": 0.5}+ r = client.post("/v1/systemone/batch", json=body)+ assert r.status_code == 200, r.text+ assert len(fake.calls) == len(BATCH_REQ["states"])+ for call in fake.calls:+ assert call["max_len"] == 64+ assert call["head_max_len"] == 32+ assert call["task"] == "typed-decisions"+ assert call["lang"] == "de"+ assert call["lang_guess"] == "de"+ assert call["min_confidence"] == 0.5+++def test_batch_predict_fallback_sends_no_kwargs_when_body_is_quiet(monkeypatch):+ """A quiet batch body on the fallback path must call ``predict()`` with no kwargs at all.++ An attached Router whose ``predict()`` does not take the new keywords -- the ``FakeRouter``+ class this suite has always used -- must keep answering on a bare batch. Passing ``None``+ would 500 on that shape and would override a deployment's own ``Router(lang_guess=...)``+ on a modern one.+ """+ monkeypatch.delenv("LAYA_API_KEY", raising=False)+ fake = FakeRouter()+ client = TestClient(create_app(router=fake))+ r = client.post("/v1/systemone/batch", json=BATCH_REQ)+ assert r.status_code == 200, r.text+ assert len(fake.calls) == len(BATCH_REQ["states"])+++def test_batch_controls_combine_on_one_call(monkeypatch):+ """Call kwargs and item overrides land on the same call, not in isolation.++ A caller that sends a token budget and a chunking cap together must see them on the same+ ``predict_batch`` invocation -- not the budget on the item and the chunk cap dropped, and+ not the reverse. This is the whole-point-of-the-PR shape.+ """+ client, fake = _batch_client(monkeypatch)+ r = client.post("/v1/systemone/batch",+ json={**BATCH_REQ, "batch_size": 2, "sort_by_length": True,+ "min_confidence": 0.8, "task": "typed-decisions", "max_len": 128})+ assert r.status_code == 200, r.text+ requests, kwargs = fake.batch_calls[0]+ assert kwargs == {"min_confidence": 0.8, "batch_size": 2, "sort_by_length": True}+ for item in requests:+ assert item["task"] == "typed-decisions"+ assert item["max_len"] == 128