Skip to content

Commit 3485f6d

Browse files
authored
Fix(anthripiuc): re-instate structured request exceptions (#433)
1 parent 4ac4921 commit 3485f6d

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/Providers/Anthropic/Handlers/Structured.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use InvalidArgumentException;
1010
use Prism\Prism\Contracts\PrismRequest;
11+
use Prism\Prism\Exceptions\PrismException;
1112
use Prism\Prism\Providers\Anthropic\Maps\FinishReasonMap;
1213
use Prism\Prism\Providers\Anthropic\Maps\MessageMap;
1314
use Prism\Prism\Structured\Request as StructuredRequest;
@@ -105,7 +106,7 @@ public static function buildHttpRequestPayload(PrismRequest $request): array
105106

106107
// Validate options
107108
if ($request->providerOptions('citations') === true && $request->providerOptions('use_tool_calling') === true) {
108-
throw new InvalidArgumentException(
109+
throw new PrismException(
109110
'Citations are not supported with tool calling mode. '.
110111
'Please set use_tool_calling to false in provider options to use citations.'
111112
);
@@ -300,7 +301,7 @@ protected function shouldUseToolCalling(): bool
300301
protected function validateProviderOptions(): void
301302
{
302303
if ($this->request->providerOptions('citations') === true && $this->shouldUseToolCalling()) {
303-
throw new InvalidArgumentException(
304+
throw new PrismException(
304305
'Citations are not supported with tool calling mode. '.
305306
'Please set use_tool_calling to false in provider options to use citations.'
306307
);

src/Structured/PendingRequest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Prism\Prism\Concerns\HasSchema;
1515
use Prism\Prism\Exceptions\PrismException;
1616
use Prism\Prism\ValueObjects\Messages\UserMessage;
17+
use Throwable;
1718

1819
class PendingRequest
1920
{
@@ -36,7 +37,13 @@ public function generate(): Response
3637

3738
public function asStructured(): Response
3839
{
39-
return $this->provider->structured($this->toRequest());
40+
$request = $this->toRequest();
41+
42+
try {
43+
return $this->provider->structured($request);
44+
} catch (Throwable $e) {
45+
$this->provider->handleRequestExceptions($request->model(), $e);
46+
}
4047
}
4148

4249
public function toRequest(): Request

tests/Providers/Anthropic/StructuredTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Tests\Providers\Anthropic;
66

77
use Illuminate\Support\Carbon;
8-
use InvalidArgumentException;
98
use Prism\Prism\Enums\Provider;
9+
use Prism\Prism\Exceptions\PrismException;
1010
use Prism\Prism\Prism;
1111
use Prism\Prism\Providers\Anthropic\Handlers\Structured;
1212
use Prism\Prism\Providers\Anthropic\ValueObjects\MessagePartWithCitations;
@@ -220,7 +220,7 @@
220220
->withPrompt('What is the answer?')
221221
->withProviderOptions(['citations' => true, 'use_tool_calling' => true])
222222
->asStructured()
223-
)->toThrow(InvalidArgumentException::class, 'Citations are not supported with tool calling mode');
223+
)->toThrow(PrismException::class, 'Citations are not supported with tool calling mode');
224224
});
225225

226226
it('returns structured output with default JSON mode', function (): void {

0 commit comments

Comments
 (0)