fix: align OpenAI http_client with SDK httpx#7773
fix: align OpenAI http_client with SDK httpx#7773bugkeep wants to merge 3 commits intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
_create_http_client, consider catching a narrower exception (e.g.,ImportErrorandAttributeError) instead of a bareExceptionwhen importingopenai._base_clientand accessinghttpxto avoid masking unrelated errors. create_proxy_clientnow accepts a generichttpx_module: Anybut still advertiseshttpx.AsyncClientas its return type; consider updating the type hints (e.g., to a protocol or a type variable tied tohttpx_module.AsyncClient) so that callers and static analysis tools get an accurate client type.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_create_http_client`, consider catching a narrower exception (e.g., `ImportError` and `AttributeError`) instead of a bare `Exception` when importing `openai._base_client` and accessing `httpx` to avoid masking unrelated errors.
- `create_proxy_client` now accepts a generic `httpx_module: Any` but still advertises `httpx.AsyncClient` as its return type; consider updating the type hints (e.g., to a protocol or a type variable tied to `httpx_module.AsyncClient`) so that callers and static analysis tools get an accurate client type.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/openai_source.py" line_range="445-450" />
<code_context>
+ from openai import _base_client as openai_base_client
+
+ httpx_module = openai_base_client.httpx
+ except Exception:
+ httpx_module = httpx
+ return create_proxy_client("OpenAI", proxy, httpx_module=httpx_module)
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Catch narrower exceptions instead of a blanket `Exception` when importing the OpenAI httpx module.
This block only wraps the import of `openai._base_client` and access to `openai_base_client.httpx`. Catching a blanket `Exception` can hide real runtime errors in the OpenAI SDK and cause an unexpected fallback to the global `httpx`. Please narrow this to `ImportError` and `AttributeError`, which are the expected failures when the module or attribute is missing.
```suggestion
try:
from openai import _base_client as openai_base_client
httpx_module = openai_base_client.httpx
except (ImportError, AttributeError):
httpx_module = httpx
```
</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.
Code Review
This pull request updates the OpenAI source provider to use the httpx module from the openai SDK when creating HTTP clients, ensuring compatibility with the SDK's internal type checks. The create_proxy_client utility has been generalized to support an optional httpx_module argument, and a unit test was added to verify the implementation. A review comment suggests refining the exception handling during the openai module import to be more specific and to remove redundant variable assignments.
| httpx_module: Any = httpx | ||
| try: | ||
| from openai import _base_client as openai_base_client | ||
|
|
||
| httpx_module = openai_base_client.httpx | ||
| except Exception: | ||
| httpx_module = httpx |
There was a problem hiding this comment.
The assignment httpx_module = httpx in the except block is redundant because httpx_module is already initialized to httpx on line 444. Additionally, catching a broad Exception is generally discouraged; using getattr with a default value and catching only ImportError results in a cleaner and more robust implementation.
| httpx_module: Any = httpx | |
| try: | |
| from openai import _base_client as openai_base_client | |
| httpx_module = openai_base_client.httpx | |
| except Exception: | |
| httpx_module = httpx | |
| httpx_module = httpx | |
| try: | |
| from openai import _base_client as openai_base_client | |
| httpx_module = getattr(openai_base_client, "httpx", httpx) | |
| except ImportError: | |
| pass |
References
- PEP 8: Programming Recommendations - When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause. Catching Exception can hide other errors. (link)
d5f3315 to
a0d40f1
Compare
Fixes #7770.
The OpenAI SDK validates
http_clientwithisinstance(..., httpx.AsyncClient)using the SDK's ownhttpximport. In some packaged/Docker environments multiplehttpxinstallations can be present, causing this check to fail even when passing a real AsyncClient instance.This change lets
create_proxy_clientbuild the client from a caller-provided httpx module, and updates the OpenAI provider to construct the client usingopenai._base_client.httpx(falling back to the regularhttpximport if unavailable).Tests:
uv run ruff format .uv run ruff check .uv run pytest -q tests/test_openai_source.py::test_create_http_client_uses_openai_httpx_moduleSummary by Sourcery
Align OpenAI HTTP client construction with the OpenAI SDK’s own httpx import to avoid type mismatches in certain environments.
Bug Fixes:
Enhancements:
Tests: