Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix scalar tags splitting into single-character list in card front ma…
…tter

Fixes #5012

Signed-off-by: zeenat28-ui <zeenatriaz468@gmail.com>
  • Loading branch information
zeenat28-ui committed Sep 27, 2026
commit ec8248662b5c4c067e14db566a2da2c66183e86d
8 changes: 5 additions & 3 deletions src/huggingface_hub/repocard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def __init__(
metrics: list[str] | None = None,
model_name: str | None = None,
pipeline_tag: str | None = None,
tags: list[str] | None = None,
tags: str | list[str] | None = None,
ignore_metadata_errors: bool = False,
**kwargs,
):
Expand Down Expand Up @@ -540,7 +540,7 @@ def __init__(
duplicated_from: str | None = None,
models: list[str] | None = None,
datasets: list[str] | None = None,
tags: list[str] | None = None,
tags: str | list[str] | None = None,
ignore_metadata_errors: bool = False,
**kwargs,
):
Expand Down Expand Up @@ -767,9 +767,11 @@ def eval_results_to_model_index(model_name: str, eval_results: list[EvalResult])
return _remove_none(model_index)


def _to_unique_list(tags: list[str] | None) -> list[str] | None:
def _to_unique_list(tags: str | list[str] | None) -> list[str] | None:
if tags is None:
return tags
if isinstance(tags, str):
tags = [tags]
unique_tags = [] # make tags unique + keep order explicitly
for tag in tags:
if tag not in unique_tags:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,12 @@ def test_preserve_order_load_save(self):
model_card.data.license = "test"
assert model_card.content == "---\nlicense: test\ndatasets:\n- foo\n- bar\n---\n\nHello\n"

def test_model_card_scalar_tags_in_frontmatter(self):
content = "---\ntags: text-classification\nlanguage: en\n---\nbody"
model_card = ModelCard(content)
assert model_card.data.tags == ["text-classification"]
assert "tags:\n- text-classification" in str(model_card)


class TestDatasetCard:
def test_load_datasetcard_from_file(self):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_repocard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ def test_model_card_unique_tags(self):
data = ModelCardData(tags=["tag2", "tag1", "tag2", "tag3"])
assert data.tags == ["tag2", "tag1", "tag3"]

def test_model_card_scalar_tag(self):
data = ModelCardData(tags="text-classification")
assert data.tags == ["text-classification"]

def test_remove_top_level_none_values(self):
as_obj = ModelCardData(tags=["tag1", None], foo={"bar": 3, "baz": None}, pipeline_tag=None)
as_dict = as_obj.to_dict()
Expand Down Expand Up @@ -384,3 +388,7 @@ def test_space_card_data(self) -> None:
"duplicated_from": "multimodalart/dreambooth-training",
}
assert card_data.tags is None # SpaceCardData has some default attributes

def test_space_card_data_scalar_tag(self) -> None:
card_data = SpaceCardData(tags="gradio")
assert card_data.tags == ["gradio"]