Skip to content

Commit 79ffc3a

Browse files
committed
[Buckets] Add search param to list_buckets
1 parent f7a25b4 commit 79ffc3a

6 files changed

Lines changed: 40 additions & 2 deletions

File tree

docs/source/en/guides/buckets.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ username/logs 321.8 MB 2000 2026-02-13
128128

129129
# List buckets in a specific namespace
130130
>>> hf buckets ls huggingface
131+
132+
# Filter buckets by name
133+
>>> hf buckets list --search "checkpoint"
134+
```
135+
136+
You can also filter buckets by name using `search`:
137+
138+
```py
139+
>>> for bucket in list_buckets(search="checkpoint"):
140+
... print(bucket.id)
131141
```
132142

133143
You can use the `--quiet` and `--format json` options to get different output format. This is particularly interesting if you want to pipe the output to another tool like `grep` or `jq`.

docs/source/en/guides/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ username/logs 321.8 MB 2000 2026-02-13
574574

575575
# List buckets in a specific namespace
576576
>>> hf buckets ls my-org
577+
578+
# Filter buckets by name
579+
>>> hf buckets list --search "checkpoint"
577580
```
578581

579582
To get detailed information about a specific bucket (returned as JSON), use `hf buckets info`:

docs/source/en/package_reference/cli.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ $ hf buckets list [OPTIONS] [ARGUMENT]
399399
* `-h, --human-readable`: Show sizes in human readable format.
400400
* `--tree`: List files in tree format (only for listing files).
401401
* `-R, --recursive`: List files recursively (only for listing files).
402+
* `--search TEXT`: Search query.
402403
* `--format [table|json]`: Output format (table or json). [default: table]
403404
* `-q, --quiet`: Print only IDs (one per line).
404405
* `--token TEXT`: A User Access Token generated from https://huggingface.co/settings/tokens.
@@ -407,6 +408,7 @@ $ hf buckets list [OPTIONS] [ARGUMENT]
407408
Examples
408409
$ hf buckets list
409410
$ hf buckets list huggingface
411+
$ hf buckets list --search "my-prefix"
410412
$ hf buckets list user/my-bucket
411413
$ hf buckets list user/my-bucket -R
412414
$ hf buckets list user/my-bucket -h

src/huggingface_hub/cli/buckets.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
FormatOpt,
4444
OutputFormat,
4545
QuietOpt,
46+
SearchOpt,
4647
TokenOpt,
4748
api_object_to_dict,
4849
get_hf_api,
@@ -283,6 +284,7 @@ def _is_bucket_id(argument: str) -> bool:
283284
examples=[
284285
"hf buckets list",
285286
"hf buckets list huggingface",
287+
'hf buckets list --search "my-prefix"',
286288
"hf buckets list user/my-bucket",
287289
"hf buckets list user/my-bucket -R",
288290
"hf buckets list user/my-bucket -h",
@@ -325,6 +327,7 @@ def list_cmd(
325327
help="List files recursively (only for listing files).",
326328
),
327329
] = False,
330+
search: SearchOpt = None,
328331
format: FormatOpt = OutputFormat.table,
329332
quiet: QuietOpt = False,
330333
token: TokenOpt = None,
@@ -338,6 +341,8 @@ def list_cmd(
338341
is_file_mode = argument is not None and _is_bucket_id(argument)
339342

340343
if is_file_mode:
344+
if search is not None:
345+
raise typer.BadParameter("Cannot use --search when listing files.")
341346
_list_files(
342347
argument=argument, # type: ignore
343348
human_readable=human_readable,
@@ -350,6 +355,7 @@ def list_cmd(
350355
else:
351356
_list_buckets(
352357
namespace=argument,
358+
search=search,
353359
human_readable=human_readable,
354360
as_tree=as_tree,
355361
recursive=recursive,
@@ -361,6 +367,7 @@ def list_cmd(
361367

362368
def _list_buckets(
363369
namespace: str | None,
370+
search: str | None,
364371
human_readable: bool,
365372
as_tree: bool,
366373
recursive: bool,
@@ -382,7 +389,7 @@ def _list_buckets(
382389
namespace = namespace.rstrip("/")
383390

384391
api = get_hf_api(token=token)
385-
results = [api_object_to_dict(bucket) for bucket in api.list_buckets(namespace=namespace)]
392+
results = [api_object_to_dict(bucket) for bucket in api.list_buckets(namespace=namespace, search=search)]
386393

387394
if not results:
388395
if not quiet and format != OutputFormat.json:

src/huggingface_hub/hf_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12523,13 +12523,16 @@ def list_buckets(
1252312523
self,
1252412524
namespace: str | None = None,
1252512525
*,
12526+
search: str | None = None,
1252612527
token: bool | str | None = None,
1252712528
) -> Iterable[BucketInfo]:
1252812529
"""List buckets on the Hub under a certain namespace.
1252912530

1253012531
Args:
1253112532
namespace (`str`, *optional*):
1253212533
List buckets under this namespace (user or organization). Defaults to listing user's buckets.
12534+
search (`str`, *optional*):
12535+
A search string to filter bucket names.
1253312536
token (`bool` or `str`, *optional*):
1253412537
A valid user access token (string). Defaults to the locally saved
1253512538
token, which is the recommended method for authentication (see
@@ -12547,12 +12550,18 @@ def list_buckets(
1254712550

1254812551
>>> for bucket in list_buckets(namespace="huggingface"): # lists buckets in the "huggingface" organization
1254912552
... print(bucket)
12553+
12554+
>>> for bucket in list_buckets(search="my-prefix"): # filter buckets by name
12555+
... print(bucket)
1255012556
```
1255112557
"""
1255212558
if namespace is None:
1255312559
namespace = "me"
12560+
params: dict[str, Any] = {}
12561+
if search is not None:
12562+
params["search"] = search
1255412563
for item in paginate(
12555-
f"{self.endpoint}/api/buckets/{namespace}", params={}, headers=self._build_hf_headers(token=token)
12564+
f"{self.endpoint}/api/buckets/{namespace}", params=params, headers=self._build_hf_headers(token=token)
1255612565
):
1255712566
yield BucketInfo(**item)
1255812567

tests/test_buckets_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ def test_bucket_list_error_recursive_with_namespace():
506506
assert "Cannot use --recursive when listing buckets" in result.output
507507

508508

509+
def test_bucket_list_error_search_with_files(tree_bucket: str):
510+
"""Cannot use --search when listing files."""
511+
result = cli(f"hf buckets list {tree_bucket} --search foo")
512+
assert result.exit_code != 0
513+
assert "Cannot use --search when listing files" in result.output
514+
515+
509516
# =============================================================================
510517
# List files
511518
# =============================================================================

0 commit comments

Comments
 (0)