Skip to content

Commit 84e067e

Browse files
committed
test(ai): add test for when the last Content has no parts
1 parent fff619e commit 84e067e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Content.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,9 @@ constructor(public val role: String? = "user", public val parts: List<Part>) {
9090
@Serializable
9191
internal data class Internal(
9292
@EncodeDefault val role: String? = "user",
93-
@EncodeDefault val parts: List<InternalPart>? = emptyList()
93+
@EncodeDefault val parts: List<InternalPart> = emptyList()
9494
) {
95-
// TODO: add unit tests before sending a pull request (see DevAPIStreamingSnapshotTests.kt)
9695
internal fun toPublic(): Content {
97-
if (parts.isNullOrEmpty()) {
98-
return Content(role, listOf(TextPart(" ")))
99-
}
10096
val returnedParts =
10197
parts.map { it.toPublic() }.filterNot { it is TextPart && it.text.isEmpty() }
10298
// If all returned parts were text and empty, we coalesce them into a single one-character

firebase-ai/src/test/java/com/google/firebase/ai/DevAPIStreamingSnapshotTests.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ internal class DevAPIStreamingSnapshotTests {
8888
}
8989
}
9090

91+
@Test
92+
fun `streaming returned the last Content without parts`() =
93+
goldenDevAPIStreamingFile("streaming-success-no-content-parts.txt") {
94+
val responses = model.generateContentStream("prompt")
95+
96+
withTimeout(testTimeout) {
97+
val responseList = responses.toList()
98+
responseList.isEmpty() shouldBe false
99+
responseList.last().candidates.first().apply {
100+
finishReason shouldBe FinishReason.STOP
101+
content.parts.isEmpty() shouldBe false
102+
}
103+
}
104+
}
105+
91106
@Test
92107
fun `stopped for recitation`() =
93108
goldenDevAPIStreamingFile("streaming-failure-recitation-no-content.txt") {

0 commit comments

Comments
 (0)