Skip to content

Clean-up huggingface_hub integration#263

Closed
Wauplin wants to merge 18 commits intohuggingface:mainfrom
Wauplin:cleanup-huggingface-hub-integration
Closed

Clean-up huggingface_hub integration#263
Wauplin wants to merge 18 commits intohuggingface:mainfrom
Wauplin:cleanup-huggingface-hub-integration

Conversation

@Wauplin
Copy link
Copy Markdown
Contributor

@Wauplin Wauplin commented Feb 4, 2026

This PR cleans up a bit the integration with huggingface_hub. In particular, it defines a _get_hf_api helper that instantiate an HfApi client with the correct user agent set. The goal is to have consistent user agent across all calls made to the Hub.

List of changes:

  • add kernels.utils._get_hf_api to return a HfApi instance
  • use it everywhere in kernels. No more from huggingface_hub import create_repo, snapshot_download, etc. anymore
  • slightly refactored user agent creation to always return a string (=> deduplicates some logic). Removed _user_agents helper (not used anymore)
  • rely on huggingface_hub.constants.HF_HUB_DISABLE_TELEMETRY instead of os.getenv("DISABLE_TELEMETRY"). The hfh constant is more robust (e.g. handles DO_NOT_TRACK env variable)
  • removed requests from benchmark dependencies. Use get_session, build_hf_headers and hf_raise_for_status instead. This should get you better logs in case of errors and relies either on requests or httpx depending on what's available. It also handles token retrieval by default.

Overall kernels behavior and API remains exactly the same. Changes are meant to be purely internal.

I am sorry in advance for all the style changes. I've run make style locally and inadvertently pushed more changes than what I thought. Let me know if you want me to try to revert them.

return repo_id.split("/")[-1].replace("-", "_")


def _get_user_agent(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO, it wouldn't hurt to keep this function and use it inside _get_hf_api(). I think that's more idiomatic and structured.


return user_agent
return HfApi(
library_name="kernels", library_version=__version__, user_agent=user_agent_str
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice refactor!

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.

indeed ! thanks @Wauplin

Comment on lines -574 to -583
if isinstance(user_agent, dict):
user_agent.update(
{
"kernels": __version__,
"python": python,
"torch": torch.__version__,
"build_variant": build_variant(),
"file_type": "kernel",
}
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hopefully, it doesn't break anything. Cc: @MekkCyber do you reckon anything to be breaking for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

my goal was to deduplicate logic (the user agent was defined both in a dictionary format and a string format, now we only define it once)

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.

good for me since we still convert the dict to str so not breaking

Comment thread kernels/uv.lock
@sayakpaul sayakpaul requested a review from danieldk February 4, 2026 15:16
MekkCyber
MekkCyber previously approved these changes Feb 9, 2026
Copy link
Copy Markdown
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

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

Nice refactor! thanks for pr @Wauplin

Comment on lines -574 to -583
if isinstance(user_agent, dict):
user_agent.update(
{
"kernels": __version__,
"python": python,
"torch": torch.__version__,
"build_variant": build_variant(),
"file_type": "kernel",
}
)
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.

good for me since we still convert the dict to str so not breaking


return user_agent
return HfApi(
library_name="kernels", library_version=__version__, user_agent=user_agent_str
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.

indeed ! thanks @Wauplin

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin
Copy link
Copy Markdown
Contributor Author

Wauplin commented Feb 9, 2026

@sayakpaul @MekkCyber thanks for the reviews. I've solved the merged conflict + fixed the CI. Can I let you do a last check and merge?

sayakpaul
sayakpaul previously approved these changes Feb 9, 2026
@Wauplin
Copy link
Copy Markdown
Contributor Author

Wauplin commented Feb 10, 2026

Resolved another merge conflict. @sayakpaul once CI is green could you merge so that we don't get new conflicts. I'm not maintainer on this repo so can't do it myself :)

sayakpaul
sayakpaul previously approved these changes Feb 10, 2026
Copy link
Copy Markdown
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

@danieldk could you give this a check as well?

@Wauplin
Copy link
Copy Markdown
Contributor Author

Wauplin commented Feb 10, 2026

@sayakpaul @danieldk I had to resolved conflicts due to this refactor PR #269 + fixed some formatting corrections I pushed by mistake but it should now be good to merge again. I'd really prefer not to have to do it again 🙏

@drbh
Copy link
Copy Markdown
Collaborator

drbh commented Feb 10, 2026

Hey @Wauplin thank you for the improvements and the refactors! apologies for the moving target. I've reopened this PR in #274 including all of your changes and resolved the remaining conflict

@drbh
Copy link
Copy Markdown
Collaborator

drbh commented Feb 10, 2026

closing as #274 was merged.

Thanks again!

@drbh drbh closed this Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants