From 36385d27b0ba8552d18f77b10c046d6678bb800c Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Tue, 28 Jan 2025 18:27:37 -0500 Subject: [PATCH] Drop empty text parts when parsing the model's response 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. --- .../google/firebase/vertexai/internal/util/conversions.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index f8388054260..ed0efc394c0 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -219,8 +219,12 @@ internal fun com.google.firebase.vertexai.common.server.Candidate.toPublic(): Ca internal fun com.google.firebase.vertexai.common.UsageMetadata.toPublic(): UsageMetadata = UsageMetadata(promptTokenCount ?: 0, candidatesTokenCount ?: 0, totalTokenCount ?: 0) -internal fun com.google.firebase.vertexai.common.shared.Content.toPublic(): Content = - Content(role, parts.map { it.toPublic() }) +internal fun com.google.firebase.vertexai.common.shared.Content.toPublic(): Content { + val returnedParts = parts.map { it.toPublic() }.filterNot { it is TextPart && it.text.isEmpty() } + // If all returned parts were text and empty, we coalesce them into a single one-character string + // part so the backend doesn't fail if we send this back as part of a multi-turn interaction. + return Content(role, returnedParts.ifEmpty { listOf(TextPart(" ")) }) +} internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part { return when (this) {