Skip to content

Commit 93ebb89

Browse files
committed
Make PromptFeedback and SafetyRatings optional
1 parent de7cf55 commit 93ebb89

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/Resources/PromptFeedback.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
/**
2727
* @param array{
2828
* blockReason: string|null,
29-
* safetyRatings: array<int, array{category: string, probability: string, blocked?: bool|null}>
29+
* safetyRatings?: array<int, array{category: string, probability: string, blocked?: bool|null}>
3030
* } $array
3131
* @return self
3232
*/
@@ -35,7 +35,7 @@ public static function fromArray(array $array): self
3535
$blockReason = BlockReason::tryFrom($array['blockReason'] ?? '');
3636
$safetyRatings = array_map(
3737
static fn (array $rating): SafetyRating => SafetyRating::fromArray($rating),
38-
$array['safetyRatings'],
38+
$array['safetyRatings'] ?? [],
3939
);
4040

4141
return new self($blockReason, $safetyRatings);

src/Responses/GenerateContentResponse.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use GeminiAPI\Resources\Parts\PartInterface;
1010
use GeminiAPI\Resources\Parts\TextPart;
1111
use GeminiAPI\Resources\PromptFeedback;
12-
use InvalidArgumentException;
1312
use ValueError;
1413

1514
class GenerateContentResponse
@@ -18,11 +17,11 @@ class GenerateContentResponse
1817

1918
/**
2019
* @param Candidate[] $candidates
21-
* @param PromptFeedback $promptFeedback
20+
* @param ?PromptFeedback $promptFeedback
2221
*/
2322
public function __construct(
2423
public readonly array $candidates,
25-
public readonly PromptFeedback $promptFeedback,
24+
public readonly ?PromptFeedback $promptFeedback = null,
2625
) {
2726
$this->ensureArrayOfType($candidates, Candidate::class);
2827
}
@@ -72,7 +71,7 @@ public function text(): string
7271
* @param array{
7372
* promptFeedback: array{
7473
* blockReason: string|null,
75-
* safetyRatings: array<int, array{category: string, probability: string, blocked: bool|null}>,
74+
* safetyRatings?: array<int, array{category: string, probability: string, blocked: bool|null}>,
7675
* },
7776
* candidates: array<int, array{
7877
* citationMetadata: array{citationSources: array<int, array{startIndex?: int|null, endIndex?: int|null, uri?: string|null, license?: string|null}>},
@@ -87,17 +86,16 @@ public function text(): string
8786
*/
8887
public static function fromArray(array $array): self
8988
{
90-
if (empty($array['promptFeedback']) || !is_array($array['promptFeedback'])) {
91-
throw new InvalidArgumentException('invalid promptFeedback');
89+
$promptFeedback = null;
90+
if (!empty($array['promptFeedback'])) {
91+
$promptFeedback = PromptFeedback::fromArray($array['promptFeedback']);
9292
}
9393

9494
$candidates = array_map(
9595
static fn (array $candidate): Candidate => Candidate::fromArray($candidate),
9696
$array['candidates'] ?? [],
9797
);
9898

99-
$promptFeedback = PromptFeedback::fromArray($array['promptFeedback']);
100-
10199
return new self($candidates, $promptFeedback);
102100
}
103101
}

0 commit comments

Comments
 (0)