From c60c7135452d528f00c5461958b04b8231bd40e7 Mon Sep 17 00:00:00 2001 From: Dave Heineman Date: Fri, 29 Aug 2025 12:34:11 +0200 Subject: [PATCH] Allow the JsonSchema With attribute to be used for structured output --- fixtures/StructuredOutput/MathReasoning.php | 4 ++++ src/agent/tests/StructuredOutput/AgentProcessorTest.php | 2 ++ src/platform/src/Contract/JsonSchema/Attribute/With.php | 2 +- src/platform/tests/Contract/JsonSchema/FactoryTest.php | 3 ++- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/fixtures/StructuredOutput/MathReasoning.php b/fixtures/StructuredOutput/MathReasoning.php index 9d49f9842..edb2e3e26 100644 --- a/fixtures/StructuredOutput/MathReasoning.php +++ b/fixtures/StructuredOutput/MathReasoning.php @@ -11,6 +11,8 @@ namespace Symfony\AI\Fixtures\StructuredOutput; +use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With; + final class MathReasoning { /** @@ -18,6 +20,8 @@ final class MathReasoning */ public function __construct( public array $steps, + #[With(minimum: 0, maximum: 100)] + public int $confidence, public string $finalAnswer, ) { } diff --git a/src/agent/tests/StructuredOutput/AgentProcessorTest.php b/src/agent/tests/StructuredOutput/AgentProcessorTest.php index 150a87591..6e23369e3 100644 --- a/src/agent/tests/StructuredOutput/AgentProcessorTest.php +++ b/src/agent/tests/StructuredOutput/AgentProcessorTest.php @@ -130,6 +130,7 @@ public function testProcessOutputWithComplexResponseFormat() "output": "x = -3.75" } ], + "confidence": 100, "finalAnswer": "x = -3.75" } JSON); @@ -148,6 +149,7 @@ public function testProcessOutputWithComplexResponseFormat() $this->assertInstanceOf(Step::class, $structure->steps[2]); $this->assertInstanceOf(Step::class, $structure->steps[3]); $this->assertInstanceOf(Step::class, $structure->steps[4]); + $this->assertSame(100, $structure->confidence); $this->assertSame('x = -3.75', $structure->finalAnswer); } diff --git a/src/platform/src/Contract/JsonSchema/Attribute/With.php b/src/platform/src/Contract/JsonSchema/Attribute/With.php index 7dcc71c50..8bb905e0b 100644 --- a/src/platform/src/Contract/JsonSchema/Attribute/With.php +++ b/src/platform/src/Contract/JsonSchema/Attribute/With.php @@ -16,7 +16,7 @@ /** * @author Oskar Stark */ -#[\Attribute(\Attribute::TARGET_PARAMETER)] +#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] final readonly class With { /** diff --git a/src/platform/tests/Contract/JsonSchema/FactoryTest.php b/src/platform/tests/Contract/JsonSchema/FactoryTest.php index e5cd88f5b..d9e50a23a 100644 --- a/src/platform/tests/Contract/JsonSchema/FactoryTest.php +++ b/src/platform/tests/Contract/JsonSchema/FactoryTest.php @@ -212,9 +212,10 @@ public function testBuildPropertiesForMathReasoningClass() 'additionalProperties' => false, ], ], + 'confidence' => ['type' => 'integer', 'minimum' => 0, 'maximum' => 100], 'finalAnswer' => ['type' => 'string'], ], - 'required' => ['steps', 'finalAnswer'], + 'required' => ['steps', 'confidence', 'finalAnswer'], 'additionalProperties' => false, ];