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(ts): reject a nested choice label, as Python does (#425 parity)

divit aggarwal8737f0029594
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts
index f5bc8e69..2a5846ed 100644
--- a/laya-ts/src/agent.ts
+++ b/laya-ts/src/agent.ts
@@ -157,6 +157,16 @@ export function checkQuestion(qid: string, qdef: unknown): void {
if (Object.keys(crit as object).length === 0) {
throw new Error(`question ${qidStr(qid)}: a choice question needs at least one criterion`);
}
+ if (Array.isArray(crit)) {
+ crit.forEach((label: unknown, i) => {
+ if (typeof label !== "object" || label === null) return;
+ throw new Error(
+ `question ${qidStr(qid)}: choice label ${i} is a ${Array.isArray(label) ? "list" : "dict"}; a label is ` +
+ `rendered as option text and used as the answer key, so it must be a scalar (a string, number or ` +
+ `null), got ${JSON.stringify(label)}`,
+ );
+ });
+ }
} else if (t === "score") {
if (!Array.isArray(crit)) {
throw new Error(
diff --git a/laya-ts/tests/audit-regressions.test.ts b/laya-ts/tests/audit-regressions.test.ts
index 041cf7cb..150dfc8f 100644
--- a/laya-ts/tests/audit-regressions.test.ts
+++ b/laya-ts/tests/audit-regressions.test.ts
@@ -58,6 +58,15 @@ describe("audit regressions", () => {
expect(() => checkQuestion("q", { type: "score", instructions: "?", criteria: ["low", "mid", undefined] })).toThrow("level 2");
});
+ it("rejects a nested choice label, as Python does (#425)", () => {
+ const check = (criteria: unknown[]) => () => checkQuestion("q", { type: "choice", instructions: "?", criteria });
+ expect(check(["yes", { k: 1 }])).toThrow("choice label 1 is a dict");
+ expect(check([["y", ["z"]], "no"])).toThrow("choice label 0 is a list");
+ expect(check(["a", "b", [1]])).toThrow('question "q": choice label 2 is a list');
+ expect(check(["a", 1, true, null, 2.5])).not.toThrow();
+ expect(() => checkQuestion("q", { type: "choice", instructions: "?", criteria: { a: "x", b: { d: 1 } } })).not.toThrow();
+ });
+
it("rejects malformed provider output", async () => {
const agent = new Agent({ provider: {
async runEncoder(_batch: any) { return { lastHidden: [[[0, 0]]] }; },