Skip to content

Commit 65b23aa

Browse files
authored
Drop empty text parts when parsing the model's response (#6652)
Empty text parts go from a nuance when processed, to an exception when send to the backend. To prevent this issue, we are dropping them when parsing the response from the server.
1 parent b17d041 commit 65b23aa

File tree

1 file changed

+6
-2
lines changed
  • firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util

1 file changed

+6
-2
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,12 @@ internal fun com.google.firebase.vertexai.common.server.Candidate.toPublic(): Ca
219219
internal fun com.google.firebase.vertexai.common.UsageMetadata.toPublic(): UsageMetadata =
220220
UsageMetadata(promptTokenCount ?: 0, candidatesTokenCount ?: 0, totalTokenCount ?: 0)
221221

222-
internal fun com.google.firebase.vertexai.common.shared.Content.toPublic(): Content =
223-
Content(role, parts.map { it.toPublic() })
222+
internal fun com.google.firebase.vertexai.common.shared.Content.toPublic(): Content {
223+
val returnedParts = parts.map { it.toPublic() }.filterNot { it is TextPart && it.text.isEmpty() }
224+
// If all returned parts were text and empty, we coalesce them into a single one-character string
225+
// part so the backend doesn't fail if we send this back as part of a multi-turn interaction.
226+
return Content(role, returnedParts.ifEmpty { listOf(TextPart(" ")) })
227+
}
224228

225229
internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
226230
return when (this) {

0 commit comments

Comments
 (0)