Skip to content

Commit cb80d32

Browse files
committed
Preserve checked nullable structured-output properties
1 parent b01830a commit cb80d32

4 files changed

Lines changed: 54 additions & 87 deletions

File tree

‎.changeset/structured-output-optional-null.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"effect": patch
33
---
44

5-
Stop `OpenAiStructuredOutput.toCodecOpenAI` and `AnthropicStructuredOutput.toCodecAnthropic` from adding a second `null` branch to optional properties whose JSON form already accepts `null`, such as `Schema.optional(...)` and `Schema.optionalKey(Schema.NullOr(...))`.
5+
Avoid duplicate null branches for optional properties in `OpenAiStructuredOutput.toCodecOpenAI` and `AnthropicStructuredOutput.toCodecAnthropic`.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,8 @@ function optionalToNullable(type: SchemaAST.AST): SchemaAST.AST {
322322
}
323323

324324
function isNullable(ast: SchemaAST.AST): boolean {
325-
const encoded = SchemaAST.toEncoded(ast)
326-
if (encoded.checks !== undefined) return false
327-
return SchemaAST.isNull(encoded) || (SchemaAST.isUnion(encoded) && encoded.types.some(isNullable))
325+
if (ast.checks !== undefined || ast.encoding !== undefined) return false
326+
return SchemaAST.isNull(ast) || (SchemaAST.isUnion(ast) && ast.types.some(isNullable))
328327
}
329328

330329
function prepareChecks(checks: SchemaAST.Checks | undefined): SchemaAST.Checks | undefined {

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

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -492,40 +492,35 @@ describe("toCodecAnthropic", () => {
492492

493493
it("optional properties that already accept null", async () => {
494494
const schema = Schema.Struct({
495-
a: Schema.optional(Schema.Finite),
495+
a: Schema.optional(Schema.Finite).annotate({ description: "Optional number" }),
496496
b: Schema.optionalKey(Schema.NullOr(Schema.Finite))
497497
})
498-
assertJsonSchema(
499-
schema,
500-
{
501-
"type": "object",
502-
"properties": {
503-
"a": {
504-
"anyOf": [
505-
{ "type": "number" },
506-
{ "type": "null" }
507-
]
508-
},
509-
"b": {
510-
"anyOf": [
511-
{ "type": "number" },
512-
{ "type": "null" }
513-
]
514-
}
515-
},
516-
"required": ["a", "b"],
517-
"additionalProperties": false
518-
}
519-
)
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+
})
520507
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+
})
521513

522-
const encoding = asserts.encoding()
523-
await encoding.succeed({ a: 1, b: 2 })
524-
await encoding.succeed({}, { a: null, b: null })
525-
526-
const decoding = asserts.decoding()
527-
await decoding.succeed({ a: 1, b: 2 })
528-
await decoding.succeed({ a: null, b: null }, {})
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 }, {})
529524
})
530525
})
531526

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

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -694,62 +694,35 @@ describe("toCodecOpenAI", () => {
694694

695695
it("optional properties that already accept null", async () => {
696696
const schema = Schema.Struct({
697-
a: Schema.optional(Schema.Finite),
698-
b: Schema.optionalKey(Schema.NullOr(Schema.Finite)),
699-
c: Schema.optional(Schema.Record(Schema.String, Schema.Finite))
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
700708
})
701-
assertJsonSchema(
702-
schema,
703-
{
704-
"type": "object",
705-
"properties": {
706-
"a": {
707-
"anyOf": [
708-
{ "type": "number" },
709-
{ "type": "null" }
710-
]
711-
},
712-
"b": {
713-
"anyOf": [
714-
{ "type": "number" },
715-
{ "type": "null" }
716-
]
717-
},
718-
"c": {
719-
"anyOf": [
720-
{
721-
"type": "array",
722-
"description":
723-
"Object encoded as array of [key, value] pairs. Apply object constraints to the decoded object",
724-
"items": {
725-
"type": "object",
726-
"description":
727-
"Tuple encoded as an object with numeric string keys ('0', '1', ...). If present, '__rest__' contains remaining elements",
728-
"properties": {
729-
"0": { "type": "string" },
730-
"1": { "type": "number" }
731-
},
732-
"required": ["0", "1"],
733-
"additionalProperties": false
734-
}
735-
},
736-
{ "type": "null" }
737-
]
738-
}
739-
},
740-
"required": ["a", "b", "c"],
741-
"additionalProperties": false
742-
}
743-
)
744709
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+
})
745715

746-
const encoding = asserts.encoding()
747-
await encoding.succeed({ a: 1, b: 2, c: { "k": 3 } }, { a: 1, b: 2, c: [{ 0: "k", 1: 3 }] })
748-
await encoding.succeed({}, { a: null, b: null, c: null })
749-
750-
const decoding = asserts.decoding()
751-
await decoding.succeed({ a: 1, b: 2, c: [{ 0: "k", 1: 3 }] }, { a: 1, b: 2, c: { "k": 3 } })
752-
await decoding.succeed({ a: null, b: null, c: null }, {})
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 }, {})
753726
})
754727
})
755728

0 commit comments

Comments
 (0)