Skip to content

Commit bafc7b9

Browse files
committed
bug #805 Feature/treat nullable fields as required (HaKIMus)
This PR was squashed before being merged into the main branch. Discussion ---------- Feature/treat nullable fields as required | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #585 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Context: #585 (comment) Summary: Nullable fields must be present in the required list of their parent output structure, otherwise platforms (like openai) will reject the call. Proof of work: #585 (comment) `structured-output-union-types.php` output: ```php file:///~/ai/examples/openai/structured-output-union-types.php#L33 Symfony\AI\Fixtures\StructuredOutput\UnionType\UnionTypeDtofile:///~/ai/fixtures/StructuredOutput/UnionType/UnionTypeDto.php#L14 {#310 +time: Symfony\AI\Fixtures\StructuredOutput\UnionType\HumanReadableTimeUnionfile:///~/ai/fixtures/StructuredOutput/UnionType/HumanReadableTimeUnion.php#L14 {#316 +readableTime: "2023-10-07T19:32:10Z" } } ``` <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 57d9b91 Feature/treat nullable fields as required
2 parents 7a631c6 + 57d9b91 commit bafc7b9

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

src/agent/tests/StructuredOutput/ResponseFormatFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testCreate()
3838
'isActive' => ['type' => 'boolean'],
3939
'age' => ['type' => ['integer', 'null']],
4040
],
41-
'required' => ['id', 'name', 'createdAt', 'isActive'],
41+
'required' => ['id', 'name', 'createdAt', 'isActive', 'age'],
4242
'additionalProperties' => false,
4343
],
4444
'strict' => true,

src/platform/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
0.1
55
---
66

7+
* Add nullables as required in structured outputs
78
* Add support for Albert API for French/EU data sovereignty
89
* Add unified abstraction layer for interacting with various AI models and providers
910
* Add support for 16+ AI providers:

src/platform/src/Contract/JsonSchema/Factory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ private function convertTypes(array $elements): ?array
117117
if (!isset($schema['anyOf'])) {
118118
$schema['type'] = [$schema['type'], 'null'];
119119
}
120-
} elseif (!($element instanceof \ReflectionParameter && $element->isOptional())) {
120+
}
121+
122+
if (!($element instanceof \ReflectionParameter && $element->isOptional())) {
121123
$result['required'][] = $name;
122124
}
123125

src/platform/tests/Contract/JsonSchema/FactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function testBuildPropertiesForUserClass()
183183
'isActive' => ['type' => 'boolean'],
184184
'age' => ['type' => ['integer', 'null']],
185185
],
186-
'required' => ['id', 'name', 'createdAt', 'isActive'],
186+
'required' => ['id', 'name', 'createdAt', 'isActive', 'age'],
187187
'additionalProperties' => false,
188188
];
189189

@@ -304,7 +304,7 @@ public function testBuildPropertiesForUnionTypeDto()
304304
],
305305
],
306306
],
307-
'required' => [],
307+
'required' => ['time'],
308308
'additionalProperties' => false,
309309
];
310310

@@ -347,7 +347,7 @@ public function testBuildPropertiesForExampleDto()
347347
'enum' => ['Foo', 'Bar', null],
348348
],
349349
],
350-
'required' => ['name', 'taxRate'],
350+
'required' => ['name', 'taxRate', 'category'],
351351
'additionalProperties' => false,
352352
];
353353

0 commit comments

Comments
 (0)