Skip to content

fix(provider): force Gemini chat client to use httpx#7983

Open
whatevertogo wants to merge 1 commit intoAstrBotDevs:masterfrom
whatevertogo:fix/issue-7564-gemini-httpx
Open

fix(provider): force Gemini chat client to use httpx#7983
whatevertogo wants to merge 1 commit intoAstrBotDevs:masterfrom
whatevertogo:fix/issue-7564-gemini-httpx

Conversation

@whatevertogo
Copy link
Copy Markdown
Contributor

@whatevertogo whatevertogo commented May 3, 2026

Fixes #7564.

Gemini chat requests can hit a google-genai aiohttp backend error path when aiohttp is installed alongside httpx, masking the real API failure as Unsupported response type: <class 'aiohttp.client_reqrep.ClientResponse'>. This PR gives the Gemini chat client a managed httpx.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 ProviderGoogleGenAI to initialize google-genai with a managed httpx async client.

  • Preserve global HTTP_PROXY / HTTPS_PROXY behavior by keeping trust_env=True when no provider proxy is set.

  • Preserve provider-level proxy by passing it into httpx.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 / 运行截图或测试结果

uv run pytest tests/test_gemini_source.py tests/test_httpx_socks_dependency.py -q
..........................                                               [100%]
26 passed in 2.61s
uv run ruff check astrbot/core/provider/sources/gemini_source.py tests/test_gemini_source.py
All checks passed!

Live check:

LIVE_GEMINI_OK True 2
ENV_KEY_PRESENT False

Notes:

  • A live non-streaming gemini-2.5-flash call returned OK.
  • A live gemini-2.5-pro call reached Gemini through the httpx path but returned RESOURCE_EXHAUSTED quota 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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Copilot AI review requested due to automatic review settings May 3, 2026 18:04
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 3, 2026
@dosubot dosubot Bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label May 3, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/provider/sources/gemini_source.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.AsyncHTTPTransport to ProviderGoogleGenAI via HttpOptions.async_client_args.
  • Thread the per-provider proxy setting 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.

Comment thread astrbot/core/provider/sources/gemini_source.py Outdated
Comment thread tests/test_gemini_source.py Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@whatevertogo whatevertogo force-pushed the fix/issue-7564-gemini-httpx branch from e5884bc to b82bac1 Compare May 3, 2026 18:13
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OneBot消息平台(aiocqhttp)无法成功调用Google Gemini模型,尝试过gemini-3-flash-preview和gemini-2.5-pro,均失败

2 participants