From 57d9b917eb58134494fd9d9ffd66aad086db7872 Mon Sep 17 00:00:00 2001 From: HaKIM Date: Thu, 23 Oct 2025 01:18:44 +0200 Subject: [PATCH] Feature/treat nullable fields as required --- .../tests/StructuredOutput/ResponseFormatFactoryTest.php | 2 +- src/platform/CHANGELOG.md | 1 + src/platform/src/Contract/JsonSchema/Factory.php | 4 +++- src/platform/tests/Contract/JsonSchema/FactoryTest.php | 6 +++--- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/agent/tests/StructuredOutput/ResponseFormatFactoryTest.php b/src/agent/tests/StructuredOutput/ResponseFormatFactoryTest.php index 86e97fcfb..ff1a9202b 100644 --- a/src/agent/tests/StructuredOutput/ResponseFormatFactoryTest.php +++ b/src/agent/tests/StructuredOutput/ResponseFormatFactoryTest.php @@ -38,7 +38,7 @@ public function testCreate() 'isActive' => ['type' => 'boolean'], 'age' => ['type' => ['integer', 'null']], ], - 'required' => ['id', 'name', 'createdAt', 'isActive'], + 'required' => ['id', 'name', 'createdAt', 'isActive', 'age'], 'additionalProperties' => false, ], 'strict' => true, diff --git a/src/platform/CHANGELOG.md b/src/platform/CHANGELOG.md index fc194a602..09b45f933 100644 --- a/src/platform/CHANGELOG.md +++ b/src/platform/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 0.1 --- + * Add nullables as required in structured outputs * Add support for Albert API for French/EU data sovereignty * Add unified abstraction layer for interacting with various AI models and providers * Add support for 16+ AI providers: diff --git a/src/platform/src/Contract/JsonSchema/Factory.php b/src/platform/src/Contract/JsonSchema/Factory.php index c0aa7005f..f8cb05840 100644 --- a/src/platform/src/Contract/JsonSchema/Factory.php +++ b/src/platform/src/Contract/JsonSchema/Factory.php @@ -117,7 +117,9 @@ private function convertTypes(array $elements): ?array if (!isset($schema['anyOf'])) { $schema['type'] = [$schema['type'], 'null']; } - } elseif (!($element instanceof \ReflectionParameter && $element->isOptional())) { + } + + if (!($element instanceof \ReflectionParameter && $element->isOptional())) { $result['required'][] = $name; } diff --git a/src/platform/tests/Contract/JsonSchema/FactoryTest.php b/src/platform/tests/Contract/JsonSchema/FactoryTest.php index 7fa787b37..f76f1bba9 100644 --- a/src/platform/tests/Contract/JsonSchema/FactoryTest.php +++ b/src/platform/tests/Contract/JsonSchema/FactoryTest.php @@ -183,7 +183,7 @@ public function testBuildPropertiesForUserClass() 'isActive' => ['type' => 'boolean'], 'age' => ['type' => ['integer', 'null']], ], - 'required' => ['id', 'name', 'createdAt', 'isActive'], + 'required' => ['id', 'name', 'createdAt', 'isActive', 'age'], 'additionalProperties' => false, ]; @@ -304,7 +304,7 @@ public function testBuildPropertiesForUnionTypeDto() ], ], ], - 'required' => [], + 'required' => ['time'], 'additionalProperties' => false, ]; @@ -347,7 +347,7 @@ public function testBuildPropertiesForExampleDto() 'enum' => ['Foo', 'Bar', null], ], ], - 'required' => ['name', 'taxRate'], + 'required' => ['name', 'taxRate', 'category'], 'additionalProperties' => false, ];