live.connect() (Vertex AI) resolves ADC and refreshes the OAuth token synchronously on the event loop on every connect
Maintainer thường phản hồi trong vòng 1 ngày
@Venkaiahbabuneelam đang làm issue này rồi.
Từ ngày 6/10/2026.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 74/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- google-cloud, python
- Lĩnh vực
- authentication, backend-api-design, performance
Hướng nghiên cứu
Start in google/genai/live.py at AsyncLive.connect() and compare its Vertex AI authentication path with _api_client._async_access_token() and async_get_token_from_credentials(). Check the existing Live and auth tests before making a change. Done means ADC is cached per client, refresh work happens off the event loop, and the reported connect-time stalls and repeated refreshes are covered by tests.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Environment
- google-genai 2.24.0, google-auth 2.58.0, Python 3.12
- Vertex AI backend (
GOOGLE_GENAI_USE_VERTEXAI=true), service-account ADC - Live API with many concurrent sessions per process (telephony voice agent)
What happens
On Vertex, AsyncLive.connect() (google/genai/live.py, ~L1055-1069) runs this before it opens the websocket:
if not self._api_client._credentials:
creds, _ = google.auth.default(scopes=[...]) # sync, on the event loop
else:
creds = self._api_client._credentials
if not (creds.token and creds.valid):
auth_req = _auth_requests().Request()
creds.refresh(auth_req) # sync HTTP, on the event loop
Two problems:
- Blocking I/O inside an async API.
creds.refresh()makes a synchronousrequestscall to the token endpoint, on the event loop. Every other coroutine in the process stalls until it returns. For us that is live audio for every other call on the pod. - No caching when the client was built without
credentials=. The resolved credentials are a local variable and are never stored on_api_client. Everyconnect()therefore runsgoogle.auth.default()again and refreshes a fresh, token-less credentials object. The sync refresh runs on every connect, not once an hour.
The async REST path already handles both. _api_client._async_access_token() loads auth via asyncio.to_thread(load_auth, ...) under an async lock and caches it in self._credentials. async_get_token_from_credentials() refreshes via asyncio.to_thread(refresh_auth, ...), only when expired.
Repro
import asyncio, time
from google import genai
async def ticker(stop, worst):
while not stop.is_set():
t = time.perf_counter(); await asyncio.sleep(0.01)
worst[0] = max(worst[0], (time.perf_counter() - t - 0.01) * 1000)
async def main():
client = genai.Client() # Vertex via env, no credentials= passed
for i in range(3):
stop, worst = asyncio.Event(), [0.0]
tk = asyncio.create_task(ticker(stop, worst))
await asyncio.sleep(0.05) # let the ticker start before connect() blocks
async with client.aio.live.connect(
model="gemini-live-2.5-flash-native-audio",
config={"response_modalities": ["AUDIO"]},
):
pass
stop.set(); await tk
print(f"connect #{i+1}: max event-loop stall {worst[0]:.0f} ms")
await client.aio.aclose()
asyncio.run(main())
Output (Vertex, us-central1). I also counted service_account.Credentials.refresh calls:
connect #1: max event-loop stall 517 ms, token refreshes during connect: 1
connect #2: max event-loop stall 410 ms, token refreshes during connect: 1
connect #3: max event-loop stall 415 ms, token refreshes during connect: 1
Expected: auth resolved once per client, refresh only near expiry, and never on the event loop. Stalls should be a few ms.
Suggested fix
In AsyncLive.connect(), reuse the async REST path: await self._api_client._async_access_token(). That loads ADC off-loop, caches it on the client, and refreshes off-loop under the async auth lock. At minimum, store the resolved credentials on _api_client and wrap google.auth.default() and creds.refresh() in asyncio.to_thread.
Workaround
We build one genai.Client(credentials=creds) per process, in a thread. We keep creds FRESH by calling creds.refresh() via asyncio.to_thread whenever creds.token_state is not TokenState.FRESH. valid turns False at the same REFRESH_THRESHOLD, so connect() never takes the sync branch. With the same ticker, max stall per connect drops to 9–28 ms.
- Ngôn ngữ chính
- Python
- Star
- 4k
- Fork
- 1k
- Merge trung bình
- 1 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 54
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của googleapis/python-genai
-
Curated history keeps half a user turn when the model turn is invalidCó thể đã có người làm @Venkaiahbabuneelam đã nhận 2 ngày trước. Đang mởpriority: p2 status:awaiting user response type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
googleapis/python-genai#3051 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Unsubscripted typing.List and typing.Dict crash convert_if_exist_pydantic_model in AFCCó thể đã có người làm @chauvuusvn đã nhận 4 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
googleapis/python-genai#3044 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Name collision in google.genai.interactions: triggers.Interaction shadows response model Interaction in static type checkersCó thể đã có người làm @Venkaiahbabuneelam đã nhận 9 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
googleapis/python-genai#3013 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Callable tools: optional scalar parameters (`int | None`) get `"type": "object"` in `parameters_json_schema`, so Gemini sends `{}`Có thể đã có người làm @Venkaiahbabuneelam đã nhận 1 ngày trước. Đang mởpriority: p2 type: bug
googleapis/python-genai#3060 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Gemini API latency has recently increased significantly, especially with store=trueCó thể đã có người làm @Venkaiahbabuneelam đã nhận 1 ngày trước. Đang mởpriority: p3 status:awaiting user response type: question
googleapis/python-genai#3059 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của googleapis/python-genai
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
Harmony OPeNDAP SubSetter (HOSS) Geographic LARC_CLOUD PREFIRE_SAT2_AUX-SAT R01 production
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
nasa/harmony-autotester#245 ·
-
[FEATURE] - Add UTVD supportĐang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Deltares/imod-python#1928 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100