Skip to content

Commit dd5c470

Browse files
Add tests and improve handling for tool calls and parameterless tools in schemas
- Added test cases for tool calls with various inputs in both Anthropics and Converses. - Improved handling of empty tool call arguments by converting them to stdClass objects. - Updated `ToolMap` and `MessageMap` to standardize the mapping of parameterless tools. - Introduced `providerToolCalls` as an empty array in text handlers for consistency.
1 parent 2f48846 commit dd5c470

23 files changed

+538
-59
lines changed

src/Bedrock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function schema(PrismRequest $request): BedrockSchema
9999
{
100100
$override = $request->providerOptions();
101101

102-
$override = data_get($override, 'apiSchema', null);
102+
$override = data_get($override, 'apiSchema');
103103

104104
return $override ?? BedrockSchema::fromModelString($request->model());
105105
}

src/BedrockServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function getCredentials(array $config): Credentials
3535
protected function registerWithPrism(): void
3636
{
3737
$this->app->extend(PrismManager::class, function (PrismManager $prismManager): \Prism\Prism\PrismManager {
38-
$prismManager->extend(Bedrock::KEY, fn ($app, $config): Bedrock => new Bedrock(
38+
$prismManager->extend(Bedrock::KEY, fn ($app, array $config): Bedrock => new Bedrock(
3939
credentials: BedrockServiceProvider::getCredentials($config),
4040
region: $config['region']
4141
));

src/Schemas/Anthropic/AnthropicStructuredHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ protected function prepareTempResponse(): void
105105
usage: new Usage(
106106
promptTokens: data_get($data, 'usage.input_tokens'),
107107
completionTokens: data_get($data, 'usage.output_tokens'),
108-
cacheWriteInputTokens: data_get($data, 'usage.cache_creation_input_tokens', null),
109-
cacheReadInputTokens: data_get($data, 'usage.cache_read_input_tokens', null)
108+
cacheWriteInputTokens: data_get($data, 'usage.cache_creation_input_tokens'),
109+
cacheReadInputTokens: data_get($data, 'usage.cache_read_input_tokens')
110110
),
111111
meta: new Meta(
112112
id: data_get($data, 'id'),

src/Schemas/Anthropic/AnthropicTextHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ protected function addStep(Request $request, array $toolResults = []): void
155155
finishReason: $this->tempResponse->finishReason,
156156
toolCalls: $this->tempResponse->toolCalls,
157157
toolResults: $toolResults,
158+
providerToolCalls: [],
158159
usage: $this->tempResponse->usage,
159160
meta: $this->tempResponse->meta,
160161
messages: $request->messages(),

src/Schemas/Anthropic/Concerns/ExtractsToolCalls.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ protected function extractToolCalls(array $data): array
1414
{
1515
$toolCalls = array_map(function ($content): ?ToolCall {
1616
if (data_get($content, 'type') === 'tool_use') {
17+
$input = data_get($content, 'input');
18+
1719
return new ToolCall(
1820
id: data_get($content, 'id'),
1921
name: data_get($content, 'name'),
20-
arguments: data_get($content, 'input')
22+
arguments: is_string($input) ? (json_decode($input, true) ?? []) : ($input ?? [])
2123
);
2224
}
2325

src/Schemas/Anthropic/Maps/MessageMap.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function map(array $messages): array
2828
}
2929

3030
return array_map(
31-
fn (Message $message): array => self::mapMessage($message),
31+
self::mapMessage(...),
3232
$messages
3333
);
3434
}
@@ -40,7 +40,7 @@ public static function map(array $messages): array
4040
public static function mapSystemMessages(array $messages): array
4141
{
4242
return array_map(
43-
fn (Message $message): array => self::mapSystemMessage($message),
43+
self::mapSystemMessage(...),
4444
$messages
4545
);
4646
}
@@ -65,7 +65,7 @@ protected static function mapSystemMessage(SystemMessage $systemMessage): array
6565
{
6666
$providerOptions = $systemMessage->providerOptions();
6767

68-
$cacheType = data_get($providerOptions, 'cacheType', null);
68+
$cacheType = data_get($providerOptions, 'cacheType');
6969

7070
return array_filter([
7171
'type' => 'text',
@@ -96,7 +96,7 @@ protected static function mapUserMessage(UserMessage $message): array
9696
{
9797
$providerOptions = $message->providerOptions();
9898

99-
$cacheType = data_get($providerOptions, 'cacheType', null);
99+
$cacheType = data_get($providerOptions, 'cacheType');
100100
$cache_control = $cacheType ? ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType] : null;
101101

102102
if ($message->documents() !== []) {
@@ -123,7 +123,7 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
123123
{
124124
$providerOptions = $message->providerOptions();
125125

126-
$cacheType = data_get($providerOptions, 'cacheType', null);
126+
$cacheType = data_get($providerOptions, 'cacheType');
127127

128128
$content = [];
129129

@@ -150,7 +150,7 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
150150
'type' => 'tool_use',
151151
'id' => $toolCall->id,
152152
'name' => $toolCall->name,
153-
'input' => $toolCall->arguments(),
153+
'input' => $toolCall->arguments() === [] ? new \stdClass : $toolCall->arguments(),
154154
], $message->toolCalls)
155155
: [];
156156

src/Schemas/Anthropic/Maps/ToolMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class ToolMap
1616
public static function map(array $tools): array
1717
{
1818
return array_map(function (PrismTool $tool): array {
19-
$cacheType = data_get($tool->providerOptions(), 'cacheType', null);
19+
$cacheType = data_get($tool->providerOptions(), 'cacheType');
2020

2121
return array_filter([
2222
'name' => $tool->name(),
2323
'description' => $tool->description(),
2424
'input_schema' => [
2525
'type' => 'object',
26-
'properties' => $tool->parametersAsArray(),
26+
'properties' => $tool->parametersAsArray() ?: (object) [],
2727
'required' => $tool->requiredParameters(),
2828
],
2929
'cache_control' => $cacheType

src/Schemas/Cohere/CohereEmbeddingsHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function buildResponse(): EmbeddingsResponse
6565
$body = $this->httpResponse->json();
6666

6767
return new EmbeddingsResponse(
68-
embeddings: array_map(fn (array $item): Embedding => Embedding::fromArray($item), data_get($body, 'embeddings', [])),
68+
embeddings: array_map(Embedding::fromArray(...), data_get($body, 'embeddings', [])),
6969
usage: new EmbeddingsUsage(
7070
tokens: (int) $this->httpResponse->header('X-Amzn-Bedrock-Input-Token-Count')
7171
),

src/Schemas/Converse/Concerns/ExtractsToolCalls.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ protected function extractToolCalls(array $data): array
1818
return;
1919
}
2020

21+
$input = data_get($use, 'input');
22+
2123
return new ToolCall(
2224
id: data_get($use, 'toolUseId'),
2325
name: data_get($use, 'name'),
24-
arguments: data_get($use, 'input')
26+
arguments: is_string($input) ? (json_decode($input, true) ?? []) : ($input ?? [])
2527
);
2628

2729
}, data_get($data, 'output.message.content', []));

src/Schemas/Converse/ConverseTextHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ protected function addStep(Request $request, array $toolResults = []): void
161161
finishReason: $this->tempResponse->finishReason,
162162
toolCalls: $this->tempResponse->toolCalls,
163163
toolResults: $toolResults,
164+
providerToolCalls: [],
164165
usage: $this->tempResponse->usage,
165166
meta: $this->tempResponse->meta,
166167
messages: $request->messages(),

0 commit comments

Comments
 (0)