Skip to content

Load a dataset from a cloud storage directory given as an fsspec URL - #9163

Open
behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/5281-fsspec-url-path
Open

behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/5281-fsspec-url-path

Conversation

@behroozazarkhalili

@behroozazarkhalili behroozazarkhalili commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #5281

data_files="s3://..." with storage_options already works, but the form asked for in #5281, a cloud storage directory as the first argument, doesn't: the URL is treated as a relative local path and fails with Couldn't find any data file at <cwd>/s3:/bucket/dir.

With this change a directory given as a fsspec URL is read like a local directory:

ds = load_dataset("s3://my-bucket/path/to/my/data", storage_options={"key": ..., "secret": ...})

As proposed in #5281 (comment). I didn't get a go-ahead there, so feel free to close this if you prefer to keep the first argument local-or-Hub only.

How it works:

  • dataset_module_factory sends a path that is a remote URL (is_remote_url), after the hf://datasets/ and hf://buckets/ prefixes are handled as before, to a new FsspecDatasetModuleFactory. Local paths, Hub repositories and buckets take the same branches as before.
  • The factory follows HubBucketDatasetModuleFactory, with the filesystem coming from url_to_fs and the storage options selected by _prepare_path_and_storage_options, like resolve_pattern does: it reads README.md / .huggingface.yaml if present, gets the patterns with get_data_patterns, resolves them with DataFilesDict.from_patterns and infers the builder from the extensions. download_config is passed to each of these so the credentials reach the filesystem.
  • data_dir, data_files (relative to the directory), streaming=True, and configs defined in a dataset card work as for a local directory.
  • A URL that isn't a directory raises FileNotFoundError: Couldn't find a directory at <url>.

The factory repeats most of HubBucketDatasetModuleFactory.get_module. I left the bucket factory untouched because tests/test_buckets.py patches datasets.load.HfFileSystem; I can merge the two in this PR if you want.

Docs: a bullet in the path docstring of load_dataset / load_dataset_builder and a short section in filesystems.mdx.

Tests (5 new functions, 8 cases, in tests/test_load.py, on the mockfs fixture): splits from file names in map and streaming mode with and without a trailing slash, the builder's data_files and dataset_name, data_dir and data_files, a dataset card with a config, missing and empty directories. tests/test_load.py, tests/test_buckets.py and tests/test_data_files.py on Python 3.12: 3 tests fail on main in my environment (test_load_dataset_builder_then_edit_then_load_again and two test_data_files.py tests); on this branch 2 of the same 3 fail and the 8 new cases pass.

load_dataset("s3://bucket/dir") was treated as a relative local path and failed, while data_files="s3://..." already worked. A non-local, non-Hub URL is now resolved like a local directory over the remote filesystem, with storage_options passed to it.
@behroozazarkhalili

Copy link
Copy Markdown
Contributor Author

Note on CI: on Python 3.10 the new test_load_dataset_from_fsspec_url* tests fail with TypeError: object.__init__() takes exactly one argument (the instance to initialize). This is the xPath.__init__ error that currently fails the Python 3.10 jobs on main and that #9119 fixes. On Python 3.14 the new tests pass.

@JonnyFaeth

JonnyFaeth commented Nov 25, 2026 •

Copy link
Copy Markdown

Tested 74ae268 offline on Windows/Python 3.12.14: the 8 fsspec-URL cases pass. I also checked a conflicting README/.huggingface.yaml pair through load_dataset in both streaming modes: the standalone YAML wins, selecting its config, split, and actual rows. Those 2 extra cases pass (10 total).

This adds coverage for the new factory's metadata merge, which the README-only case does not exercise. Reversing the merge precedence only inside FsspecDatasetModuleFactory makes both added cases fail with train instead of validation. No defect found; this is a suggested regression guard, with mutation rather than parent-revision evidence.

Append to tests/test_load.py using the existing helper/imports:

@pytest.mark.parametrize("streaming", [False, True])
def test_fsspec_directory_standalone_yaml_overrides_card(mockfs, streaming):
    _write_mockfs_csv_dataset(mockfs, "bucket/overrides")
    with mockfs.open("bucket/overrides/README.md", "w") as f:
        f.write("---\nconfigs:\n- config_name: card\n  data_files:\n  - split: train\n    path: train.csv\n---\n")
    with mockfs.open("bucket/overrides/.huggingface.yaml", "w") as f:
        f.write("configs:\n- config_name: standalone\n  data_files:\n  - split: validation\n    path: test.csv\n")
    ds = load_dataset("mock://bucket/overrides", storage_options=mockfs.storage_options, streaming=streaming)
    assert set(ds) == {"validation"}
    assert ds["validation"].config_name == "standalone"
    assert list(ds["validation"]) == [{"text": "c"}]

Ruff check/format pass. This uses the repository's mockfs fixture and short local test/cache paths; it does not validate live S3 credentials, Python 3.10, or the full loading suite.

AI disclosure: Codex designed and ran these checks under @JonnyFaeth's standing authorization; Jonathan has not personally reviewed this run.

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.

Support cloud storage in load_dataset

2 participants