Per-request Authorization and X-Server-Timeout leak into the client's default headers (stale token in Interactions and Live)
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
Start by reading BaseApiClient._build_request in _api_client.py and the header handling in _gaos/google_genai.py and live.py. Run the mentioned _build_request unit test and add or run regression coverage for sync and async models-to-Interactions and models-to-Live flows. Done means request-specific headers no longer mutate client defaults, refreshed tokens and per-request timeouts are used, and Live sets the quota project itself.
由索引模型根据 Issue 内容生成。
描述
Description
When a request is sent without per-request http_options, BaseApiClient._build_request reuses the client's own self._http_options.headers dict for the request. The request path then writes per-request values into it:
X-Server-Timeout, viapopulate_server_timeout_header(_api_client.py~1545);Authorizationandx-goog-user-project(~1564–1566 and ~1641–1645).
Those values become client defaults, and other code reads them as such:
- Interactions bridge: it snapshots the client's headers (
_gaos/google_genai.py~99–103), and_apply_default_headerscopies the staleAuthorizationonto every request._apply_auththen skips the fresh token because a header is already set. - Live:
live.connectonly setsAuthorizationif it's absent (live.py~1141). - Server timeout:
populate_server_timeout_headeronly setsX-Server-Timeoutif it's absent.
Impact
- Vertex AI with ADC or credentials: if any REST call (e.g.
models.generate_content) runs beforeclient.interactions(or the other Interactions-layer resources) is first used, those requests send the first bearer token for the life of the process. Once that token expires, after about an hour, they start failing with 401. Long-running servers hit this. - Live: it connects with the token from the last REST call, even after a refresh.
- Timeouts: with a client-level timeout, a later per-request
timeoutstill sends the earlierX-Server-Timeout, and the server enforces it. So the same call behaves differently depending on what ran before.
Repro (offline, mocked transport, fake credentials)
import httpx
import google.oauth2.credentials
from google import genai
from google.genai import types
GEN = {'candidates': [{'content': {'role': 'model', 'parts': [{'text': 'ok'}]}, 'finishReason': 'STOP'}]}
INTERACTION = {'id': 'abc', 'status': 'completed', 'outputs': [], 'object': 'interaction',
'created': '2026-01-01T00:00:00Z', 'updated': '2026-01-01T00:00:00Z'}
sent = []
def handler(request: httpx.Request) -> httpx.Response:
kind = 'interactions' if '/interactions/' in request.url.path else 'models'
sent.append((kind, request.headers.get('authorization')))
return httpx.Response(200, json=INTERACTION if kind == 'interactions' else GEN)
creds = google.oauth2.credentials.Credentials(token='token-1')
client = genai.Client(
vertexai=True, project='p', location='us-central1', credentials=creds,
http_options=types.HttpOptions(httpx_client=httpx.Client(transport=httpx.MockTransport(handler))),
)
client.models.generate_content(model='gemini-2.5-flash', contents='hi')
creds.token = 'token-2' # what google-auth does when it refreshes the token
client.interactions.get(id='abc')
client.models.generate_content(model='gemini-2.5-flash', contents='hi')
creds.token = 'token-3'
client.interactions.get(id='abc')
for kind, auth in sent:
print(f'{kind:13s} {auth}')
print('client default headers:', {k: v for k, v in client._api_client._http_options.headers.items() if k.lower() in ('authorization', 'x-server-timeout')})
Output on main (364889aa, google-genai 2.31.0):
models Bearer token-1
interactions Bearer token-1
models Bearer token-2
interactions Bearer token-1 <- credentials hold token-3
client default headers: {'Authorization': 'Bearer token-2'}
If interactions is used before any models call, it gets the fresh token each time, so this depends on call order.
Proposed fix
Copy the headers at the one place that builds per-request headers, so per-request values stay on the HttpRequest:
@@ class BaseApiClient: def _build_request(
if patched_http_options.headers is None:
raise ValueError('Request headers must be set.')
- populate_server_timeout_header(
- patched_http_options.headers, timeout_in_seconds
- )
+ # Without per-request options, patched_http_options is the client's own
+ # options. Copy the headers so that this request's X-Server-Timeout, and
+ # the Authorization header set when it is sent, do not become client
+ # defaults that later requests, Interactions and Live reuse.
+ headers = dict(patched_http_options.headers)
+ populate_server_timeout_header(headers, timeout_in_seconds)
return HttpRequest(
method=http_method,
url=url,
- headers=patched_http_options.headers,
+ headers=headers,
data=request_dict,
timeout=timeout_in_seconds,
)
With this, Interactions and Live always send the current token, X-Server-Timeout matches each request's timeout, and the client's default headers stay as configured. It covers sync and async and every transport.
I'd rather not patch the readers (filter Authorization in the bridge, always overwrite X-Server-Timeout). That would be three separate patches, and it would break the existing rule that a user-set X-Server-Timeout wins (test_server_timeout_not_overwritten).
One behaviour change worth noting: on Vertex, Live currently only gets x-goog-user-project incidentally, after some REST call has written it into the shared headers. With this fix it won't, so Live should set it itself from credentials.quota_project_id.
I have regression tests that fail on main:
- a unit test on
_build_request; - sync and async models → Interactions;
- models → Live.
Happy to open a PR.
- 主要语言
- Python
- 星标
- 4k
- 派生
- 1k
- 平均合并
- 1 天 7 小时
- 30 天内合并 PR
- 88
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
googleapis/python-genai 的其他 Issue
-
Test import module that doesn't exist可能已有人在做 @Venkaiahbabuneelam 于 5 天前认领。 未关闭priority: p2 status:awaiting user response type: bug
难度 2/5 1-3 小时 新手友好度 66/100
googleapis/python-genai#3122 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
FunctionDeclaration.from_callable drops enum and properties from Optional[...] parameters可能已有人在做 @Venkaiahbabuneelam 于 6 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 67/100
googleapis/python-genai#3105 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Curated history keeps half a user turn when the model turn is invalid可能已有人在做 @Venkaiahbabuneelam 于 14 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 62/100
googleapis/python-genai#3051 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[Bug]: Unsubscripted typing.List and typing.Dict crash convert_if_exist_pydantic_model in AFC可能已有人在做 @Venkaiahbabuneelam 于 14 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 82/100
googleapis/python-genai#3044 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Name collision in google.genai.interactions: triggers.Interaction shadows response model Interaction in static type checkers可能已有人在做 @Venkaiahbabuneelam 于 21 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 78/100
googleapis/python-genai#3013 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 googleapis/python-genai 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 66/100
AlexsLemonade/scpca-docs#518 ·
-
bug
难度 1/5 1 小时以内 新手友好度 95/100
rocketride-org/rocketride-server#2705 · 1 条评论 ·
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 85/100
Metabuilder-Labs/tokenjam#786 ·
维护者通常 1 天内回复
-
prototype(verifier_token): map disallowed warning to contraindicated before composite precedence check可能已有人在做 @drk-ram 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
agentrust-io/trace-spec#582 ·
维护者通常 1 天内回复
-
area:cli priority:low refactor size:small
难度 1/5 1 小时以内 新手友好度 88/100
RonaldHensbergen/composable-data-stack#906 ·
维护者通常 1 天内回复