Skip to content

fix(ai): fix serialization for Content with no parts field #6964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ constructor(public val role: String? = "user", public val parts: List<Part>) {
@Serializable
internal data class Internal(
@EncodeDefault val role: String? = "user",
val parts: List<InternalPart>
@EncodeDefault val parts: List<InternalPart> = emptyList()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here noting why we need the encodeDefault in this case, as it's not immediately apparent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually can't remember why I added this here 😳 the tests still pass even if we don't use encodeDefault.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devs can send a request with an empty list, so makes sense that @EncodeDefault that is used for encoding, not decoding, isn't 100% necessary. Should be safe to remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. Don't forget to add an entry to the changelog

) {
internal fun toPublic(): Content {
val returnedParts =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ internal class DevAPIStreamingSnapshotTests {
}
}

@Test
fun `streaming returned the last Content without parts`() =
goldenDevAPIStreamingFile("streaming-success-no-content-parts.txt") {
val responses = model.generateContentStream("prompt")

withTimeout(testTimeout) {
val responseList = responses.toList()
responseList.isEmpty() shouldBe false
responseList.last().candidates.first().apply {
finishReason shouldBe FinishReason.STOP
content.parts.isEmpty() shouldBe false
}
}
}

@Test
fun `stopped for recitation`() =
goldenDevAPIStreamingFile("streaming-failure-recitation-no-content.txt") {
Expand Down
Loading