Skip to content

Different behavior when passing exist_ok #2419

@albertvillanova

Description

@albertvillanova

Describe the bug

I don't get any error if I call:

api.create_repo(repo_id, repo_type="dataset", token=token, exist_ok=True)

with a non-write token if the repo already exists:

  • The 403 Forbidden error is ignored if the repo exists

However, I do get an error if I call:

api.create_branch(repo_id, branch=branch, repo_type="dataset", token=token, exist_ok=True)

with a non-write token if the branch already exists in the repo:

  • The 403 Forbidden error is raised

Why are not the behaviors aligned?
I would expect the second case does not raise an error either.

Reproduction

Create a READ access token.

No error is raised when calling create_repo:

from huggingface_hub import HfApi

api = HfApi()
repo_id = "DFKI-SLT/science_ie"
token = <YOUR READ-TOKEN>

api.create_repo(repo_id, repo_type="dataset", token=token, exist_ok=True)

Output:

RepoUrl('https://huggingface.co/dataset/DFKI-SLT/science_ie', endpoint='https://huggingface.co', repo_type='dataset', repo_id='DFKI-SLT/science_ie')

An error is raised when calling create_branch:

from huggingface_hub import HfApi

api = HfApi()
repo_id = "DFKI-SLT/science_ie"
branch = "script"
token = <YOUR READ-TOKEN>

api.create_branch(repo_id, branch=revision, repo_type="dataset", token=token, exist_ok=True)

Logs

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/huggingface/datasets/venv/lib/python3.9/site-packages/huggingface_hub/utils/_errors.py in hf_raise_for_status(response, endpoint_name)
    303     try:
--> 304         response.raise_for_status()
    305     except HTTPError as e:

~/huggingface/datasets/venv/lib/python3.9/site-packages/requests/models.py in raise_for_status(self)
   1023         if http_error_msg:
-> 1024             raise HTTPError(http_error_msg, response=self)
   1025 

HTTPError: 403 Client Error: Forbidden for url: https://huggingface.co/api/datasets/DFKI-SLT/science_ie/branch/script

The above exception was the direct cause of the following exception:

HfHubHTTPError                            Traceback (most recent call last)
<ipython-input-11-5fdb76f86d82> in <module>
----> 1 api.create_branch(repo_id, branch="script", repo_type="dataset", exist_ok=True)

~/huggingface/datasets/venv/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py in _inner_fn(*args, **kwargs)
    112             kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.__name__, has_token=has_token, kwargs=kwargs)
    113 
--> 114         return fn(*args, **kwargs)
    115 
    116     return _inner_fn  # type: ignore

~/huggingface/datasets/venv/lib/python3.9/site-packages/huggingface_hub/hf_api.py in create_branch(self, repo_id, branch, revision, token, repo_type, exist_ok)
   5501         response = get_session().post(url=branch_url, headers=headers, json=payload)
   5502         try:
-> 5503             hf_raise_for_status(response)
   5504         except HfHubHTTPError as e:
   5505             if not (e.response.status_code == 409 and exist_ok):

~/huggingface/datasets/venv/lib/python3.9/site-packages/huggingface_hub/utils/_errors.py in hf_raise_for_status(response, endpoint_name)
    365                 + "make sure you have a token with the `write` role."
    366             )
--> 367             raise HfHubHTTPError(message, response=response) from e
    368 
    369         # Convert `HTTPError` into a `HfHubHTTPError` to display request information

HfHubHTTPError:  (Request ID: Root=1-66a1f6ed-217c6f852d4a6ef701fabdc7;87549d9f-417b-4b16-ab4d-b55ea7a7ad1c)

403 Forbidden: Forbidden: cannot write to script.
Cannot access content at: https://huggingface.co/api/datasets/DFKI-SLT/science_ie/branch/script.
If you are trying to create or update content,make sure you have a token with the `write` role.

System info

- huggingface_hub version: 0.23.2
- Platform: Linux-5.15.0-116-generic-x86_64-with-glibc2.31
- Python version: 3.9.7
- Running in iPython ?: Yes
- iPython shell: TerminalInteractiveShell
- Running in notebook ?: No
- Running in Google Colab ?: No
- Token path ?: /home/albert/.cache/huggingface/token
- Has saved token ?: True
- Who am I ?: albertvillanova
- Configured git credential helpers: 
- FastAI: N/A
- Tensorflow: 2.17.0
- Torch: 2.3.0
- Jinja2: 2.11.3
- Graphviz: N/A
- keras: 3.4.1
- Pydot: 1.4.2
- Pillow: 10.3.0
- hf_transfer: N/A
- gradio: N/A
- tensorboard: N/A
- numpy: 1.26.4
- pydantic: 2.7.1
- aiohttp: 3.9.5
- ENDPOINT: https://huggingface.co

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions