@@ -17,15 +17,15 @@ final class Candidate implements Arrayable
17
17
{
18
18
/**
19
19
* @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.
21
21
* @param array<SafetyRating> $safetyRatings List of ratings for the safety of a response candidate. There is at most one rating per category.
22
22
* @param CitationMetadata $citationMetadata Output only. Citation information for model-generated candidate.
23
23
* @param int|null $tokenCount Output only. Token count for this candidate.
24
24
* @param int|null $index Output only. Index of the candidate in the list of candidates.
25
25
*/
26
26
public function __construct (
27
27
public readonly Content $ content ,
28
- public readonly FinishReason $ finishReason ,
28
+ public readonly ? FinishReason $ finishReason ,
29
29
public readonly array $ safetyRatings ,
30
30
public readonly CitationMetadata $ citationMetadata ,
31
31
public readonly ?int $ index ,
@@ -34,7 +34,7 @@ public function __construct(
34
34
) {}
35
35
36
36
/**
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
38
38
*/
39
39
public static function from (array $ attributes ): self
40
40
{
@@ -56,9 +56,14 @@ public static function from(array $attributes): self
56
56
default => new Content (parts: [], role: Role::MODEL ),
57
57
};
58
58
59
+ $ finishReason = match (true ) {
60
+ isset ($ attributes ['finishReason ' ]) => FinishReason::from ($ attributes ['finishReason ' ]),
61
+ default => null ,
62
+ };
63
+
59
64
return new self (
60
65
content: $ content ,
61
- finishReason: FinishReason:: from ( $ attributes [ ' finishReason ' ]) ,
66
+ finishReason: $ finishReason ,
62
67
safetyRatings: $ safetyRatings ,
63
68
citationMetadata: $ citationMetadata ,
64
69
index: $ attributes ['index ' ] ?? null ,
@@ -71,7 +76,7 @@ public function toArray(): array
71
76
{
72
77
return [
73
78
'content ' => $ this ->content ->toArray (),
74
- 'finishReason ' => $ this ->finishReason ->value ,
79
+ 'finishReason ' => $ this ->finishReason ? ->value,
75
80
'safetyRatings ' => array_map (
76
81
static fn (SafetyRating $ rating ): array => $ rating ->toArray (),
77
82
$ this ->safetyRatings
0 commit comments