Implementation:Bentoml BentoML Tag Class
| Knowledge Sources | |
|---|---|
| Domains | Core Framework, Versioning, Artifact Management |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Implements the Tag class used throughout BentoML to represent versioned identifiers for bentos and models in the format name:version.
Description
The Tag class is an attrs-based dataclass with name and optional version fields. Both fields are automatically lowercased and validated against a strict pattern: must be at most 63 characters, contain only lowercase alphanumeric characters, underscores, dashes, or dots, and start and end with an alphanumeric character. The class provides comparison operators (__eq__, __lt__, __hash__), string conversion (__str__ produces "name:version"), and several factory methods: from_str (parse "name:version" strings), from_taglike (accept either a Tag or string), and make_new_version (generate a UUID-based version using base32 encoding). The path method returns a POSIX path representation (name/version), and latest_path returns the path to the "latest" version file. The module also includes to_snake_case for converting CamelCase to snake_case, and registers cattrs structure/unstructure hooks for Tag serialization.
Usage
Use the Tag class anywhere you need to reference a BentoML artifact by name and version. It is the fundamental identifier type used by Store, Bento, Model, and related BentoML subsystems.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/tag.py
- Lines: 1-158
Signature
def to_snake_case(name: str) -> str: ...
def validate_tag_str(value: str): ...
@attr.define(slots=True)
class Tag:
name: str
version: t.Optional[str]
def __init__(self, name: str, version: t.Optional[str] = None): ...
def __str__(self): ...
def __repr__(self): ...
def __eq__(self, other: object) -> bool: ...
def __lt__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
@classmethod
def from_taglike(cls, taglike: t.Union["Tag", str]) -> "Tag": ...
@classmethod
def from_str(cls, tag_str: str) -> "Tag": ...
def make_new_version(self) -> "Tag": ...
def path(self) -> str: ...
def latest_path(self) -> str: ...
Import
from bentoml._internal.tag import Tag
# or via the public API:
from bentoml import Tag
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | The artifact name; will be lowercased and validated |
| version | str or None | No | The version string; will be lowercased and validated if provided |
| tag_str | str | Yes (from_str) | String in "name:version" or "name" format to parse |
| taglike | Tag or str | Yes (from_taglike) | Either a Tag instance or a string to convert |
Outputs
| Name | Type | Description |
|---|---|---|
| Tag | Tag | A validated Tag instance with name and optional version |
| str | str | String representation as "name:version" or "name" (via __str__) |
| str | str | POSIX path as "name/version" (via path()) |
Usage Examples
from bentoml import Tag
# Create a tag
tag = Tag("my_model", "v1.0.0")
print(str(tag)) # "my_model:v1.0.0"
# Parse from string
tag = Tag.from_str("iris_classifier:abc123")
print(tag.name) # "iris_classifier"
print(tag.version) # "abc123"
# Generate a new version
base_tag = Tag("my_model")
versioned_tag = base_tag.make_new_version()
print(versioned_tag) # "my_model:<auto-generated-version>"
# Get filesystem path
print(tag.path()) # "iris_classifier/abc123"
print(tag.latest_path()) # "iris_classifier/latest"