fix(provider): force Gemini chat client to use httpx#7983
fix(provider): force Gemini chat client to use httpx#7983whatevertogo wants to merge 1 commit intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/gemini_source.py" line_range="97" />
<code_context>
+ }
if proxy:
- http_options.async_client_args = {"proxy": proxy}
logger.info(f"[Gemini] 使用代理: {proxy}")
self.client = genai.Client(
api_key=self.chosen_api_key,
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Avoid logging full proxy URLs to prevent leaking credentials or internal network details.
If `proxy` may include credentials (e.g., `http://user:pass@host:port`) or sensitive internal hosts, logging it directly can leak secrets or internal network details. Mask credentials and/or log only non-sensitive parts (e.g., host/port) or a simple flag like `using proxy` instead of the full URL.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR updates the Gemini chat provider to force google-genai onto an httpx async transport so chat requests avoid the aiohttp response-type failure described in #7564. It fits into AstrBot's provider layer by changing how the Gemini chat adapter constructs its SDK client and adding focused regression tests around that initialization.
Changes:
- Add an explicit
httpx.AsyncHTTPTransporttoProviderGoogleGenAIviaHttpOptions.async_client_args. - Thread the per-provider
proxysetting into the new transport. - Add unit tests covering transport injection and proxy forwarding during Gemini client initialization.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
astrbot/core/provider/sources/gemini_source.py |
Changes Gemini chat client initialization to always supply an httpx transport. |
tests/test_gemini_source.py |
Adds unit tests for transport construction and provider-level proxy propagation. |
There was a problem hiding this comment.
Code Review
This pull request modifies the Gemini provider to use httpx.AsyncHTTPTransport for its asynchronous client, addressing an issue where aiohttp could mask API errors. The implementation ensures that proxy configurations are correctly passed to the new transport. Additionally, unit tests have been introduced to verify that the client correctly initializes with the forced transport and handles proxy settings as expected. I have no feedback to provide.
google-genai prefers aiohttp when aiohttp is installed, and that backend can mask Gemini API failures as Unsupported response type for aiohttp.ClientResponse. Give the Gemini chat client a managed httpx AsyncClient so chat and tool paths avoid aiohttp while preserving environment proxy support, provider proxy support, timeout configuration, and clean shutdown. Also stop logging proxy URLs or API key prefixes. Constraint: Keep the fix scoped to the Gemini chat provider reported in the issue. Rejected: Do not remove aiohttp from project dependencies or switch every Gemini provider in this PR. Directive: Use a managed httpx AsyncClient when constructing the Google Gemini chat client. Confidence: medium-high Scope-risk: low Reversibility: straightforward Tested: uv run pytest tests/test_gemini_source.py tests/test_httpx_socks_dependency.py -q Tested: uv run ruff check astrbot/core/provider/sources/gemini_source.py tests/test_gemini_source.py Tested: Live non-streaming gemini-2.5-flash call returned OK. Not-tested: Successful live gemini-2.5-pro call because the provided key returned RESOURCE_EXHAUSTED quota errors. Related: AstrBotDevs#7564 Co-authored-by: OmX <omx@oh-my-codex.local>
e5884bc to
b82bac1
Compare
Fixes #7564.
Gemini chat requests can hit a google-genai aiohttp backend error path when
aiohttpis installed alongsidehttpx, masking the real API failure asUnsupported response type: <class 'aiohttp.client_reqrep.ClientResponse'>. This PR gives the Gemini chat client a managedhttpx.AsyncClient, which makes google-genai avoid the aiohttp backend for chat paths while preserving AstrBot's global env proxy support and provider-level proxy setting.Modifications / 改动点
This is NOT a breaking change. / 这不是一个破坏性变更。
Force
ProviderGoogleGenAIto initialize google-genai with a managed httpx async client.Preserve global
HTTP_PROXY/HTTPS_PROXYbehavior by keepingtrust_env=Truewhen no provider proxy is set.Preserve provider-level
proxyby passing it intohttpx.AsyncClient(proxy=...).Close the managed httpx client in
terminate().Avoid logging full proxy URLs or API key prefixes.
Add unit tests for httpx client injection, env proxy preservation, provider proxy propagation, and secret-safe logging.
Leave Gemini TTS and Embedding providers unchanged because the issue reports the chat provider path.
Screenshots or Test Results / 运行截图或测试结果
Live check:
Notes:
gemini-2.5-flashcall returnedOK.gemini-2.5-procall reached Gemini through the httpx path but returnedRESOURCE_EXHAUSTEDquota errors for the provided key, so a successful 2.5 Pro response was not verified.Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。