From 097607150088ccda4da9289c6e904c678d357d80 Mon Sep 17 00:00:00 2001 From: WangRui-OpenAI <275922481+WangRui-OpenAI@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:30:25 +0000 Subject: [PATCH] fix(client): accept explicit null in nested structured output schemas Co-authored-by: Rogier Slag <2778689+rogierslag@users.noreply.github.com> --- .../com/openai/core/JsonSchemaValidator.kt | 6 ++++++ .../com/openai/core/StructuredOutputsTest.kt | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaValidator.kt b/openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaValidator.kt index 1e3b4f717..09609b472 100644 --- a/openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaValidator.kt +++ b/openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaValidator.kt @@ -390,6 +390,12 @@ internal class JsonSchemaValidator private constructor() { return } + // Optional enums can generate an anyOf with an explicit null alternative. + if (typeName == TYPE_NULL && depth != ROOT_DEPTH) { + validateSimpleSchema(schema, typeName, path, depth) + return + } + when (typeName) { TYPE_ARRAY -> validateArraySchema(schema, path, depth) TYPE_OBJECT -> validateObjectSchema(schema, path, depth) diff --git a/openai-java-core/src/test/kotlin/com/openai/core/StructuredOutputsTest.kt b/openai-java-core/src/test/kotlin/com/openai/core/StructuredOutputsTest.kt index f71f9c75b..14c778145 100644 --- a/openai-java-core/src/test/kotlin/com/openai/core/StructuredOutputsTest.kt +++ b/openai-java-core/src/test/kotlin/com/openai/core/StructuredOutputsTest.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.ObjectNode import com.openai.errors.OpenAIInvalidDataException +import com.openai.models.responses.ResponseTextConfig import io.swagger.v3.oas.annotations.media.ArraySchema import io.swagger.v3.oas.annotations.media.Schema import java.util.Optional @@ -256,6 +257,17 @@ internal class StructuredOutputsTest { assertThat(validator.isValid()).isTrue } + @Test + fun schemaTest_optionalEnumIsAcceptedByLocalValidation() { + @Suppress("unused") class X(val value: Optional) + + schema = extractSchema(X::class.java) + + assertThatNoException().isThrownBy { + ResponseTextConfig.builder().format(X::class.java).build() + } + } + @Test fun schemaTest_tinySchemaFromOptionalBoolean() { @Suppress("unused") class X(val b: Optional) @@ -754,6 +766,14 @@ internal class StructuredOutputsTest { .startsWith("#: 'properties' field is missing, empty or not an object.") } + @Test + fun schemaTest_nullTypeInRootSchema() { + schema = parseJson("""{"$SCHEMA":"$SCHEMA_VER","type":"null"}""") + validator.validate(schema) + + assertThat(validator.errors()).containsExactly("#/type: Unsupported 'type' value: 'null'.") + } + @Test fun schemaTest_anyOfInRootSchema() { // OpenAI does not allow `"anyOf"` to appear at the root level of a schema.