Skip to content

[Python] Fix Hugging Face API token fallback - #40779

Merged
tvalentyn merged 1 commit into
apache:masterfrom
bvolpato:bvolpato/huggingface-api-error-reporting
Nov 18, 2026
Merged

tvalentyn merged 1 commit into
apache:masterfrom
bvolpato:bvolpato/huggingface-api-error-reporting

Conversation

@bvolpato

@bvolpato bvolpato commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Related #30799.

When the token comes from HF_TOKEN, the redundant request header replaces the valid session header with Bearer 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 None for the HF_TOKEN fallback, 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.

@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Oct 25, 2026
@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.11%. Comparing base (12bbb17) to head (3605e47).
⚠️ Report is 291 commits behind head on master.

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     
Flag Coverage Δ
python 80.11% <100.00%> (+0.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bvolpato
bvolpato force-pushed the bvolpato/huggingface-api-error-reporting branch 2 times, most recently from 74c1d31 to 48dcc1f Compare November 3, 2026 03:46
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.
@bvolpato
bvolpato force-pushed the bvolpato/huggingface-api-error-reporting branch from 48dcc1f to 3605e47 Compare November 4, 2026 15:19
@bvolpato bvolpato changed the title [Python] Fix Hugging Face API error and token handling [Python] Fix Hugging Face API token fallback Nov 4, 2026
@bvolpato
bvolpato marked this pull request as ready for review November 17, 2026 00:10

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 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 Authorization header configured on the requests.Session in load_model().
  • The new unit tests in InferenceAPIHandlerTest effectively 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.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.
Backup reviewer: shunping (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@tvalentyn
tvalentyn merged commit a7b565e into apache:master Nov 18, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants