Skip to content

Commit 72c3fb4

Browse files
Emit a single null branch for optional structured-output properties that already accept null (#10522)
Co-authored-by: Effect Bot <[email protected]>
1 parent f527f90 commit 72c3fb4

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Avoid duplicate null branches for optional properties in `OpenAiStructuredOutput.toCodecOpenAI` and `AnthropicStructuredOutput.toCodecAnthropic`.

‎packages/effect/src/ai/internal/structured-output.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ function compilerAnnotations(
312312

313313
function optionalToNullable(type: SchemaAST.AST): SchemaAST.AST {
314314
return SchemaAST.decodeTo(
315-
new SchemaAST.Union([type, SchemaAST.null]),
315+
isNullable(type) ? SchemaAST.replaceContext(type, undefined) : new SchemaAST.Union([type, SchemaAST.null]),
316316
SchemaAST.optionalKey(SchemaAST.toType(type)),
317317
SchemaTransformation.transformOptional({
318318
decode: Option.filter(Predicate.isNotNull),
@@ -321,6 +321,11 @@ function optionalToNullable(type: SchemaAST.AST): SchemaAST.AST {
321321
)
322322
}
323323

324+
function isNullable(ast: SchemaAST.AST): boolean {
325+
if (ast.checks !== undefined || ast.encoding !== undefined) return false
326+
return SchemaAST.isNull(ast) || (SchemaAST.isUnion(ast) && ast.types.some(isNullable))
327+
}
328+
324329
function prepareChecks(checks: SchemaAST.Checks | undefined): SchemaAST.Checks | undefined {
325330
if (checks === undefined) return undefined
326331
const out = SchemaAST.mapOrSame(checks, prepareCheck)

‎packages/effect/test/ai/AnthropicStructuredOutput.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,39 @@ describe("toCodecAnthropic", () => {
489489
await decoding.succeed({ "a": "a", "b": 1 })
490490
await decoding.succeed({ "a": "a", "b": null }, { a: "a" })
491491
})
492+
493+
it("optional properties that already accept null", async () => {
494+
const schema = Schema.Struct({
495+
a: Schema.optional(Schema.Finite).annotate({ description: "Optional number" }),
496+
b: Schema.optionalKey(Schema.NullOr(Schema.Finite))
497+
})
498+
assertJsonSchema(schema, {
499+
type: "object",
500+
properties: {
501+
a: { anyOf: [{ type: "number" }, { type: "null" }], description: "Optional number" },
502+
b: { anyOf: [{ type: "number" }, { type: "null" }] }
503+
},
504+
required: ["a", "b"],
505+
additionalProperties: false
506+
})
507+
const asserts = new TestSchema.Asserts(toCodecAnthropic(schema).codec)
508+
await asserts.encoding().succeed({ a: 1, b: 2 })
509+
await asserts.encoding().succeed({}, { a: null, b: null })
510+
await asserts.decoding().succeed({ a: 1, b: 2 })
511+
await asserts.decoding().succeed({ a: null, b: null }, {})
512+
})
513+
514+
it("optional checked nullable properties preserve absent keys", async () => {
515+
const checked = Schema.NullOr(Schema.Record(Schema.String, Schema.Finite))
516+
.check(Schema.makeFilter((value) => value !== null, { expected: "non-null" }))
517+
const schema = Schema.Struct({
518+
a: Schema.optionalKey(checked),
519+
b: Schema.optionalKey(Schema.Union([Schema.String, checked]))
520+
})
521+
const asserts = new TestSchema.Asserts(toCodecAnthropic(schema).codec)
522+
await asserts.encoding().succeed({}, { a: null, b: null })
523+
await asserts.decoding().succeed({ a: null, b: null }, {})
524+
})
492525
})
493526

494527
it("Class", () => {

‎packages/effect/test/ai/OpenAiStructuredOutput.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,39 @@ describe("toCodecOpenAI", () => {
691691
await decoding.succeed({ "a": "a", "b": 1 })
692692
await decoding.succeed({ "a": "a", "b": null }, { a: "a" })
693693
})
694+
695+
it("optional properties that already accept null", async () => {
696+
const schema = Schema.Struct({
697+
a: Schema.optional(Schema.Finite).annotate({ description: "Optional number" }),
698+
b: Schema.optionalKey(Schema.NullOr(Schema.Finite))
699+
})
700+
assertJsonSchema(schema, {
701+
type: "object",
702+
properties: {
703+
a: { anyOf: [{ type: "number" }, { type: "null" }], description: "Optional number" },
704+
b: { anyOf: [{ type: "number" }, { type: "null" }] }
705+
},
706+
required: ["a", "b"],
707+
additionalProperties: false
708+
})
709+
const asserts = new TestSchema.Asserts(toCodecOpenAI(schema).codec)
710+
await asserts.encoding().succeed({ a: 1, b: 2 })
711+
await asserts.encoding().succeed({}, { a: null, b: null })
712+
await asserts.decoding().succeed({ a: 1, b: 2 })
713+
await asserts.decoding().succeed({ a: null, b: null }, {})
714+
})
715+
716+
it("optional checked nullable properties preserve absent keys", async () => {
717+
const checked = Schema.NullOr(Schema.Record(Schema.String, Schema.Finite))
718+
.check(Schema.makeFilter((value) => value !== null, { expected: "non-null" }))
719+
const schema = Schema.Struct({
720+
a: Schema.optionalKey(checked),
721+
b: Schema.optionalKey(Schema.Union([Schema.String, checked]))
722+
})
723+
const asserts = new TestSchema.Asserts(toCodecOpenAI(schema).codec)
724+
await asserts.encoding().succeed({}, { a: null, b: null })
725+
await asserts.decoding().succeed({ a: null, b: null }, {})
726+
})
694727
})
695728

696729
it("Class", () => {

0 commit comments

Comments
 (0)