Implementation:Pytorch Serve Get Yaml Config
Overview
get_yaml_config is a utility function in TorchServe that reads a YAML configuration file from disk and returns its contents as a Python dictionary. It is the entry point for loading model-level declarative configuration, consumed during Service.__init__ to populate the Context.model_yaml_config attribute that handlers use for initialization.
| Field | Value |
|---|---|
| Implementation Name | Get Yaml Config |
| Type | API Doc |
| Workflow | Model_Deployment |
| Domains | Configuration, Model_Serving |
| Knowledge Sources | TorchServe |
| Last Updated | 2026-02-13 00:00 GMT |
Description
The get_yaml_config function is a thin wrapper around PyYAML's yaml.safe_load(). It opens the specified YAML file, parses it safely (preventing arbitrary code execution that yaml.load() allows), and returns the parsed content as a dictionary. The function is intentionally simple, following the Unix philosophy of doing one thing well.
Where It Is Called
The function is consumed in the following locations:
Service.__init__(ts/service.py): When a model manifest contains aconfigFileentry,Service.__init__callsget_yaml_config()to load the configuration and passes it to theContextconstructor.BaseHandler.initialize(ts/torch_handler/base_handler.py): The handler accesses the parsed config throughcontext.model_yaml_configto readpt2compilation options and custom handler settings.
Safety
The function uses yaml.safe_load() rather than yaml.load(), which prevents YAML deserialization attacks. This is important because model archives may come from untrusted sources and the configuration file is parsed automatically during model loading.
Usage
from ts.utils.util import get_yaml_config
config = get_yaml_config("/path/to/model_config.yaml")
# config is a plain Python dict
print(config.get("batchSize")) # e.g., 8
print(config.get("pt2", {})) # e.g., {"compile": {"enable": True, "backend": "inductor"}}
Code Reference
Source Location
| File | Lines | Repository |
|---|---|---|
ts/utils/util.py |
L136-140 | pytorch/serve |
Signature
def get_yaml_config(yaml_file_path: str) -> dict:
"""
Read a YAML configuration file and return its contents as a dictionary.
Args:
yaml_file_path (str): Absolute or relative path to the YAML file.
Returns:
dict: Parsed YAML content. Returns an empty dict if the file
contains no valid YAML mappings.
"""
config = {}
with open(yaml_file_path, "r") as file:
config = yaml.safe_load(file)
return config
Import
from ts.utils.util import get_yaml_config
I/O Contract
| Parameter | Type | Required | Description |
|---|---|---|---|
yaml_file_path |
str |
Yes | Path to the YAML configuration file on disk |
| Return | Type | Description |
|---|---|---|
config |
dict |
Parsed YAML content as a Python dictionary |
Error Conditions
| Error | Condition |
|---|---|
FileNotFoundError |
The specified yaml_file_path does not exist
|
yaml.YAMLError |
The file contains invalid YAML syntax |
PermissionError |
The process does not have read permissions for the file |
Usage Examples
Example 1: Loading model configuration in Service
This shows how get_yaml_config is called internally by Service.__init__:
# From ts/service.py - Service.__init__
import os
from ts.utils.util import get_yaml_config
class Service(object):
def __init__(self, model_name, model_dir, manifest, entry_point,
gpu, batch_size, limit_max_image_pixels=True, metrics_cache=None):
model_yaml_config = {}
if manifest is not None and "model" in manifest:
model = manifest["model"]
if "configFile" in model:
model_yaml_config_file = model["configFile"]
model_yaml_config = get_yaml_config(
os.path.join(model_dir, model_yaml_config_file)
)
# model_yaml_config is then passed to Context(...)
Example 2: Reading torch.compile configuration
from ts.utils.util import get_yaml_config
config = get_yaml_config("model_config.yaml")
# Check for torch.compile settings
pt2_config = config.get("pt2", {})
if "compile" in pt2_config:
compile_opts = pt2_config["compile"]
if compile_opts.get("enable", False):
backend = compile_opts.get("backend", "inductor")
print(f"torch.compile enabled with backend: {backend}")
Example 3: Reading custom handler parameters
# model_config.yaml
handler:
model_name: "bert-base-uncased"
max_length: 512
do_lower_case: true
num_labels: 3
from ts.utils.util import get_yaml_config
config = get_yaml_config("model_config.yaml")
handler_config = config.get("handler", {})
model_name = handler_config.get("model_name") # "bert-base-uncased"
max_length = handler_config.get("max_length", 128) # 512
num_labels = handler_config.get("num_labels", 2) # 3
Related Pages
- Principle:Pytorch_Serve_Model_Artifact_Configuration - The declarative configuration principle this function implements
- Implementation:Pytorch_Serve_BaseHandler - The handler that consumes YAML config during
initialize() - Implementation:Pytorch_Serve_Service_Predict - The
Serviceclass that callsget_yaml_config()in its constructor - Implementation:Pytorch_Serve_Generate_Model_Archive - Archives the config file into the
.mar