Skip to content

Commit eac6a44

Browse files
committed
feature #365 [Platform] Allow to use the #[With] attribute for structured output (dheineman)
This PR was merged into the main branch. Discussion ---------- [Platform] Allow to use the `#[With]` attribute for structured output | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | Fix #364 | License | MIT Allows the `#[With]` attribute to be set on properties thus making it usable on structured output classes. Commits ------- c60c713 Allow the JsonSchema With attribute to be used for structured output
2 parents 117aea9 + c60c713 commit eac6a44

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

fixtures/StructuredOutput/MathReasoning.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
namespace Symfony\AI\Fixtures\StructuredOutput;
1313

14+
use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With;
15+
1416
final class MathReasoning
1517
{
1618
/**
1719
* @param Step[] $steps
1820
*/
1921
public function __construct(
2022
public array $steps,
23+
#[With(minimum: 0, maximum: 100)]
24+
public int $confidence,
2125
public string $finalAnswer,
2226
) {
2327
}

src/agent/tests/StructuredOutput/AgentProcessorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function testProcessOutputWithComplexResponseFormat()
130130
"output": "x = -3.75"
131131
}
132132
],
133+
"confidence": 100,
133134
"finalAnswer": "x = -3.75"
134135
}
135136
JSON);
@@ -148,6 +149,7 @@ public function testProcessOutputWithComplexResponseFormat()
148149
$this->assertInstanceOf(Step::class, $structure->steps[2]);
149150
$this->assertInstanceOf(Step::class, $structure->steps[3]);
150151
$this->assertInstanceOf(Step::class, $structure->steps[4]);
152+
$this->assertSame(100, $structure->confidence);
151153
$this->assertSame('x = -3.75', $structure->finalAnswer);
152154
}
153155

src/platform/src/Contract/JsonSchema/Attribute/With.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Oskar Stark <[email protected]>
1818
*/
19-
#[\Attribute(\Attribute::TARGET_PARAMETER)]
19+
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)]
2020
final readonly class With
2121
{
2222
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ public function testBuildPropertiesForMathReasoningClass()
212212
'additionalProperties' => false,
213213
],
214214
],
215+
'confidence' => ['type' => 'integer', 'minimum' => 0, 'maximum' => 100],
215216
'finalAnswer' => ['type' => 'string'],
216217
],
217-
'required' => ['steps', 'finalAnswer'],
218+
'required' => ['steps', 'confidence', 'finalAnswer'],
218219
'additionalProperties' => false,
219220
];
220221

0 commit comments

Comments
 (0)