Implementation:Bentoml BentoML Configuration Containers
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Dependency Injection, Infrastructure |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
The BentoMLContainer is the central dependency injection container that provides configuration, store instances, tracing, metrics, and other shared resources throughout the BentoML framework.
Description
This module defines two core components:
BentoMLConfiguration: A class that loads, merges, and validates BentoML configuration from multiple sources in priority order:
- Default configuration (from the versioned spec module)
- Override defaults (programmatic)
- Configuration file overrides (
BENTOML_CONFIGfile) - JSON configuration overrides
- Environment variable overrides (
BENTOML_CONFIG_OPTIONS)
Configuration values support environment variable expansion and are validated against a schema.
_BentoMLContainerClass / BentoMLContainer: A dataclass-based dependency injection container using the simple_di providers pattern. It provides singleton factory methods and static providers for:
- Storage paths:
bentoml_home,bento_store_dir,model_store_dir,env_store_dir,tmp_bento_store_dir - Store instances:
bento_store,model_store,tmp_bento_store - Server configuration:
api_server_config,runners_config,http,grpc,ssl,cors - Observability:
tracer_provider(OpenTelemetry with Zipkin/Jaeger/OTLP exporters),metrics_client(Prometheus),duration_buckets - Cloud integration:
rest_api_client,bentocloud_client,cloud_config - Runtime state:
session_id,worker_index,development_mode,serialization_strategy
Usage
Used pervasively throughout BentoML as the central configuration and service locator. Other modules inject dependencies from BentoMLContainer using Provide[BentoMLContainer.xxx] decorators. The container is initialized at import time via load_config().
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/configuration/containers.py
- Lines: 1-509
Signature
class BentoMLConfiguration:
def __init__(self, override_config_file: str | None = None,
override_config_values: str | None = None,
override_defaults: dict[str, Any] | None = None,
override_config_json: dict[str, Any] | None = None,
*, validate_schema: bool = True, use_version: int = 1): ...
def to_dict(self) -> providers.ConfigDictType: ...
@dataclass
class _BentoMLContainerClass:
config = providers.Configuration()
model_aliases = providers.Static({})
# SingletonFactory providers
bentoml_home: str
bento_store: BentoStore
model_store: ModelStore
tracer_provider: TracerProvider
metrics_client: PrometheusClient
rest_api_client: RestApiClient
bentocloud_client: BentoCloudClient
# ... and more
BentoMLContainer = _BentoMLContainerClass()
Import
from bentoml._internal.configuration.containers import BentoMLContainer
from bentoml._internal.configuration.containers import BentoMLConfiguration
I/O Contract
Inputs
BentoMLConfiguration.__init__()
| Name | Type | Required | Description |
|---|---|---|---|
| override_config_file | str or None | No | Path to a YAML configuration file to override defaults |
| override_config_values | str or None | No | Space-separated key=value config overrides (from env var) |
| override_defaults | dict or None | No | Programmatic dictionary of default overrides |
| override_config_json | dict or None | No | JSON dictionary of configuration overrides |
| validate_schema | bool | No | Whether to validate configuration against schema (default: True) |
| use_version | int | No | Configuration spec version to use (default: 1) |
Outputs
| Name | Type | Description |
|---|---|---|
| BentoMLContainer.bentoml_home | str | The root BentoML home directory path (default: ~/bentoml) |
| BentoMLContainer.bento_store | BentoStore | Singleton BentoStore for managing local bentos |
| BentoMLContainer.model_store | ModelStore | Singleton ModelStore for managing local models |
| BentoMLContainer.tracer_provider | TracerProvider | OpenTelemetry tracer provider configured per settings |
| BentoMLContainer.metrics_client | PrometheusClient | Prometheus metrics client |
| BentoMLContainer.rest_api_client | RestApiClient | REST API client for BentoCloud |
| BentoMLContainer.config | Configuration | The merged configuration dictionary |
Usage Examples
from bentoml._internal.configuration.containers import BentoMLContainer
# Access configuration values
api_server_config = BentoMLContainer.api_server_config
# Get the model store
model_store = BentoMLContainer.model_store.get()
# Use in dependency injection
from simple_di import Provide, inject
@inject
def my_function(model_store=Provide[BentoMLContainer.model_store]):
models = model_store.list()
return models
# Access bentoml home directory
home = BentoMLContainer.bentoml_home.get()