[Platform][Generic][Scaleway] Fix tool call without arguments

This commit is contained in:
Guillaume RODRIGUEZ
2026-03-19 12:23:11 +01:00
committed by Christopher Hertel
parent fbde7914f9
commit ae0b77c180
2 changed files with 85 additions and 4 deletions

View File

@@ -70,7 +70,13 @@ trait CompletionsConversionTrait
}
// add arguments delta to tool call
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
if (isset($toolCall['function']['arguments'])) {
if (!isset($toolCalls[$i]['function']['arguments'])) {
$toolCalls[$i]['function']['arguments'] = '';
}
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
}
}
return $toolCalls;
@@ -131,13 +137,17 @@ trait CompletionsConversionTrait
* type: 'function',
* function: array{
* name: string,
* arguments: string
* arguments?: string
* }
* } $toolCall
*/
protected function convertToolCall(array $toolCall): ToolCall
{
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
if (isset($toolCall['function']['arguments']) && '' !== $toolCall['function']['arguments']) {
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
} else {
$arguments = [];
}
return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments);
}

View File

@@ -48,7 +48,7 @@ class ResultConverterTest extends TestCase
$this->assertSame('Hello world', $result->getContent());
}
public function testConvertToolCallResult()
public function testConvertToolWithArgsCallResult()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
@@ -84,6 +84,77 @@ class ResultConverterTest extends TestCase
$this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments());
}
public function testConvertToolWithEmptyArgsCallResult()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('toArray')->willReturn([
'choices' => [
[
'message' => [
'role' => 'assistant',
'content' => null,
'tool_calls' => [
[
'id' => 'call_123',
'type' => 'function',
'function' => [
'name' => 'test_function',
'arguments' => '',
],
],
],
],
'finish_reason' => 'tool_calls',
],
],
]);
$result = $converter->convert(new RawHttpResult($httpResponse));
$this->assertInstanceOf(ToolCallResult::class, $result);
$toolCalls = $result->getContent();
$this->assertCount(1, $toolCalls);
$this->assertSame('call_123', $toolCalls[0]->getId());
$this->assertSame('test_function', $toolCalls[0]->getName());
$this->assertSame([], $toolCalls[0]->getArguments());
}
public function testConvertToolWithoutArgsCallResult()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('toArray')->willReturn([
'choices' => [
[
'message' => [
'role' => 'assistant',
'content' => null,
'tool_calls' => [
[
'id' => 'call_123',
'type' => 'function',
'function' => [
'name' => 'test_function',
],
],
],
],
'finish_reason' => 'tool_calls',
],
],
]);
$result = $converter->convert(new RawHttpResult($httpResponse));
$this->assertInstanceOf(ToolCallResult::class, $result);
$toolCalls = $result->getContent();
$this->assertCount(1, $toolCalls);
$this->assertSame('call_123', $toolCalls[0]->getId());
$this->assertSame('test_function', $toolCalls[0]->getName());
$this->assertSame([], $toolCalls[0]->getArguments());
}
public function testConvertMultipleChoices()
{
$converter = new ResultConverter();