Skip to content

Commit 8cca69c

Browse files
wojt-janowskiclaude
andcommitted
Fix ToolChoiceMap producing invalid payloads for Anthropic and Converse schemas
Anthropic schema returned bare strings ('auto'/'any') instead of objects ({'type': 'auto'}). Converse schema nested auto/any under a 'tool' wrapper and used empty arrays instead of stdClass for JSON object serialization. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6d0b9f5 commit 8cca69c

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

src/Schemas/Anthropic/Maps/ToolChoiceMap.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
class ToolChoiceMap
1111
{
1212
/**
13-
* @return array<string, mixed>|string|null
13+
* @return array<string, mixed>|null
1414
*/
15-
public static function map(string|ToolChoice|null $toolChoice): string|array|null
15+
public static function map(string|ToolChoice|null $toolChoice): ?array
1616
{
1717
if (is_null($toolChoice)) {
1818
return null;
@@ -30,8 +30,8 @@ public static function map(string|ToolChoice|null $toolChoice): string|array|nul
3030
}
3131

3232
return match ($toolChoice) {
33-
ToolChoice::Auto => 'auto',
34-
ToolChoice::Any => 'any',
33+
ToolChoice::Auto => ['type' => 'auto'],
34+
ToolChoice::Any => ['type' => 'any'],
3535
};
3636

3737
}

src/Schemas/Converse/Maps/ToolChoiceMap.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public static function map(string|ToolChoice|null $toolChoice): string|array|nul
3131
}
3232

3333
return [
34-
'tool' => [
35-
($toolChoice === ToolChoice::Auto ? 'auto' : 'any') => [],
36-
],
34+
($toolChoice === ToolChoice::Auto ? 'auto' : 'any') => new \stdClass,
3735
];
3836
}
3937
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Schemas\Anthropic\Maps;
6+
7+
use InvalidArgumentException;
8+
use Prism\Bedrock\Schemas\Anthropic\Maps\ToolChoiceMap;
9+
use Prism\Prism\Enums\ToolChoice;
10+
11+
it('returns null when tool choice is null', function (): void {
12+
expect(ToolChoiceMap::map(null))->toBeNull();
13+
});
14+
15+
it('maps a specific tool correctly', function (): void {
16+
expect(ToolChoiceMap::map('search'))
17+
->toBe([
18+
'type' => 'tool',
19+
'name' => 'search',
20+
]);
21+
});
22+
23+
it('maps auto tool correctly', function (): void {
24+
expect(ToolChoiceMap::map(ToolChoice::Auto))
25+
->toBe(['type' => 'auto']);
26+
});
27+
28+
it('maps any tool correctly', function (): void {
29+
expect(ToolChoiceMap::map(ToolChoice::Any))
30+
->toBe(['type' => 'any']);
31+
});
32+
33+
it('throws exception for invalid tool choice', function (): void {
34+
ToolChoiceMap::map(ToolChoice::None);
35+
})->throws(InvalidArgumentException::class, 'Invalid tool choice');

tests/Schemas/Converse/Maps/ToolChoiceMapTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818

1919
it('maps any tool correctly', function (): void {
2020
expect(ToolChoiceMap::map(ToolChoice::Any))
21-
->toBe([
22-
'tool' => [
23-
'any' => [],
24-
],
21+
->toEqual([
22+
'any' => new \stdClass,
2523
]);
2624
});
2725

2826
it('maps auto tool correctly', function (): void {
2927
expect(ToolChoiceMap::map(ToolChoice::Auto))
30-
->toBe([
31-
'tool' => [
32-
'auto' => [],
33-
],
28+
->toEqual([
29+
'auto' => new \stdClass,
3430
]);
3531
});

0 commit comments

Comments
 (0)