Skip to content

Commit 436549f

Browse files
author
Mohamed Khaled
committed
Complete exception hierarchy with indexed field names and simplified fromMissingData method
1 parent 4cc060b commit 436549f

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/Providers/Http/Exception/ResponseException.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@ class ResponseException extends RuntimeException
2424
*
2525
* @param string $apiName The name of the API/provider.
2626
* @param string $fieldName The field that was expected but missing.
27-
* @param string $context Additional context about where the field was expected.
2827
* @return self
2928
*/
30-
public static function fromMissingData(string $apiName, string $fieldName, string $context = ''): self
29+
public static function fromMissingData(string $apiName, string $fieldName): self
3130
{
32-
$message = sprintf('Unexpected %s API response: Missing the "%s" key', $apiName, $fieldName);
33-
if ($context !== '') {
34-
$message .= ' in ' . $context;
35-
}
36-
$message .= '.';
31+
$message = sprintf('Unexpected %s API response: Missing the "%s" key.', $apiName, $fieldName);
3732

3833
return new self($message);
3934
}

src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleImageGenerationModel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,9 @@ protected function parseResponseChoiceToCandidate(
361361
} elseif (isset($choiceData['b64_json']) && is_string($choiceData['b64_json'])) {
362362
$imageFile = new File($choiceData['b64_json'], $expectedMimeType);
363363
} else {
364-
throw ResponseException::fromMissingData(
364+
throw ResponseException::fromInvalidData(
365365
$this->providerMetadata()->getName(),
366-
'url or b64_json',
367-
'choice data'
366+
'Each choice must contain either a url or b64_json key with a string value.'
368367
);
369368
}
370369

src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModel.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,15 @@ protected function parseResponseToGenerativeAiResult(Response $response): Genera
566566
}
567567

568568
$candidates = [];
569-
foreach ($responseData['choices'] as $choiceData) {
569+
foreach ($responseData['choices'] as $index => $choiceData) {
570570
if (!is_array($choiceData) || array_is_list($choiceData)) {
571571
throw ResponseException::fromInvalidData(
572572
$this->providerMetadata()->getName(),
573573
'Each element in the choices key must be an associative array.'
574574
);
575575
}
576576

577-
$candidates[] = $this->parseResponseChoiceToCandidate($choiceData);
577+
$candidates[] = $this->parseResponseChoiceToCandidate($choiceData, $index);
578578
}
579579

580580
$id = isset($responseData['id']) && is_string($responseData['id']) ? $responseData['id'] : '';
@@ -611,10 +611,11 @@ protected function parseResponseToGenerativeAiResult(Response $response): Genera
611611
* @since 0.1.0
612612
*
613613
* @param ChoiceData $choiceData The choice data from the API response.
614+
* @param int $index The index of the choice in the choices array.
614615
* @return Candidate The parsed candidate.
615616
* @throws RuntimeException If the choice data is invalid.
616617
*/
617-
protected function parseResponseChoiceToCandidate(array $choiceData): Candidate
618+
protected function parseResponseChoiceToCandidate(array $choiceData, int $index): Candidate
618619
{
619620
if (
620621
!isset($choiceData['message']) ||
@@ -623,16 +624,14 @@ protected function parseResponseChoiceToCandidate(array $choiceData): Candidate
623624
) {
624625
throw ResponseException::fromMissingData(
625626
$this->providerMetadata()->getName(),
626-
'message',
627-
'choice data'
627+
"choices[{$index}].message"
628628
);
629629
}
630630

631631
if (!isset($choiceData['finish_reason']) || !is_string($choiceData['finish_reason'])) {
632632
throw ResponseException::fromMissingData(
633633
$this->providerMetadata()->getName(),
634-
'finish_reason',
635-
'choice data'
634+
"choices[{$index}].finish_reason"
636635
);
637636
}
638637

0 commit comments

Comments
 (0)