Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae0b77c

Browse files
guillaume-ro-frchr-hertel
authored andcommitted
[Platform][Generic][Scaleway] Fix tool call without arguments
1 parent fbde791 commit ae0b77c

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

Completions/CompletionsConversionTrait.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ protected function convertStreamToToolCalls(array $toolCalls, array $data): arra
7070
}
7171

7272
// add arguments delta to tool call
73-
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
73+
if (isset($toolCall['function']['arguments'])) {
74+
if (!isset($toolCalls[$i]['function']['arguments'])) {
75+
$toolCalls[$i]['function']['arguments'] = '';
76+
}
77+
78+
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
79+
}
7480
}
7581

7682
return $toolCalls;
@@ -131,13 +137,17 @@ protected function convertChoice(array $choice): ToolCallResult|TextResult
131137
* type: 'function',
132138
* function: array{
133139
* name: string,
134-
* arguments: string
140+
* arguments?: string
135141
* }
136142
* } $toolCall
137143
*/
138144
protected function convertToolCall(array $toolCall): ToolCall
139145
{
140-
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
146+
if (isset($toolCall['function']['arguments']) && '' !== $toolCall['function']['arguments']) {
147+
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
148+
} else {
149+
$arguments = [];
150+
}
141151

142152
return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments);
143153
}

Tests/Completions/ResultConverterTest.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testConvertTextResult()
4848
$this->assertSame('Hello world', $result->getContent());
4949
}
5050

51-
public function testConvertToolCallResult()
51+
public function testConvertToolWithArgsCallResult()
5252
{
5353
$converter = new ResultConverter();
5454
$httpResponse = self::createMock(ResponseInterface::class);
@@ -84,6 +84,77 @@ public function testConvertToolCallResult()
8484
$this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments());
8585
}
8686

87+
public function testConvertToolWithEmptyArgsCallResult()
88+
{
89+
$converter = new ResultConverter();
90+
$httpResponse = self::createMock(ResponseInterface::class);
91+
$httpResponse->method('toArray')->willReturn([
92+
'choices' => [
93+
[
94+
'message' => [
95+
'role' => 'assistant',
96+
'content' => null,
97+
'tool_calls' => [
98+
[
99+
'id' => 'call_123',
100+
'type' => 'function',
101+
'function' => [
102+
'name' => 'test_function',
103+
'arguments' => '',
104+
],
105+
],
106+
],
107+
],
108+
'finish_reason' => 'tool_calls',
109+
],
110+
],
111+
]);
112+
113+
$result = $converter->convert(new RawHttpResult($httpResponse));
114+
115+
$this->assertInstanceOf(ToolCallResult::class, $result);
116+
$toolCalls = $result->getContent();
117+
$this->assertCount(1, $toolCalls);
118+
$this->assertSame('call_123', $toolCalls[0]->getId());
119+
$this->assertSame('test_function', $toolCalls[0]->getName());
120+
$this->assertSame([], $toolCalls[0]->getArguments());
121+
}
122+
123+
public function testConvertToolWithoutArgsCallResult()
124+
{
125+
$converter = new ResultConverter();
126+
$httpResponse = self::createMock(ResponseInterface::class);
127+
$httpResponse->method('toArray')->willReturn([
128+
'choices' => [
129+
[
130+
'message' => [
131+
'role' => 'assistant',
132+
'content' => null,
133+
'tool_calls' => [
134+
[
135+
'id' => 'call_123',
136+
'type' => 'function',
137+
'function' => [
138+
'name' => 'test_function',
139+
],
140+
],
141+
],
142+
],
143+
'finish_reason' => 'tool_calls',
144+
],
145+
],
146+
]);
147+
148+
$result = $converter->convert(new RawHttpResult($httpResponse));
149+
150+
$this->assertInstanceOf(ToolCallResult::class, $result);
151+
$toolCalls = $result->getContent();
152+
$this->assertCount(1, $toolCalls);
153+
$this->assertSame('call_123', $toolCalls[0]->getId());
154+
$this->assertSame('test_function', $toolCalls[0]->getName());
155+
$this->assertSame([], $toolCalls[0]->getArguments());
156+
}
157+
87158
public function testConvertMultipleChoices()
88159
{
89160
$converter = new ResultConverter();

0 commit comments

Comments
 (0)