Repository navigation
[Python] Fix Hugging Face API token fallback - #40779
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #40779 +/- ##
==========================================
+ Coverage 79.83% 80.11% +0.27%
==========================================
Files 494 494
Lines 83575 83614 +39
==========================================
+ Hits 66722 66985 +263
+ Misses 16853 16629 -224
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
74c1d31 to
48dcc1f
Compare
The handler passed an authorization header on each request, which replaced the header of the session. When the token came from the HF_TOKEN environment variable, the request sent Bearer None and the provider rejected it. Reuse the authorization established when loading the session. Also pass the URL to the API URL log call, which printed a literal %s.
48dcc1f to
3605e47
Compare
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a bug in _InferenceAPIHandler.run_inference where passing headers=self._config.authorization_token on each session.post call overwrote the session's Authorization header with Bearer None when relying on the HF_TOKEN environment variable fallback. It also fixes a missing log format argument for the API URL and adds unit tests covering session token resolution and HTTP error handling.
🔍 General Feedback
- The fix is clean and minimal, properly relying on the
Authorizationheader configured on therequests.Sessioninload_model(). - The new unit tests in
InferenceAPIHandlerTesteffectively verify both explicit and environment-based token headers as well as HTTP error propagation without requiring live API calls.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @tvalentyn for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Related #30799.
When the token comes from
HF_TOKEN, the redundant request header replaces the valid session header withBearer None, and the provider rejects the request. Reuse the authorization established when loading the session.Pass the URL to the API URL log call, which currently prints a literal
%s.The HTTP error handling from the first version of this change landed separately in #40953. This PR keeps that handling and adds regression checks for it.
In a real pipeline against a local fake endpoint, current master sends
Bearer Nonefor theHF_TOKENfallback, and this change sends the real token. The token regression check fails without this change and passes with it. The module tests, Ruff, and YAPF ran locally. The live Inference API tests need a token, so they were not run.