Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Read description for schema to give more context to the LLM #38

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/StructuredOutput/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function buildSchema(string $className): array
foreach ($properties as $property) {
$propertyName = $property->getName();
$types = $this->propertyInfo->getTypes($className, $propertyName);
$description = $this->propertyInfo->getShortDescription($className, $propertyName);

if (empty($types)) {
// Skip if no type info is available
Expand All @@ -62,6 +63,11 @@ public function buildSchema(string $className): array
$type = $types[0];
$propertySchema = $this->getTypeSchema($type);

// Add description if available
if ($description) {
$propertySchema['description'] = $description;
}

// Add property schema to main schema
$schema['properties'][$propertyName] = $propertySchema;

Expand Down
3 changes: 3 additions & 0 deletions tests/StructuredOutput/Data/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
final class User
{
public int $id;
/**
* @var string The name of the user in lowercase
*/
public string $name;
public \DateTimeInterface $createdAt;
public bool $isActive;
Expand Down
5 changes: 4 additions & 1 deletion tests/StructuredOutput/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function buildSchemaForUserClass(): void
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'name' => [
'type' => 'string',
'description' => 'The name of the user in lowercase',
],
'createdAt' => [
'type' => 'string',
'format' => 'date-time',
Expand Down