Skip to content

Commit 3dc208c

Browse files
authored
VertexAI: convert exceptions from countTokens() to VertexAIException (#6105)
1 parent 8d5c103 commit 3dc208c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/GenerativeModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ internal constructor(
195195
* @return A [CountTokensResponse] containing the amount of tokens in the prompt.
196196
*/
197197
suspend fun countTokens(vararg prompt: Content): CountTokensResponse {
198-
return controller.countTokens(constructCountTokensRequest(*prompt)).toPublic()
198+
try {
199+
return controller.countTokens(constructCountTokensRequest(*prompt)).toPublic()
200+
} catch (e: Throwable) {
201+
throw FirebaseVertexAIException.from(e)
202+
}
199203
}
200204

201205
/**

firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,10 @@ internal class UnarySnapshotTests {
427427
response.totalBillableCharacters shouldBe 0
428428
}
429429
}
430+
431+
@Test
432+
fun `countTokens fails with model not found`() =
433+
goldenUnaryFile("failure-model-not-found.json", HttpStatusCode.NotFound) {
434+
withTimeout(testTimeout) { shouldThrow<ServerException> { model.countTokens("prompt") } }
435+
}
430436
}

0 commit comments

Comments
 (0)