Skip to content

Attach images/videos to discussions and PRs (like gh --attach) #5111

Open

Description

@gary149

low prio

The latest gh release (2.102) added an --attach flag to gh pr create and gh pr edit:

gh pr create --attach './before.png#Before the fix' --attach ./after.png

It uploads the files to GitHub and puts them in the PR body. If the body already references a file (![alt](./before.png)), that reference is rewritten to the uploaded URL. Otherwise the image is appended. Very handy, especially for agents opening PRs with screenshots.

There's no equivalent in huggingface_hub today. When you drag an image into a discussion or PR comment box on the Hub, the web editor uploads it to https://huggingface.co/uploads and inserts the returned cdn-uploads.huggingface.co URL as markdown. From Python or the hf CLI, the only way to get the same result is to call that endpoint by hand:

URL=$(curl -s -X POST https://huggingface.co/uploads -H "Content-Type: image/png" --data-binary @shot.png)

and then paste ![shot]($URL) into the description passed to create_discussion or create_commit(..., create_pr=True). It works, but it's undocumented and every script (or agent) has to rediscover it.

Proposal

  • Python: HfApi.upload_attachment(path) -> str, returning the CDN URL.
  • CLI: a repeatable --attach FILE[#alt text] option on hf discussions create (including --pr), hf discussions comment and hf discussions edit, with the same behavior as gh: rewrite ./file references in the body, append the others.
  • Maybe later: the same option on hf upload --create-pr, for the PR description.
  • Check the file type client-side against what the Hub editor accepts (PNG, JPEG, GIF, WebP, MP4, MOV, WebM, MP3, WAV). SVG is not accepted.

Open question (Hub side)

The upload endpoint doesn't take a token: files uploaded outside the browser end up under .../uploads/noauth/... instead of being tied to the user's account. Before exposing this as a public API, it would be good to have the endpoint accept a regular Authorization: Bearer token so uploads are attributed to the user.

Activity

  1. julien-c commented on Oct 9, 2026

    @julien-c
    Member

    Before exposing this as a public API, it would be good to have the endpoint accept a regular Authorization: Bearer token so uploads are attributed to the user.

    it does accept a token!

    EDIT: shit no it doesn't

  2. gary149 commented on Oct 12, 2026

    @gary149
    ContributorAuthor

    it does accept a token!

    yes indeed (lands under uploads/noauth) & the token is ignored (works without it) but it's probably fine.

  3. moon-bot-app commented on Oct 13, 2026

    @moon-bot-app
    Contributor

    Follow-up on the token question: POST /uploads was cookie-only server-side, so a Bearer token was silently ignored and uploads landed under uploads/noauth/.

    Hub-side fix: https://github.com/huggingface-internal/moon-landing/pull/20414 (internal). Once it's deployed:

    • Authorization: Bearer hf_… (user access token) and OAuth tokens are accepted, and uploads are attributed to the user (uploads/<userId>/…).
    • The token needs discussion.write, the same permission as posting a comment: classic read/write tokens, fine-grained tokens with discussion.write (global or scoped to any repo/namespace), or OAuth apps with the write-discussions scope. Otherwise it returns a 403.
    • Anonymous uploads keep working as before.

    So HfApi.upload_attachment can just send the usual auth headers.

  4. julien-c commented on Oct 14, 2026

    @julien-c
    Member

    the server side change has been deployed FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions