Skip to content

[Platform] Simplify choice handling with multiple results #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
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
2 changes: 0 additions & 2 deletions src/agent/tests/StructuredOutput/AgentProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\Metadata\Metadata;
use Symfony\AI\Platform\Result\ObjectResult;
use Symfony\AI\Platform\Result\TextResult;
Expand All @@ -34,7 +33,6 @@
#[UsesClass(Input::class)]
#[UsesClass(Output::class)]
#[UsesClass(MessageBag::class)]
#[UsesClass(Choice::class)]
#[UsesClass(MissingModelSupportException::class)]
#[UsesClass(TextResult::class)]
#[UsesClass(ObjectResult::class)]
Expand Down
27 changes: 5 additions & 22 deletions src/platform/src/Bridge/Gemini/Gemini/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\AI\Platform\Bridge\Gemini\Gemini;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\RawResultInterface;
Expand Down Expand Up @@ -49,18 +48,9 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
throw new RuntimeException('Response does not contain any content.');
}

/** @var Choice[] $choices */
$choices = array_map($this->convertChoice(...), $data['candidates']);

if (1 !== \count($choices)) {
return new ChoiceResult(...$choices);
}

if ($choices[0]->hasToolCall()) {
return new ToolCallResult(...$choices[0]->getToolCalls());
}

return new TextResult($choices[0]->getContent());
return 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices);
}

private function convertStream(HttpResponse $result): \Generator
Expand Down Expand Up @@ -94,7 +84,6 @@ private function convertStream(HttpResponse $result): \Generator
throw new RuntimeException('Failed to decode JSON response.', 0, $e);
}

/** @var Choice[] $choices */
$choices = array_map($this->convertChoice(...), $data['candidates'] ?? []);

if (!$choices) {
Expand All @@ -106,13 +95,7 @@ private function convertStream(HttpResponse $result): \Generator
continue;
}

if ($choices[0]->hasToolCall()) {
yield new ToolCallResult(...$choices[0]->getToolCalls());
}

if ($choices[0]->hasContent()) {
yield $choices[0]->getContent();
}
yield $choices[0]->getContent();
}
}
}
Expand All @@ -132,16 +115,16 @@ private function convertStream(HttpResponse $result): \Generator
* }
* } $choice
*/
private function convertChoice(array $choice): Choice
private function convertChoice(array $choice): ToolCallResult|TextResult
{
$contentPart = $choice['content']['parts'][0] ?? [];

if (isset($contentPart['functionCall'])) {
return new Choice(toolCalls: [$this->convertToolCall($contentPart['functionCall'])]);
return new ToolCallResult($this->convertToolCall($contentPart['functionCall']));
}

if (isset($contentPart['text'])) {
return new Choice($contentPart['text']);
return new TextResult($contentPart['text']);
}

throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finishReason']));
Expand Down
18 changes: 4 additions & 14 deletions src/platform/src/Bridge/Mistral/Llm/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\RawResultInterface;
Expand Down Expand Up @@ -60,18 +59,9 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
throw new RuntimeException('Response does not contain choices.');
}

/** @var Choice[] $choices */
$choices = array_map($this->convertChoice(...), $data['choices']);

if (1 !== \count($choices)) {
return new ChoiceResult(...$choices);
}

if ($choices[0]->hasToolCall()) {
return new ToolCallResult(...$choices[0]->getToolCalls());
}

return new TextResult($choices[0]->getContent());
return 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices);
}

private function convertStream(HttpResponse $result): \Generator
Expand Down Expand Up @@ -170,14 +160,14 @@ private function isToolCallsStreamFinished(array $data): bool
* finish_reason: 'stop'|'length'|'tool_calls'|'content_filter',
* } $choice
*/
private function convertChoice(array $choice): Choice
private function convertChoice(array $choice): ToolCallResult|TextResult
{
if ('tool_calls' === $choice['finish_reason']) {
return new Choice(toolCalls: array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
return new ToolCallResult(...array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
}

if ('stop' === $choice['finish_reason']) {
return new Choice($choice['message']['content']);
return new TextResult($choice['message']['content']);
}

throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finish_reason']));
Expand Down
18 changes: 4 additions & 14 deletions src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\RawResultInterface;
Expand Down Expand Up @@ -57,18 +56,9 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
throw new RuntimeException('Response does not contain choices.');
}

/** @var Choice[] $choices */
$choices = array_map($this->convertChoice(...), $data['choices']);

if (1 !== \count($choices)) {
return new ChoiceResult(...$choices);
}

if ($choices[0]->hasToolCall()) {
return new ToolCallResult(...$choices[0]->getToolCalls());
}

return new TextResult($choices[0]->getContent());
return 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices);
}

private function convertStream(HttpResponse $result): \Generator
Expand Down Expand Up @@ -167,14 +157,14 @@ private function isToolCallsStreamFinished(array $data): bool
* finish_reason: 'stop'|'length'|'tool_calls'|'content_filter',
* } $choice
*/
private function convertChoice(array $choice): Choice
private function convertChoice(array $choice): ToolCallResult|TextResult
{
if ('tool_calls' === $choice['finish_reason']) {
return new Choice(toolCalls: array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
return new ToolCallResult(...array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
}

if (\in_array($choice['finish_reason'], ['stop', 'length'], true)) {
return new Choice($choice['message']['content']);
return new TextResult($choice['message']['content']);
}

throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finish_reason']));
Expand Down
50 changes: 0 additions & 50 deletions src/platform/src/Result/Choice.php

This file was deleted.

16 changes: 8 additions & 8 deletions src/platform/src/Result/ChoiceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@
final class ChoiceResult extends BaseResult
{
/**
* @var Choice[]
* @var ResultInterface[]
*/
private readonly array $choices;
private readonly array $results;

public function __construct(Choice ...$choices)
public function __construct(ResultInterface ...$results)
{
if ([] === $choices) {
throw new InvalidArgumentException('Result must have at least one choice.');
if (1 >= \count($results)) {
throw new InvalidArgumentException('A choice result must contain at least two results.');
}

$this->choices = $choices;
$this->results = $results;
}

/**
* @return Choice[]
* @return ResultInterface[]
*/
public function getContent(): array
{
return $this->choices;
return $this->results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\TextResult;
Expand All @@ -29,7 +28,6 @@

#[CoversClass(ResultConverter::class)]
#[Small]
#[UsesClass(Choice::class)]
#[UsesClass(ChoiceResult::class)]
#[UsesClass(TextResult::class)]
#[UsesClass(ToolCall::class)]
Expand Down
18 changes: 7 additions & 11 deletions src/platform/tests/Result/ChoiceResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,30 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Result\Choice;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\TextResult;

#[CoversClass(ChoiceResult::class)]
#[UsesClass(Choice::class)]
#[Small]
final class ChoiceResultTest extends TestCase
{
public function testChoiceResultCreation()
{
$choice1 = new Choice('choice1');
$choice2 = new Choice(null);
$choice3 = new Choice('choice3');
$result = new ChoiceResult($choice1, $choice2, $choice3);
$choice1 = new TextResult('choice1');
$choice3 = new TextResult('choice2');
$result = new ChoiceResult($choice1, $choice3);

$this->assertCount(3, $result->getContent());
$this->assertCount(2, $result->getContent());
$this->assertSame('choice1', $result->getContent()[0]->getContent());
$this->assertNull($result->getContent()[1]->getContent());
$this->assertSame('choice3', $result->getContent()[2]->getContent());
$this->assertSame('choice2', $result->getContent()[1]->getContent());
}

public function testChoiceResultWithNoChoices()
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('Result must have at least one choice.');
self::expectExceptionMessage('A choice result must contain at least two results.');

new ChoiceResult();
}
Expand Down
61 changes: 0 additions & 61 deletions src/platform/tests/Result/ChoiceTest.php

This file was deleted.