Skip to content

Commit 9c396d5

Browse files
Fix 403 Error in LiveGenerativeModel due to missing Android Package headers
- Located the `getWebSocketSession` function in `APIController.kt`. - Modified the function to ensure `applyHeaderProvider()` is called within the webSocketSession configuration block. - Since `webSocketSession` block is not suspending, refactored header generation to `resolveHeaders` suspend function which is called before the block. - Added a regression test (verified and then deleted) to confirm headers are attached. - Verified that headers are correctly attached.
1 parent c8ada3c commit 9c396d5

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

firebase-ai/src/main/kotlin/com/google/firebase/ai/common/APIController.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,13 @@ internal constructor(
236236
"wss://firebasevertexai.googleapis.com/ws/google.firebase.vertexai.v1beta.GenerativeService/BidiGenerateContent?key=$key"
237237
}
238238

239-
suspend fun getWebSocketSession(location: String): DefaultClientWebSocketSession =
240-
client.webSocketSession(getBidiEndpoint(location)) { applyCommonHeaders() }
239+
suspend fun getWebSocketSession(location: String): DefaultClientWebSocketSession {
240+
val headers = resolveHeaders()
241+
return client.webSocketSession(getBidiEndpoint(location)) {
242+
applyCommonHeaders()
243+
headers.forEach { (key, value) -> header(key, value) }
244+
}
245+
}
241246

242247
fun generateContentStream(
243248
request: GenerateContentRequest
@@ -285,16 +290,17 @@ internal constructor(
285290
}
286291

287292
private suspend fun HttpRequestBuilder.applyHeaderProvider() {
288-
if (headerProvider != null) {
289-
try {
290-
withTimeout(headerProvider.timeout) {
291-
for ((tag, value) in headerProvider.generateHeaders()) {
292-
header(tag, value)
293-
}
294-
}
295-
} catch (e: TimeoutCancellationException) {
296-
Log.w(TAG, "HeaderProvided timed out without generating headers, ignoring")
297-
}
293+
val headers = resolveHeaders()
294+
headers.forEach { (tag, value) -> header(tag, value) }
295+
}
296+
297+
private suspend fun resolveHeaders(): Map<String, String> {
298+
if (headerProvider == null) return emptyMap()
299+
return try {
300+
withTimeout(headerProvider.timeout) { headerProvider.generateHeaders() }
301+
} catch (e: TimeoutCancellationException) {
302+
Log.w(TAG, "HeaderProvided timed out without generating headers, ignoring")
303+
emptyMap()
298304
}
299305
}
300306

0 commit comments

Comments
 (0)