YAML loading¶
YAML files can include other files with !include.
YAML loader with support for !include directive.
This module provides a custom YAML loader that extends the standard safe_load functionality with the ability to include external YAML files using the !include tag.
Example
config: !include model_config.yaml backend: !include backends/local.yaml
IncludeLoader
¶
Bases: SafeLoader
Custom YAML loader with support for !include directive.
This loader extends yaml.SafeLoader to add the !include constructor, which allows referencing external YAML files. It tracks the chain of included files to prevent circular dependencies.
Features: - Relative path resolution from the including file's directory - Absolute path support - Circular dependency detection - Environment variable expansion in paths - Maximum include depth limit
Attributes:
| Name | Type | Description |
|---|---|---|
_root_dir |
Directory of the root YAML file being loaded |
|
_include_chain |
Set[Path]
|
Set of file paths already included (for cycle detection) |
_include_depth |
Current nesting depth of includes |
Initialize the loader with stream and include tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
str | IO
|
YAML stream to load from |
required |
include_constructor
¶
include_constructor(
loader: IncludeLoader, node: Node
) -> Any
Constructor function for !include tag.
This function is called by the YAML loader when it encounters an !include tag. It resolves the path, loads the referenced file, and returns its contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
IncludeLoader
|
The IncludeLoader instance |
required |
node
|
Node
|
The YAML node containing the include path |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The loaded content from the included file (dict, list, or scalar) |
Raises:
| Type | Description |
|---|---|
YAMLError
|
If the file cannot be loaded or circular dependency detected |
FileNotFoundError
|
If the included file doesn't exist |
ValueError
|
If include depth exceeds maximum |
load_yaml_with_includes
¶
Load a YAML file with support for !include directives.
This is the main entry point for loading YAML files that may contain !include tags. It uses the custom IncludeLoader to handle includes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
str | Path | IO
|
File path (str/Path) or file-like object to load from |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Loaded YAML content as a dictionary |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file doesn't exist |
YAMLError
|
If the YAML is invalid or includes have errors |
Example
content = load_yaml_with_includes('pipeline.yaml') print(content['config'])
safe_load_with_includes
¶
Load YAML content from a string with include support.
This function is useful when you have YAML content as a string but still want to support includes. You can specify a root directory for resolving relative paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
YAML content as a string |
required |
root_dir
|
Path
|
Directory to use for resolving relative includes (default: cwd) |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Loaded YAML content as a dictionary |
Example
yaml_str = "config: !include model.yaml" content = safe_load_with_includes(yaml_str, Path('/configs'))