@@ -39,7 +39,9 @@ public class GenerateContentResponse(
39
39
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
40
40
*/
41
41
public val text: String? by lazy {
42
- candidates.first().nonThoughtParts().filterIsInstance<TextPart >().joinToString(" " ) { it.text }
42
+ candidates.firstOrNull()?.nonThoughtParts()?.filterIsInstance<TextPart >()?.joinToString(" " ) {
43
+ it.text
44
+ }
43
45
}
44
46
45
47
/* *
@@ -49,7 +51,7 @@ public class GenerateContentResponse(
49
51
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
50
52
*/
51
53
public val functionCalls: List <FunctionCallPart > by lazy {
52
- candidates.first() .nonThoughtParts().filterIsInstance<FunctionCallPart >()
54
+ candidates.firstOrNull()? .nonThoughtParts()? .filterIsInstance<FunctionCallPart >().orEmpty ()
53
55
}
54
56
55
57
/* *
@@ -59,7 +61,7 @@ public class GenerateContentResponse(
59
61
* Learn more about [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
60
62
*/
61
63
public val thoughtSummary: String? by lazy {
62
- candidates.first() .thoughtParts().filterIsInstance<TextPart >().joinToString(" " )
64
+ candidates.firstOrNull()? .thoughtParts()? .filterIsInstance<TextPart >()? .joinToString(" " )
63
65
}
64
66
65
67
/* *
@@ -71,10 +73,14 @@ public class GenerateContentResponse(
71
73
* [thinking](https://firebase.google.com/docs/ai-logic/thinking?api=dev).
72
74
*/
73
75
public val inlineDataParts: List <InlineDataPart > by lazy {
74
- candidates.first().nonThoughtParts().let { parts ->
75
- parts.filterIsInstance<ImagePart >().map { it.toInlineDataPart() } +
76
- parts.filterIsInstance<InlineDataPart >()
77
- }
76
+ candidates
77
+ .firstOrNull()
78
+ ?.nonThoughtParts()
79
+ ?.let { parts ->
80
+ parts.filterIsInstance<ImagePart >().map { it.toInlineDataPart() } +
81
+ parts.filterIsInstance<InlineDataPart >()
82
+ }
83
+ .orEmpty()
78
84
}
79
85
80
86
private fun Candidate.thoughtParts (): List <Part > = content.parts.filter { it.isThought }
0 commit comments