Skip to content

Commit a9f460c

Browse files
fix: encode empty tool arguments as JSON object instead of array (#896)
1 parent d6aed55 commit a9f460c

12 files changed

Lines changed: 167 additions & 6 deletions

File tree

src/Providers/DeepSeek/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
9595
'type' => 'function',
9696
'function' => [
9797
'name' => $toolCall->name,
98-
'arguments' => json_encode($toolCall->arguments()),
98+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
9999
],
100100
], $message->toolCalls);
101101

src/Providers/Groq/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
9595
'type' => 'function',
9696
'function' => [
9797
'name' => $toolCall->name,
98-
'arguments' => json_encode($toolCall->arguments()),
98+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
9999
],
100100
], $message->toolCalls);
101101

src/Providers/Mistral/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
9696
'type' => 'function',
9797
'function' => [
9898
'name' => $toolCall->name,
99-
'arguments' => json_encode($toolCall->arguments()),
99+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
100100
],
101101
], $message->toolCalls);
102102

src/Providers/OpenAI/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
155155
'call_id' => $toolCall->resultId,
156156
'type' => 'function_call',
157157
'name' => $toolCall->name,
158-
'arguments' => json_encode($toolCall->arguments()),
158+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
159159
], $message->toolCalls)
160160
);
161161
}

src/Providers/OpenRouter/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
159159
'type' => 'function',
160160
'function' => [
161161
'name' => $toolCall->name,
162-
'arguments' => json_encode($toolCall->arguments()),
162+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
163163
],
164164
], $message->toolCalls);
165165

src/Providers/XAI/Maps/MessageMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function mapAssistantMessage(AssistantMessage $message): void
9595
'type' => 'function',
9696
'function' => [
9797
'name' => $toolCall->name,
98-
'arguments' => json_encode($toolCall->arguments()),
98+
'arguments' => json_encode($toolCall->arguments() ?: (object) []),
9999
],
100100
], $message->toolCalls);
101101

tests/Providers/DeepSeek/MessageMapTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@
137137
]]);
138138
});
139139

140+
it('maps assistant message with tool calls with empty arguments as json object', function (): void {
141+
$messageMap = new MessageMap(
142+
messages: [
143+
new AssistantMessage('', [
144+
new ToolCall(
145+
'tool_1234',
146+
'get_schema',
147+
[]
148+
),
149+
]),
150+
],
151+
systemPrompts: []
152+
);
153+
154+
expect($messageMap())->toBe([[
155+
'role' => 'assistant',
156+
'tool_calls' => [[
157+
'id' => 'tool_1234',
158+
'type' => 'function',
159+
'function' => [
160+
'name' => 'get_schema',
161+
'arguments' => '{}',
162+
],
163+
]],
164+
]]);
165+
});
166+
140167
it('maps tool result messages', function (): void {
141168
$messageMap = new MessageMap(
142169
messages: [

tests/Providers/Groq/MessageMapTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@
139139
]]);
140140
});
141141

142+
it('maps assistant message with tool calls with empty arguments as json object', function (): void {
143+
$messageMap = new MessageMap(
144+
messages: [
145+
new AssistantMessage('', [
146+
new ToolCall(
147+
'tool_1234',
148+
'get_schema',
149+
[]
150+
),
151+
]),
152+
],
153+
systemPrompts: []
154+
);
155+
156+
expect($messageMap())->toBe([[
157+
'role' => 'assistant',
158+
'tool_calls' => [[
159+
'id' => 'tool_1234',
160+
'type' => 'function',
161+
'function' => [
162+
'name' => 'get_schema',
163+
'arguments' => '{}',
164+
],
165+
]],
166+
]]);
167+
});
168+
142169
it('maps tool result messages', function (): void {
143170
$messageMap = new MessageMap(
144171
messages: [

tests/Providers/Mistral/MessageMapTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,33 @@
9999
]]);
100100
});
101101

102+
it('maps assistant message with tool calls with empty arguments as json object', function (): void {
103+
$messageMap = new MessageMap(
104+
messages: [
105+
new AssistantMessage('', [
106+
new ToolCall(
107+
'tool_1234',
108+
'get_schema',
109+
[]
110+
),
111+
]),
112+
],
113+
systemPrompts: []
114+
);
115+
116+
expect($messageMap())->toBe([[
117+
'role' => 'assistant',
118+
'tool_calls' => [[
119+
'id' => 'tool_1234',
120+
'type' => 'function',
121+
'function' => [
122+
'name' => 'get_schema',
123+
'arguments' => '{}',
124+
],
125+
]],
126+
]]);
127+
});
128+
102129
it('maps tool result messages', function (): void {
103130
$messageMap = new MessageMap(
104131
messages: [

tests/Providers/OpenAI/MessageMapTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@
189189
]);
190190
});
191191

192+
it('maps assistant message with tool calls with empty arguments as json object', function (): void {
193+
$messageMap = new MessageMap(
194+
messages: [
195+
new AssistantMessage('', [
196+
new ToolCall(
197+
'tool_1234',
198+
'get_schema',
199+
[],
200+
'call_1234'
201+
),
202+
]),
203+
],
204+
systemPrompts: []
205+
);
206+
207+
expect($messageMap())->toBe([
208+
[
209+
'id' => 'tool_1234',
210+
'call_id' => 'call_1234',
211+
'type' => 'function_call',
212+
'name' => 'get_schema',
213+
'arguments' => '{}',
214+
],
215+
]);
216+
});
217+
192218
it('maps tool result messages', function (): void {
193219
$messageMap = new MessageMap(
194220
messages: [

0 commit comments

Comments
 (0)