Skip to content

Conversation

@Wauplin
Copy link
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
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
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
Collaborator

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 -584
if isinstance(user_agent, dict):
user_agent.update(
{
"kernels": __version__,
"python": python,
"torch": torch.__version__,
"build_variant": build_variant(),
"file_type": "kernel",
}
)
Copy link
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
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
Collaborator

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

@sayakpaul sayakpaul requested a review from danieldk February 4, 2026 15:16
MekkCyber
MekkCyber previously approved these changes Feb 9, 2026
Copy link
Collaborator

@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 -584
if isinstance(user_agent, dict):
user_agent.update(
{
"kernels": __version__,
"python": python,
"torch": torch.__version__,
"build_variant": build_variant(),
"file_type": "kernel",
}
)
Copy link
Collaborator

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
Collaborator

Choose a reason for hiding this comment

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

indeed ! thanks @Wauplin

@HuggingFaceDocBuilderDev

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
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?

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.

4 participants