Skip to content

fix: resolve array tool arguments #368

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
Jun 30, 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
3 changes: 2 additions & 1 deletion src/Chain/Toolbox/ToolCallArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function resolveArguments(object $tool, Tool $metadata, ToolCall $toolCal
$arguments = [];

foreach ($toolCall->arguments as $name => $value) {
$arguments[$name] = $this->denormalizer->denormalize($value, (string) $parameters[$name]->getType());
$parameterType = (string) $parameters[$name]->getType();
$arguments[$name] = 'array' === $parameterType ? $value : $this->denormalizer->denormalize($value, $parameterType);
}

return $arguments;
Expand Down
21 changes: 21 additions & 0 deletions tests/Chain/Toolbox/ToolCallArgumentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpLlm\LlmChain\Platform\Response\ToolCall;
use PhpLlm\LlmChain\Platform\Tool\ExecutionReference;
use PhpLlm\LlmChain\Platform\Tool\Tool;
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolArray;
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolDate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -29,4 +30,24 @@ public function resolveArguments(): void

self::assertEquals(['date' => new \DateTimeImmutable('2025-06-29')], $resolver->resolveArguments($tool, $metadata, $toolCall));
}

#[Test]
public function resolveScalarArrayArguments(): void
{
$resolver = new ToolCallArgumentResolver();

$tool = new ToolArray();
$metadata = new Tool(new ExecutionReference(ToolArray::class, '__invoke'), 'tool_array', 'A tool with array parameters');
$toolCall = new ToolCall('tool_id_1234', 'tool_array', [
'urls' => ['https://symfony.com', 'https://php.net'],
'ids' => [1, 2, 3],
]);

$expected = [
'urls' => ['https://symfony.com', 'https://php.net'],
'ids' => [1, 2, 3],
];

self::assertSame($expected, $resolver->resolveArguments($tool, $metadata, $toolCall));
}
}
2 changes: 1 addition & 1 deletion tests/Fixture/Tool/ToolArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;

#[AsTool('tool_no_params', 'A tool without parameters')]
#[AsTool('tool_array', 'A tool with array parameters')]
final class ToolArray
{
/**
Expand Down