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): accept pydantic v2 Optional[...] (anyOf/oneOf nullable)
AlKor13116e8aeb39bd
diff --git a/laya/structured.py b/laya/structured.pyindex 5bb226cf..a3bcabea 100644--- a/laya/structured.py+++ b/laya/structured.py@@ -122,6 +122,22 @@ def _field(path: str, name: str, prop: Dict[str, Any]) -> _Field:if not isinstance(prop, dict):raise SchemaError("%s: property must be an object, got %s" % (path, type(prop).__name__))description = prop.get("description")+ # Pydantic v2 renders `Optional[X]` as `{"anyOf": [<X>, {"type": "null"}]}` with no+ # top-level type/enum/const, the same nullable shape the list form `type: ["string", "null"]`+ # already handles below. Unwrap the single non-null branch (carrying the outer description)+ # so `Optional[Literal[...]]`, `Optional[int]` and friends map instead of raising. A union of+ # two real types is genuinely ambiguous and still rejected.+ if not ({"const", "enum", "type"} & set(prop)):+ union = prop.get("anyOf") or prop.get("oneOf")+ if union is not None:+ branches = [b for b in union if isinstance(b, dict) and b.get("type") != "null"]+ if len(branches) != 1:+ raise SchemaError(+ "%s: only 'Optional[...]' unions (one non-null branch) are supported, got %d"+ % (path, len(branches)))+ branch = dict(branches[0])+ branch.setdefault("description", description)+ return _field(path, name, branch)if "const" in prop:return _enum_field(path, name, [prop["const"]], description)if "enum" in prop:diff --git a/tests/test_structured.py b/tests/test_structured.pyindex 5ab3ac8e..67f20e66 100644--- a/tests/test_structured.py+++ b/tests/test_structured.py@@ -194,6 +194,34 @@ from laya.onnx_agent import ONNXAgent # noqa: E402check("method/ONNXAgent.decide", callable(getattr(ONNXAgent, "decide", None)), True)+# --------------------------------------------------------------- nullable via anyOf (pydantic v2)+# Pydantic v2 renders Optional[X] as {"anyOf": [<X>, {"type": "null"}]}, the same nullable intent+# as the list form type:["string","null"]. Both must map to the underlying field, not raise.+NULLABLE = {+ "type": "object",+ "properties": {+ "dept": {"anyOf": [{"type": "string", "enum": ["billing", "sales"]}, {"type": "null"}],+ "description": "Which team?"},+ "score": {"anyOf": [{"type": "integer", "minimum": 0, "maximum": 2}, {"type": "null"}]},+ "flag": {"anyOf": [{"type": "boolean"}, {"type": "null"}]},+ },+}+nq = questions_from_json_schema(NULLABLE)+check("nullable/anyOf enum is a choice", nq["dept"]["type"], "choice")+check("nullable/anyOf carries the outer description", nq["dept"]["instructions"], "Which team?")+check("nullable/anyOf bounded integer is a score", nq["score"]["type"], "score")+check("nullable/anyOf boolean is a noul", nq["flag"]["type"], "noul")+# oneOf is accepted the same way+check("nullable/oneOf enum is a choice",+ questions_from_json_schema(+ {"type": "object", "properties": {"a": {"oneOf": [{"enum": ["x", "y"]}, {"type": "null"}]}}}+ )["a"]["type"], "choice")+# a union of two real types stays ambiguous and is rejected+check_raises("nullable/two real branches rejected", SchemaError,+ _bad({"type": "object",+ "properties": {"a": {"anyOf": [{"type": "boolean"}, {"type": "integer"}]}}}))++print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))for f in FAIL:print(" FAIL", f)