From bc5767209252c3a39eda13412c252cfd76aea9bf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 23 Jul 2026 10:43:35 +0000 Subject: [PATCH] Add type:string to all-string enum schemas in serializeSchema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When all enum values are strings, include "type": "string" alongside the "enum" array in the serialized JSON Schema. This gives LLMs a stronger type constraint and follows JSON Schema best-practice (RFC 3986 ยง6.1.2). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- skills/rig/rig.ts | 4 +++- src/rig.test.ts | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/skills/rig/rig.ts b/skills/rig/rig.ts index 660979e..f301cd4 100644 --- a/skills/rig/rig.ts +++ b/skills/rig/rig.ts @@ -272,7 +272,9 @@ function serializeSchema(schema: Schema): JsonSchemaObject { const withDescription = (obj: JsonSchemaObject): JsonSchemaObject => description === undefined ? obj : { ...obj, description }; if ("enum" in schema) { - return withDescription({ enum: schema.enum }); + const enumValues = schema.enum as readonly unknown[]; + const allStrings = enumValues.length > 0 && enumValues.every((v) => typeof v === "string"); + return withDescription(allStrings ? { type: "string", enum: schema.enum } : { enum: schema.enum }); } if ("items" in schema) { return withDescription({ type: "array", items: serializeSchema(schema.items) }); diff --git a/src/rig.test.ts b/src/rig.test.ts index f147cce..538eb20 100644 --- a/src/rig.test.ts +++ b/src/rig.test.ts @@ -1002,8 +1002,15 @@ describe("toJsonSchema", () => { }); it("converts enum schemas", () => { - expect(toJsonSchema(s.enum("a", "b", "c"))).toEqual({ enum: ["a", "b", "c"] }); - expect(toJsonSchema(s.enum(["x", "y"], "A choice"))).toEqual({ enum: ["x", "y"], description: "A choice" }); + expect(toJsonSchema(s.enum("a", "b", "c"))).toEqual({ type: "string", enum: ["a", "b", "c"] }); + expect(toJsonSchema(s.enum(["x", "y"], "A choice"))).toEqual({ type: "string", enum: ["x", "y"], description: "A choice" }); + }); + + it("adds type:string to all-string enums but not mixed enums", () => { + expect(toJsonSchema(s.enum("low", "medium", "high"))).toEqual({ type: "string", enum: ["low", "medium", "high"] }); + expect(toJsonSchema(s.enum(1, 2, 3))).toEqual({ enum: [1, 2, 3] }); + expect(toJsonSchema(s.enum("yes", 1))).toEqual({ enum: ["yes", 1] }); + expect(toJsonSchema(s.enum())).toEqual({ enum: [] }); }); it("converts array schemas", () => {