Skip to content

Commit d9bb489

Browse files
authored
fix: empty tool calls (#430)
1 parent b70c9c5 commit d9bb489

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/ValueObjects/ToolCall.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(
2525
public function arguments(): array
2626
{
2727
if (is_string($this->arguments)) {
28+
if ($this->arguments === '' || $this->arguments === '0') {
29+
return [];
30+
}
31+
2832
/** @var string $arguments */
2933
$arguments = $this->arguments;
3034

tests/ValueObjects/ToolCallTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Prism\Prism\ValueObjects\ToolCall;
6+
7+
it('handles empty string arguments correctly', function (): void {
8+
$toolCall = new ToolCall(
9+
id: 'test-id',
10+
name: 'test-tool',
11+
arguments: ''
12+
);
13+
14+
expect($toolCall->arguments())->toBe([]);
15+
});
16+
17+
it('handles null arguments correctly', function (): void {
18+
$toolCall = new ToolCall(
19+
id: 'test-id',
20+
name: 'test-tool',
21+
arguments: []
22+
);
23+
24+
expect($toolCall->arguments())->toBe([]);
25+
});
26+
27+
it('handles valid JSON string arguments correctly', function (): void {
28+
$toolCall = new ToolCall(
29+
id: 'test-id',
30+
name: 'test-tool',
31+
arguments: '{"param1": "value1", "param2": 42}'
32+
);
33+
34+
expect($toolCall->arguments())->toBe([
35+
'param1' => 'value1',
36+
'param2' => 42,
37+
]);
38+
});
39+
40+
it('handles array arguments correctly', function (): void {
41+
$arguments = ['param1' => 'value1', 'param2' => 42];
42+
43+
$toolCall = new ToolCall(
44+
id: 'test-id',
45+
name: 'test-tool',
46+
arguments: $arguments
47+
);
48+
49+
expect($toolCall->arguments())->toBe($arguments);
50+
});
51+
52+
it('throws exception for malformed JSON string arguments', function (): void {
53+
$toolCall = new ToolCall(
54+
id: 'test-id',
55+
name: 'test-tool',
56+
arguments: '{"invalid json"'
57+
);
58+
59+
expect(fn (): array => $toolCall->arguments())->toThrow(JsonException::class);
60+
});

0 commit comments

Comments
 (0)