Skip to content

Commit 9b530fc

Browse files
authored
feat: allow null as type in schema (#195)
* Allow null as type in schema * - * - * - * -
1 parent bbd06dc commit 9b530fc

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

src/Chain/StructuredOutput/SchemaFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public function buildSchema(string $className): array
5959
$type = $types[0];
6060
$propertySchema = $this->getTypeSchema($type);
6161

62+
// Handle nullable types
63+
if (!is_array($propertySchema['type']) && $type->isNullable()) {
64+
$propertySchema['type'] = [$propertySchema['type'], 'null'];
65+
}
66+
6267
// Add description if available
6368
if ($description) {
6469
$propertySchema['description'] = $description;

tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function create(): void
3737
'format' => 'date-time',
3838
],
3939
'isActive' => ['type' => 'boolean'],
40+
'age' => ['type' => ['integer', 'null']],
4041
],
4142
'required' => ['id', 'name', 'createdAt', 'isActive'],
4243
'additionalProperties' => false,

tests/Chain/StructuredOutput/SchemaFactoryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function buildSchemaForUserClass(): void
3939
'format' => 'date-time',
4040
],
4141
'isActive' => ['type' => 'boolean'],
42+
'age' => ['type' => ['integer', 'null']],
4243
],
4344
'required' => ['id', 'name', 'createdAt', 'isActive'],
4445
'additionalProperties' => false,

tests/Fixture/StructuredOutput/User.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ final class User
1313
public string $name;
1414
public \DateTimeInterface $createdAt;
1515
public bool $isActive;
16+
public ?int $age = null;
1617
}

0 commit comments

Comments
 (0)