Skip to content

[Ai] Add workaround for invalid SafetyRating from the backend. #6925

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

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -51,7 +51,7 @@ internal constructor(
val groundingMetadata: GroundingMetadata? = null,
) {
internal fun toPublic(): Candidate {
val safetyRatings = safetyRatings?.map { it.toPublic() }.orEmpty()
val safetyRatings = safetyRatings?.mapNotNull { it.toPublic() }.orEmpty()
val citations = citationMetadata?.toPublic()
val finishReason = finishReason?.toPublic()

Expand Down Expand Up @@ -120,23 +120,31 @@ internal constructor(
internal data class Internal
@JvmOverloads
constructor(
val category: HarmCategory.Internal,
val probability: HarmProbability.Internal,
val category: HarmCategory.Internal? = null,
val probability: HarmProbability.Internal? = null,
val blocked: Boolean? = null, // TODO(): any reason not to default to false?
val probabilityScore: Float? = null,
val severity: HarmSeverity.Internal? = null,
val severityScore: Float? = null,
) {

internal fun toPublic() =
SafetyRating(
category = category.toPublic(),
probability = probability.toPublic(),
probabilityScore = probabilityScore ?: 0f,
blocked = blocked,
severity = severity?.toPublic(),
severityScore = severityScore
)
// Due to a bug in the backend, it's possible that we receive
// an invalid `SafetyRating` value, without either category or
// probability. We return null in those cases to enable
// filtering by the higher level types.
if (category == null || probability == null) {
null
} else {
SafetyRating(
category = category.toPublic(),
probability = probability.toPublic(),
probabilityScore = probabilityScore ?: 0f,
blocked = blocked,
severity = severity?.toPublic(),
severityScore = severityScore
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class PromptFeedback(
) {

internal fun toPublic(): PromptFeedback {
val safetyRatings = safetyRatings?.map { it.toPublic() }.orEmpty()
val safetyRatings = safetyRatings?.mapNotNull { it.toPublic() }.orEmpty()
return PromptFeedback(blockReason?.toPublic(), safetyRatings, blockReasonMessage)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.withTimeout
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class VertexAIStreamingSnapshotTests {
private val testTimeout = 5.seconds

Expand Down Expand Up @@ -85,6 +88,18 @@ internal class VertexAIStreamingSnapshotTests {
}
}

@Test
fun `invalid safety ratings during image generation`() =
goldenVertexStreamingFile("streaming-success-image-invalid-safety-ratings.txt") {
val responses = model.generateContentStream("prompt")

withTimeout(testTimeout) {
val responseList = responses.toList()

responseList.isEmpty() shouldBe false
}
}

@Test
fun `unknown enum in finish reason`() =
goldenVertexStreamingFile("streaming-failure-unknown-finish-enum.txt") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.json.JSONArray
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(PublicPreviewAPI::class)
@RunWith(RobolectricTestRunner::class)
internal class VertexAIUnarySnapshotTests {
private val testTimeout = 5.seconds

Expand Down Expand Up @@ -125,6 +128,16 @@ internal class VertexAIUnarySnapshotTests {
}
}

@Test
fun `invalid safety ratings during image generation`() =
goldenVertexUnaryFile("unary-success-image-invalid-safety-ratings.json") {
withTimeout(testTimeout) {
val response = model.generateContent("prompt")

response.candidates.isEmpty() shouldBe false
}
}

@Test
fun `unknown enum in finish reason`() =
goldenVertexUnaryFile("unary-failure-unknown-enum-finish-reason.json") {
Expand Down
Loading