Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Bentoml BentoML Exportable Base

From Leeroopedia
Revision as of 12:06, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Bentoml_BentoML_Exportable_Base.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Model Export, Model Import, Filesystem, Archive Formats
Last Updated 2026-02-13 15:00 GMT

Overview

Provides the Exportable abstract base class that enables BentoML artifacts (bentos and models) to be exported to and imported from various archive formats and remote filesystems.

Description

The Exportable class defines a portable import/export interface for BentoML artifacts. It leverages fsspec for filesystem abstraction, supporting local files, S3, FTP, and other protocols registered in fsspec. Supported archive formats include tar (with xz, gzip, bz2 compression), zip, and folder (plain directory copy).

The import_from() class method parses a path or URL, determines the protocol and format (auto-detected from file extension via guess_format()), and delegates to the appropriate fsspec filesystem implementation (TarFileSystem, ZipFileSystem, or DirFileSystem). For non-local protocols, a filecache wrapper is used for caching. The _from_fs() helper extracts files from the abstract filesystem into a temp directory and calls from_path() (which subclasses must implement).

The export() method performs the reverse operation: it takes the artifact's local _path and writes it to the target destination. For folder format, it uses fs.put() with recursive copy. For archive formats, it compresses via shutil.make_archive() and then uploads the archive file. The method returns the final output path.

Subclasses must implement _export_ext() (returning the default file extension), _export_name (the base filename for exports), and from_path() (constructing an instance from a local directory).

Usage

This class is not used directly. It is subclassed by Bento and Model to provide their export() and import_from() capabilities. Use bento.export("/path/to/output.bento") or Model.import_from("s3://bucket/model.tar.gz").

Code Reference

Source Location

Signature

class Exportable(ABC):
    _path: Path

    @staticmethod
    @abstractmethod
    def _export_ext() -> str: ...

    @property
    @abstractmethod
    def _export_name(self) -> str: ...

    @classmethod
    def _from_fs(cls, fs: fsspec.AbstractFileSystem) -> t.Self: ...

    @classmethod
    @abstractmethod
    def from_path(cls, path: PathType) -> t.Self: ...

    @classmethod
    def guess_format(cls, path: str) -> str: ...

    @classmethod
    def import_from(
        cls,
        path: str,
        input_format: str | None = None,
        *,
        protocol: str | None = None,
        user: str | None = None,
        passwd: str | None = None,
        params: dict[str, str] | None = None,
        subpath: str | None = None,
    ) -> t.Self: ...

    def export(
        self,
        path: str,
        output_format: t.Optional[str] = None,
        *,
        protocol: t.Optional[str] = None,
        user: t.Optional[str] = None,
        passwd: t.Optional[str] = None,
        params: t.Optional[t.Dict[str, str]] = None,
        subpath: t.Optional[str] = None,
    ) -> str: ...

    def _compress(self, path: str, output_format: str) -> str: ...

Import

from bentoml._internal.exportable import Exportable

I/O Contract

Inputs

Name Type Required Description
path str Yes Target path or URL (local, S3, FTP, etc.) for import or export
input_format / output_format str or None No Archive format; auto-detected from extension if not specified. Values: "gz", "xz", "bz2", "tar", "zip", "folder", or the subclass default extension
protocol str or None No Filesystem protocol (e.g., "file", "s3", "ftp"); inferred from URL scheme
user str or None No Username for authenticated remote filesystems
passwd str or None No Password for authenticated remote filesystems
params dict[str, str] or None No Additional query parameters for the URL
subpath str or None No Subpath within the remote resource

Outputs

Name Type Description
instance (import_from) Self A new instance of the subclass loaded from the given path
fspath (export) str The final filesystem path where the artifact was exported

Usage Examples

import bentoml

# Export a bento to a local archive
bento = bentoml.get("my_service:latest")
output_path = bento.export("/tmp/my_service.bento")

# Export to S3
output_path = bento.export("s3://my-bucket/bentos/my_service")

# Import a bento from a local archive
bento = bentoml.bentos.import_bento("/tmp/my_service.bento")

# Import from S3
bento = bentoml.bentos.import_bento("s3://my-bucket/bentos/my_service.bento")

# Export as a plain folder
output_path = bento.export("/tmp/my_service_dir/", output_format="folder")

# Export with gzip compression
output_path = bento.export("/tmp/my_service.tar.gz", output_format="gz")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment