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
fix: structured criteria crashed noul and leaked Python reprs (v0.2.1)
Nandakishor5b517bff940a
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 65fdbefa..cc08b2b3 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -42,7 +42,9 @@ jobs:run: python -c "import laya; print(laya.__version__); print(sorted(laya.__all__))"- name: Routing and language-detection tests- run: python tests/test_router.py+ run: |+ python tests/test_router.py+ python tests/test_criteria.pylint:name: lintdiff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 01cbb868..28e1b324 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -36,7 +36,9 @@ jobs:pip install -e .- name: Tests must pass before anything is published- run: python tests/test_router.py+ run: |+ python tests/test_router.py+ python tests/test_criteria.py- name: Buildrun: python -m builddiff --git a/laya/__init__.py b/laya/__init__.pyindex 7de17e09..e94d1710 100644--- a/laya/__init__.py+++ b/laya/__init__.py@@ -22,7 +22,7 @@ from .presets import ()from .router import DEFAULT_MODELS, RouteDecision, Router-__version__ = "0.2.0"+__version__ = "0.2.1"__all__ = ["Agent","RLAgent",diff --git a/laya/common.py b/laya/common.pyindex 6148c056..326b76a4 100644--- a/laya/common.py+++ b/laya/common.py@@ -18,17 +18,31 @@ def serialize_state(state: Union[str, dict, list]) -> str:return json.dumps(state, ensure_ascii=False)+def render_criterion(value) -> str:+ """Render one criterion value as text.++ Strings pass through; anything structured (dict, list, number) becomes compact JSON, so a+ rubric reads as JSON rather than a Python repr. Without this a dict-valued criterion+ crashed `noul` outright and leaked `{'desc': ...}` into `choice` and `score` prompts.+ """+ if isinstance(value, str):+ return value+ return json.dumps(value, ensure_ascii=False, separators=(", ", ": "), default=str)++def render_options(q: Dict) -> List[str]:"""Render option texts in label-index order. Noul is always [false, true]."""t, crit = q["t"], q.get("crit")if t == "choice":- return [k if not v else "%s: %s" % (k, v) for k, v in crit.items()]+ # only None/"" mean "no description"; 0 and False are legitimate criterion values+ return [k if v is None or v == "" else "%s: %s" % (k, render_criterion(v)) for k, v in crit.items()]if t == "score":- return ["level %d: %s" % (i, c) for i, c in enumerate(crit)]+ return ["level %d: %s" % (i, render_criterion(c)) for i, c in enumerate(crit)]crit = crit or {}+ false_crit, true_crit = crit.get("false"), crit.get("true")return [- "false: " + (crit.get("false") or "no, the statement does not hold"),- "true: " + (crit.get("true") or "yes, the statement holds"),+ "false: " + (render_criterion(false_crit) if false_crit not in (None, "") else "no, the statement does not hold"),+ "true: " + (render_criterion(true_crit) if true_crit not in (None, "") else "yes, the statement holds"),]diff --git a/pyproject.toml b/pyproject.tomlindex c16978f1..51c57737 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "laya"-version = "0.2.0"+version = "0.2.1"description = "Fast, non-autoregressive System 1 decision engine with calibrated probabilities"readme = "README.md"requires-python = ">=3.8"diff --git a/setup.py b/setup.pyindex 5527f4dd..80dc2b6e 100644--- a/setup.py+++ b/setup.py@@ -2,7 +2,7 @@ from setuptools import setup, find_packagessetup(name="laya",- version="0.2.0",+ version="0.2.1",description="Fast, non-autoregressive System 1 decision engine with calibrated probabilities",long_description=open("README.md").read(),long_description_content_type="text/markdown",diff --git a/tests/test_criteria.py b/tests/test_criteria.pynew file mode 100644index 00000000..76a8d6fa--- /dev/null+++ b/tests/test_criteria.py@@ -0,0 +1,105 @@+"""Criteria rendering: structured values must not crash or leak Python reprs.++Regression tests for the bug reported in PR #2 (trocker): a `noul` question whose criteria+values were dicts raised `TypeError: can only concatenate str (not "dict") to str`, and+`choice`/`score` stringified dicts as Python reprs instead of JSON.+"""+import json+import os+import sys++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from laya.common import render_criterion, render_options # noqa: E402++PASS, FAIL = [], []+++def check(name, got, want):+ if got == want:+ PASS.append(name)+ else:+ FAIL.append("%s:\n got %r\n want %r" % (name, got, want))+++def check_true(name, cond, detail=""):+ if cond:+ PASS.append(name)+ else:+ FAIL.append("%s %s" % (name, detail))+++# --------------------------------------------------------------- render_criterion+check("criterion/str passes through", render_criterion("phishing or scam"), "phishing or scam")+check("criterion/dict -> json", render_criterion({"desc": "phishing"}), '{"desc": "phishing"}')+check("criterion/list -> json", render_criterion(["a", "b"]), '["a", "b"]')+check("criterion/int -> json", render_criterion(3), "3")+check("criterion/bool -> json", render_criterion(False), "false")+check("criterion/non-ascii kept", render_criterion({"d": "münchen"}), '{"d": "münchen"}')+check_true("criterion/unserialisable falls back to str",+ isinstance(render_criterion({"o": object()}), str))+++# --------------------------------------------------------------- the reported crash+q = {"t": "noul", "ins": "Is this phishing?",+ "crit": {"true": {"desc": "phishing, scam or fraud"}, "false": {"desc": "legitimate"}}}+try:+ out = render_options(q)+ check("noul/dict criteria does not crash", len(out), 2)+ check_true("noul/false renders as json", out[0] == 'false: {"desc": "legitimate"}', out[0])+ check_true("noul/true renders as json", out[1] == 'true: {"desc": "phishing, scam or fraud"}', out[1])+ check_true("noul/no python repr leaked", "'" not in "".join(out), out)+except TypeError as e:+ FAIL.append("noul/dict criteria CRASHED: %s" % e)+++# --------------------------------------------------------------- choice and score+out = render_options({"t": "choice", "ins": "x",+ "crit": {"billing": {"desc": "payments"}, "tech": None, "sales": ""}})+check("choice/dict -> json", out[0], 'billing: {"desc": "payments"}')+check("choice/None -> bare key", out[1], "tech")+check("choice/empty string -> bare key", out[2], "sales")+check_true("choice/no python repr", "{'" not in "".join(out), out)++# 0 and False are real criterion values, not "missing"+out = render_options({"t": "choice", "ins": "x", "crit": {"zero": 0, "no": False}})+check("choice/0 is kept", out[0], "zero: 0")+check("choice/False is kept", out[1], "no: false")++out = render_options({"t": "score", "ins": "x", "crit": [{"d": "low"}, "high", 2]})+check("score/dict level -> json", out[0], 'level 0: {"d": "low"}')+check("score/str level unchanged", out[1], "level 1: high")+check("score/int level -> json", out[2], "level 2: 2")+++# --------------------------------------------------------------- unchanged behaviour+check("noul/default false text", render_options({"t": "noul", "ins": "x", "crit": None})[0],+ "false: no, the statement does not hold")+check("noul/default true text", render_options({"t": "noul", "ins": "x", "crit": None})[1],+ "true: yes, the statement holds")+check("noul/string criteria still work",+ render_options({"t": "noul", "ins": "x", "crit": {"true": "yes it is", "false": "no"}}),+ ["false: no", "true: yes it is"])+check("choice/string criteria still work",+ render_options({"t": "choice", "ins": "x", "crit": {"a": "first", "b": None}}),+ ["a: first", "b"])+check("score/string criteria still work",+ render_options({"t": "score", "ins": "x", "crit": ["low", "high"]}),+ ["level 0: low", "level 1: high"])++# every rendered option must be a str, whatever went in+for qq in [{"t": "choice", "ins": "x", "crit": {"a": {"n": 1}, "b": [1, 2], "c": 3.5}},+ {"t": "score", "ins": "x", "crit": [{"a": 1}, [2], None]},+ {"t": "noul", "ins": "x", "crit": {"true": [1], "false": {"z": 0}}}]:+ check_true("all options are str (%s)" % qq["t"],+ all(isinstance(o, str) for o in render_options(qq)))++# the JSON we emit is parseable back+parsed = json.loads(render_options(+ {"t": "noul", "ins": "x", "crit": {"true": {"a": 1}, "false": {"b": 2}}})[1].split("true: ", 1)[1])+check("emitted json round-trips", parsed, {"a": 1})++print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))+for f in FAIL:+ print(" FAIL " + f)+sys.exit(1 if FAIL else 0)