Skip to content

Studio: fall back to anonymous HF browsing on a malformed token - #6605

Merged
danielhanchen merged 2 commits into
mainfrom
fix/studio-hf-discovery-anonymous-fallback
Jun 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/studio-hf-discovery-anonymous-fallback

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

The Hub Discover and Recommended feeds call the Hugging Face JS client (listModels / listDatasets) directly from the browser. That client throws

Your access token must start with 'hf_'

when handed a non-empty token that isn't a well-formed HF token, instead of falling back to anonymous access. So a single bad value left in the HF token field — e.g. a placeholder like hellothere someone typed once — takes down the entire discovery feed, surfacing as:

Couldn't reach Hugging Face — The discovery feed couldn't load. … Your access token must start with 'hf_'

This is confusing because the feed works perfectly fine with no token (anonymous public browsing). It only breaks when a malformed token is present. The failure is purely client-side, so nothing shows up in the server logs.

Fix

Add hfApiToken() to the HF token store. It returns the token only when it looks like a real hf_... credential, and undefined otherwise. hub-page.tsx's four HF call sites (model search, feed write-back, model selection, dataset search) now route through it.

A malformed token therefore degrades to anonymous public browsing instead of erroring out. The raw token is still stored and shown unchanged in the settings field — this only sanitizes what gets handed to the HF client.

export function hfApiToken(
  token: string | undefined | null,
): string | undefined {
  return token && token.startsWith("hf_") ? token : undefined;
}

Testing

  • Repro: set localStorage['unsloth_hf_token'] = 'hellothere', open /hub?tab=discover → feed errors. With this change the feed loads anonymously.
  • Clearing the token (localStorage.setItem('unsloth_hf_token','')) and a valid hf_... token both continue to work as before.

🤖 Generated with Claude Code

The Discover/Recommended feeds call the Hugging Face JS client
(`listModels`/`listDatasets`) directly from the browser. That client
throws `Your access token must start with 'hf_'` when handed a non-empty
token that isn't a well-formed HF token, instead of falling back to
anonymous access. A single bad value left in the HF token field (e.g. a
placeholder someone typed) therefore takes down the entire discovery feed
even though it works fine with no token at all.

Add `hfApiToken()` to the HF token store, which returns the token only
when it looks like a real `hf_...` credential and `undefined` otherwise,
and route hub-page's four HF call sites through it. Malformed tokens now
degrade to anonymous public browsing rather than erroring. The raw token
is still stored and shown in the settings field unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a sanitization helper function, hfApiToken, to ensure only well-formed Hugging Face tokens (starting with 'hf_') are forwarded to the Hugging Face client, preventing malformed tokens from breaking anonymous browsing. The ModelsPage component is updated to use this sanitized token. The reviewer advises against implementing logic for features not yet exposed in the UI, specifically cautioning against wiring up a token store if the token cannot be configured by the user yet.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

export function hfApiToken(
token: string | undefined | null,
): string | undefined {
return token && token.startsWith("hf_") ? token : undefined;

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.

medium

Avoid implementing logic for features that are not yet exposed in the UI or planned for the near future. Do not wire up a token store for Hugging Face if the token cannot be configured by the user yet.

References
  1. Avoid implementing logic for features that are not yet exposed in the UI or planned for the near future, even if it seems more consistent. For example, do not wire up a token for a service if the token cannot be configured by the user.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, but this guidance doesn't apply here — the HF token is already user-configurable and has been for a while. The store is wired to inputs in Settings → General (general-tab.tsx), the Export page (export-page.tsx), onboarding (dataset + model-selection steps), and the studio model section. This PR doesn't introduce a new token store; it adds a sanitizer to the existing one.

In fact this is a real, user-triggered bug: a malformed value typed into one of those fields (e.g. hellothere) makes the HF JS client throw Your access token must start with 'hf_' and takes down the entire Discover/Recommended feed, which otherwise works anonymously with no token. hfApiToken() makes a malformed token degrade to anonymous browsing instead of erroring. Keeping the change.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

Collapse the 11-line JSDoc to a 2-line note and drop the redundant
call-site comment in hub-page. AST signature check confirms code is
unchanged (comments only).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 6f5a543fdb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 71e6b18 into main Jun 23, 2026
40 checks passed
@danielhanchen
danielhanchen deleted the fix/studio-hf-discovery-anonymous-fallback branch June 23, 2026 12:16
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.

1 participant