Skip to content
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: 1 addition & 1 deletion examples/vertexai/toolcall.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

$toolbox = new Toolbox([new Clock()], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, 'gemini-2.0-flash-lite', [$processor], [$processor], logger: logger());
$agent = new Agent($platform, 'gemini-2.5-flash-lite', [$processor], [$processor], logger: logger());

$messages = new MessageBag(Message::ofUser('What time is it?'));
$result = $agent->call($messages);
Expand Down
9 changes: 6 additions & 3 deletions src/platform/src/Bridge/Gemini/Gemini/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,15 @@ private function convertChoice(array $choice): ToolCallResult|TextResult
{
$contentParts = $choice['content']['parts'];

if (1 === \count($contentParts)) {
$contentPart = $contentParts[0];

// If any part is a function call, return it immediately and ignore all other parts.
foreach ($contentParts as $contentPart) {
if (isset($contentPart['functionCall'])) {
return new ToolCallResult($this->convertToolCall($contentPart['functionCall']));
}
}

if (1 === \count($contentParts)) {
$contentPart = $contentParts[0];

if (isset($contentPart['text'])) {
return new TextResult($contentPart['text']);
Expand Down
9 changes: 6 additions & 3 deletions src/platform/src/Bridge/VertexAi/Gemini/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ private function convertChoice(array $choice): ToolCallResult|TextResult
{
$contentParts = $choice['content']['parts'];

if (1 === \count($contentParts)) {
$contentPart = $contentParts[0];

// If any part is a function call, return it immediately and ignore all other parts.
foreach ($contentParts as $contentPart) {
if (isset($contentPart['functionCall'])) {
return new ToolCallResult($this->convertToolCall($contentPart['functionCall']));
}
}

if (1 === \count($contentParts)) {
$contentPart = $contentParts[0];

if (isset($contentPart['text'])) {
return new TextResult($contentPart['text']);
Expand Down
36 changes: 36 additions & 0 deletions src/platform/tests/Bridge/Gemini/Gemini/ResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\AI\Platform\Result\ToolCallResult;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
Expand All @@ -40,4 +42,38 @@ public function testConvertThrowsExceptionWithDetailedErrorInformation()

$converter->convert(new RawHttpResult($httpResponse));
}

public function testReturnsToolCallEvenIfMultipleContentPartsAreGiven()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('getStatusCode')->willReturn(200);
$httpResponse->method('toArray')->willReturn([
'candidates' => [
[
'content' => [
'parts' => [
[
'text' => 'foo',
],
[
'functionCall' => [
'id' => '1234',
'name' => 'some_tool',
'args' => [],
],
],
],
],
],
],
]);

$result = $converter->convert(new RawHttpResult($httpResponse));
$this->assertInstanceOf(ToolCallResult::class, $result);
$this->assertCount(1, $result->getContent());
$toolCall = $result->getContent()[0];
$this->assertInstanceOf(ToolCall::class, $toolCall);
$this->assertSame('1234', $toolCall->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\ResultConverter;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\TextResult;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\AI\Platform\Result\ToolCallResult;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class ResultConverterTest extends TestCase
Expand Down Expand Up @@ -57,6 +59,42 @@ public function testItReturnsAggregatedTextOnSuccess()
$this->assertEquals("Second text\nThird text\nFourth text", $result->getContent());
}

public function testItReturnsToolCallEvenIfMultipleContentPartsAreGiven()
{
$payload = [
'content' => [
'parts' => [
[
'text' => 'foo',
],
[
'functionCall' => [
'name' => 'some_tool',
'args' => [],
],
],
],
],
];
$expectedResponse = [
'candidates' => [$payload],
];
$response = $this->createStub(ResponseInterface::class);
$response
->method('toArray')
->willReturn($expectedResponse);

$resultConverter = new ResultConverter();

$result = $resultConverter->convert(new RawHttpResult($response));

$this->assertInstanceOf(ToolCallResult::class, $result);
$this->assertCount(1, $result->getContent());
$toolCall = $result->getContent()[0];
$this->assertInstanceOf(ToolCall::class, $toolCall);
$this->assertSame('some_tool', $toolCall->id);
}

public function testItThrowsExceptionOnFailure()
{
$response = $this->createStub(ResponseInterface::class);
Expand Down