Skip to content

Commit b59e3af

Browse files
authored
Merge pull request #46 from aydinfatih/main
refactor: Make the `$finishReason` parameter in Candidate nullable
2 parents a13b109 + 717f510 commit b59e3af

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/Data/Candidate.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ final class Candidate implements Arrayable
1717
{
1818
/**
1919
* @param Content $content Output only. Generated content returned from the model.
20-
* @param FinishReason $finishReason The reason why the model stopped generating tokens.If empty, the model has not stopped generating the tokens.
20+
* @param FinishReason|null $finishReason The reason why the model stopped generating tokens.If empty, the model has not stopped generating the tokens.
2121
* @param array<SafetyRating> $safetyRatings List of ratings for the safety of a response candidate. There is at most one rating per category.
2222
* @param CitationMetadata $citationMetadata Output only. Citation information for model-generated candidate.
2323
* @param int|null $tokenCount Output only. Token count for this candidate.
2424
* @param int|null $index Output only. Index of the candidate in the list of candidates.
2525
*/
2626
public function __construct(
2727
public readonly Content $content,
28-
public readonly FinishReason $finishReason,
28+
public readonly ?FinishReason $finishReason,
2929
public readonly array $safetyRatings,
3030
public readonly CitationMetadata $citationMetadata,
3131
public readonly ?int $index,
@@ -34,7 +34,7 @@ public function __construct(
3434
) {}
3535

3636
/**
37-
* @param array{ content: ?array{ parts: array{ array{ text: ?string, inlineData: array{ mimeType: string, data: string } } }, role: string }, finishReason: string, safetyRatings: ?array{ array{ category: string, probability: string, blocked: ?bool } }, citationMetadata: ?array{ citationSources: array{ array{ startIndex: int, endIndex: int, uri: string, license: string} } }, index: ?int, tokenCount: ?int, avgLogprobs: ?float } $attributes
37+
* @param array{ content: ?array{ parts: array{ array{ text: ?string, inlineData: array{ mimeType: string, data: string } } }, role: string }, finishReason: ?string, safetyRatings: ?array{ array{ category: string, probability: string, blocked: ?bool } }, citationMetadata: ?array{ citationSources: array{ array{ startIndex: int, endIndex: int, uri: string, license: string} } }, index: ?int, tokenCount: ?int, avgLogprobs: ?float } $attributes
3838
*/
3939
public static function from(array $attributes): self
4040
{
@@ -56,9 +56,14 @@ public static function from(array $attributes): self
5656
default => new Content(parts: [], role: Role::MODEL),
5757
};
5858

59+
$finishReason = match (true) {
60+
isset($attributes['finishReason']) => FinishReason::from($attributes['finishReason']),
61+
default => null,
62+
};
63+
5964
return new self(
6065
content: $content,
61-
finishReason: FinishReason::from($attributes['finishReason']),
66+
finishReason: $finishReason,
6267
safetyRatings: $safetyRatings,
6368
citationMetadata: $citationMetadata,
6469
index: $attributes['index'] ?? null,
@@ -71,7 +76,7 @@ public function toArray(): array
7176
{
7277
return [
7378
'content' => $this->content->toArray(),
74-
'finishReason' => $this->finishReason->value,
79+
'finishReason' => $this->finishReason?->value,
7580
'safetyRatings' => array_map(
7681
static fn (SafetyRating $rating): array => $rating->toArray(),
7782
$this->safetyRatings

0 commit comments

Comments
 (0)