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): accept pydantic v2 Optional[...] (anyOf/oneOf nullable) in structured schemas

Raj Ojha4942728a71ac
diff --git a/laya-ts/src/structured.ts b/laya-ts/src/structured.ts
index 91761682..c5326e04 100644
--- a/laya-ts/src/structured.ts
+++ b/laya-ts/src/structured.ts
@@ -163,6 +163,24 @@ function fieldFor(path: string, name: string, prop: unknown): PlannedField {
}
const p = prop as Record<string, unknown>;
const description = p.description;
+ if (!("const" in p || "enum" in p || "type" in p)) {
+ const union = (p.anyOf ?? p.oneOf) as unknown[] | undefined;
+ if (union !== undefined && Array.isArray(union)) {
+ const branches = union.filter(
+ (b) => b !== null && typeof b === "object" && !Array.isArray(b) && (b as Record<string, unknown>).type !== "null",
+ ) as Record<string, unknown>[];
+ if (branches.length !== 1) {
+ throw new SchemaError(
+ `${path}: only 'Optional[...]' unions (one non-null branch) are supported, got ${branches.length}`,
+ );
+ }
+ const branch: Record<string, unknown> = { ...branches[0] };
+ if (branch.description === undefined && description !== undefined) {
+ branch.description = description;
+ }
+ return fieldFor(path, name, branch);
+ }
+ }
if ("const" in p) return enumField(path, name, [p.const], description);
if ("enum" in p) {
if (!Array.isArray(p.enum)) throw new SchemaError(`${path}: 'enum' must be a list, got ${pyType(p.enum)}`);
diff --git a/laya-ts/tests/structured.test.ts b/laya-ts/tests/structured.test.ts
index beff0083..a20d0ba3 100644
--- a/laya-ts/tests/structured.test.ts
+++ b/laya-ts/tests/structured.test.ts
@@ -236,3 +236,53 @@ describe("structured/methods", () => {
});
});
+describe("structured/nullable-anyof", () => {
+ const 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" }] },
+ },
+ };
+
+ it("unwraps anyOf nullable enum as choice and preserves description", () => {
+ const nq = questionsFromJsonSchema(NULLABLE);
+ expect(nq.dept.type).toBe("choice");
+ expect(nq.dept.instructions).toBe("Which team?");
+ expect(Object.keys(nq.dept.criteria as object)).toEqual(["billing", "sales"]);
+ });
+
+ it("unwraps anyOf nullable bounded integer as score", () => {
+ const nq = questionsFromJsonSchema(NULLABLE);
+ expect(nq.score.type).toBe("score");
+ expect(nq.score.criteria).toEqual(["0", "1", "2"]);
+ });
+
+ it("unwraps anyOf nullable boolean as noul", () => {
+ const nq = questionsFromJsonSchema(NULLABLE);
+ expect(nq.flag.type).toBe("noul");
+ });
+
+ it("accepts oneOf nullable enum as choice", () => {
+ const q = questionsFromJsonSchema({
+ type: "object",
+ properties: { a: { oneOf: [{ enum: ["x", "y"] }, { type: "null" }] } },
+ });
+ expect(q.a.type).toBe("choice");
+ expect(Object.keys(q.a.criteria as object)).toEqual(["x", "y"]);
+ });
+
+ it("rejects unions of two real types as ambiguous", () => {
+ expect(() =>
+ questionsFromJsonSchema({
+ type: "object",
+ properties: { a: { anyOf: [{ type: "boolean" }, { type: "integer" }] } },
+ }),
+ ).toThrowError(/only 'Optional\[\.\.\.\]' unions \(one non-null branch\) are supported/);
+ });
+});
+