Skip to content

Commit 58186ba

Browse files
authored
fix: resolve array tool arguments (#368)
@valtzu any idea about this case? found it after merging #359 with example [examples/toolbox/tavily.php](https://github.com/php-llm/llm-chain/blob/main/examples/toolbox/tavily.php) patch feels a bit hacky, but does the job atm
1 parent 16fc8b0 commit 58186ba

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Chain/Toolbox/ToolCallArgumentResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function resolveArguments(object $tool, Tool $metadata, ToolCall $toolCal
2828
$arguments = [];
2929

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

3435
return $arguments;

tests/Chain/Toolbox/ToolCallArgumentResolverTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpLlm\LlmChain\Platform\Response\ToolCall;
77
use PhpLlm\LlmChain\Platform\Tool\ExecutionReference;
88
use PhpLlm\LlmChain\Platform\Tool\Tool;
9+
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolArray;
910
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolDate;
1011
use PHPUnit\Framework\Attributes\CoversClass;
1112
use PHPUnit\Framework\Attributes\Test;
@@ -29,4 +30,24 @@ public function resolveArguments(): void
2930

3031
self::assertEquals(['date' => new \DateTimeImmutable('2025-06-29')], $resolver->resolveArguments($tool, $metadata, $toolCall));
3132
}
33+
34+
#[Test]
35+
public function resolveScalarArrayArguments(): void
36+
{
37+
$resolver = new ToolCallArgumentResolver();
38+
39+
$tool = new ToolArray();
40+
$metadata = new Tool(new ExecutionReference(ToolArray::class, '__invoke'), 'tool_array', 'A tool with array parameters');
41+
$toolCall = new ToolCall('tool_id_1234', 'tool_array', [
42+
'urls' => ['https://symfony.com', 'https://php.net'],
43+
'ids' => [1, 2, 3],
44+
]);
45+
46+
$expected = [
47+
'urls' => ['https://symfony.com', 'https://php.net'],
48+
'ids' => [1, 2, 3],
49+
];
50+
51+
self::assertSame($expected, $resolver->resolveArguments($tool, $metadata, $toolCall));
52+
}
3253
}

tests/Fixture/Tool/ToolArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
#[AsTool('tool_no_params', 'A tool without parameters')]
9+
#[AsTool('tool_array', 'A tool with array parameters')]
1010
final class ToolArray
1111
{
1212
/**

0 commit comments

Comments
 (0)