Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,24 @@ function (Message $message): array {
'tool_call_id' => $functionResponse->getId(),
];
}
return [
$messageData = [
'role' => $this->getMessageRoleString($message->getRole()),
'content' => array_values(array_filter(array_map(
[$this, 'getMessagePartContentData'],
$messageParts
))),
'tool_calls' => array_values(array_filter(array_map(
[$this, 'getMessagePartToolCallData'],
$messageParts
))),
];

// Only include tool_calls if there are any (OpenAI rejects empty arrays).
$toolCalls = array_values(array_filter(array_map(
[$this, 'getMessagePartToolCallData'],
$messageParts
)));
if (!empty($toolCalls)) {
$messageData['tool_calls'] = $toolCalls;
}

return $messageData;
},
$messages
);
Expand Down Expand Up @@ -398,12 +405,26 @@ protected function getMessagePartToolCallData(MessagePart $part): ?array
'The function call typed message part must contain a function call.'
);
}
$args = $functionCall->getArgs();

/*
* Ensure empty arrays become empty objects for JSON encoding.
* While in theory the JSON schema could also dictate a type of
* 'array', in practice function arguments are typically of type
* 'object'. More importantly, the OpenAI API specification seems
* to expect that, and does not support passing arrays as the root
* value.
*/
if (is_array($args) && count($args) === 0) {
$args = new \stdClass();
}

return [
'type' => 'function',
'id' => $functionCall->getId(),
'function' => [
'name' => $functionCall->getName(),
'arguments' => json_encode($functionCall->getArgs()),
'arguments' => json_encode($args),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,26 @@ public function testPrepareMessagesParamModelMessageWithFunctionCall(): void
);
}

/**
* Tests prepareMessagesParam with message having no function calls (tool_calls should not be included).
*
* @return void
*/
public function testPrepareMessagesParamNoToolCalls(): void
{
$message = new Message(
MessageRoleEnum::model(),
[new MessagePart('Hello, I am a simple text response.')]
);

$model = $this->createModel();
$prepared = $model->exposePrepareMessagesParam([$message], null);

$this->assertCount(1, $prepared);
$this->assertEquals('assistant', $prepared[0]['role']);
$this->assertArrayNotHasKey('tool_calls', $prepared[0]); // Should not have tool_calls field at all
}

/**
* Tests prepareMessagesParam() with function response.
*
Expand Down Expand Up @@ -744,6 +764,32 @@ public function testGetMessagePartToolCallDataFunctionCallPart(): void
], $data);
}

/**
* Tests getMessagePartToolCallData() with empty arguments (should encode as empty object).
*
* @return void
*/
public function testGetMessagePartToolCallDataEmptyArguments(): void
{
$functionCall = new FunctionCall(
'call_1',
'list_capabilities',
[] // Empty arguments array
);
$part = new MessagePart($functionCall);
$model = $this->createModel();
$data = $model->exposeGetMessagePartToolCallData($part);

$this->assertEquals([
'type' => 'function',
'id' => 'call_1',
'function' => [
'name' => 'list_capabilities',
'arguments' => '{}', // Should be empty object, not empty array
],
], $data);
}

/**
* Tests getMessagePartToolCallData() with text part (should return null).
*
Expand Down