Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(structured): preserve singleton boolean constraints

Bortlesboat3dc8658cd360
diff --git a/laya/structured.py b/laya/structured.py
index 780e91f6..cae96326 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -103,7 +103,7 @@ def _enum_field(path: str, name: str, values: Sequence[Any], description: Option
raise SchemaError("%s: %d options exceeds MAX_OPTIONS=%d" % (path, len(values), MAX_OPTIONS))
if not values:
raise SchemaError("%s: 'enum' must not be empty" % path)
- if all(isinstance(v, bool) for v in values):
+ if all(isinstance(v, bool) for v in values) and len(set(values)) == 2:
return _noul_field(path, name, description)
options = [(("null" if v is None else str(v)), v) for v in values]
if len({label for label, _ in options}) != len(options):
diff --git a/tests/test_structured.py b/tests/test_structured.py
index 7b05453f..099bb23c 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -76,6 +76,26 @@ check("map/nullable boolean remains noul",
questions_from_json_schema({"type": "object", "properties": {"a": {"type": ["null", "boolean"]}}})["a"]["type"],
"noul")
+for value in (True, False):
+ for constraint in ({"enum": [value]}, {"const": value}):
+ schema = {"type": "object", "properties": {"flag": {"type": "boolean", **constraint}}}
+ question = questions_from_json_schema(schema)["flag"]
+ case = "boolean singleton %r" % constraint
+ check("map/%s is a choice" % case, question["type"], "choice")
+ check("map/%s has only the allowed option" % case,
+ question.get("criteria"), {str(value): None})
+ projected = answers_to_json({"flag": {"type": "choice", "choice": str(value)}}, schema)
+ check_true("project/%s preserves its boolean value" % case, projected["flag"] is value)
+
+for options in ([True, False], [False, True]):
+ schema = {"type": "object", "properties": {"flag": {"enum": options}}}
+ check("map/two boolean options %r remain noul" % options,
+ questions_from_json_schema(schema)["flag"]["type"], "noul")
+ for probability, value in ((0.2, False), (0.8, True)):
+ projected = answers_to_json({"flag": {"type": "noul", "noul": probability}}, schema)
+ check_true("project/boolean options %r with noul %s" % (options, probability),
+ projected["flag"] is value)
+
# --------------------------------------------------------------- projection
ANSWERS = {
@@ -542,6 +562,22 @@ try:
})
check("pydantic/instance", (ticket.department, ticket.urgency, ticket.needs_human),
("support", 1, True))
+
+ class BooleanLiterals(pydantic.BaseModel):
+ enabled: Literal[True]
+ disabled: Literal[False]
+
+ literal_questions = questions_from_pydantic(BooleanLiterals)
+ check("pydantic/true literal has one option", literal_questions["enabled"].get("criteria"),
+ {"True": None})
+ check("pydantic/false literal has one option", literal_questions["disabled"].get("criteria"),
+ {"False": None})
+ literals = answer_to_pydantic(BooleanLiterals, {
+ "enabled": {"type": "choice", "choice": "True"},
+ "disabled": {"type": "choice", "choice": "False"},
+ })
+ check_true("pydantic/boolean literals preserve their values",
+ literals.enabled is True and literals.disabled is False)
except ImportError:
PASS.append("pydantic/skipped (not installed)")