> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/automodel/_mcp/server.

# nemo_automodel.components.config.loader

## Module Contents

### Classes

| Name                                                                      | Description                                                                     |
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| [`ConfigNode`](#nemo_automodel-components-config-loader-ConfigNode)       | A configuration node that wraps a dictionary (or parts of it) from a YAML file. |
| [`_OrigValueStr`](#nemo_automodel-components-config-loader-_OrigValueStr) | String that keeps its original (unresolved) value for safe printing.            |

### Functions

| Name                                                                                          | Description                                                             |
| --------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [`_is_allowed_module`](#nemo_automodel-components-config-loader-_is_allowed_module)           | Return True if a module is safe/allowed to import.                      |
| [`_is_safe_attr`](#nemo_automodel-components-config-loader-_is_safe_attr)                     | -                                                                       |
| [`_is_safe_path`](#nemo_automodel-components-config-loader-_is_safe_path)                     | -                                                                       |
| [`_redact`](#nemo_automodel-components-config-loader-_redact)                                 | -                                                                       |
| [`_resolve_target`](#nemo_automodel-components-config-loader-_resolve_target)                 | Resolve a dotted path to a Python object with safety checks.            |
| [`config_to_yaml_str`](#nemo_automodel-components-config-loader-config_to_yaml_str)           | Convert a config object to a YAML string suitable for logging/printing. |
| [`load_module_from_file`](#nemo_automodel-components-config-loader-load_module_from_file)     | Dynamically imports a module from a given file path.                    |
| [`load_yaml_config`](#nemo_automodel-components-config-loader-load_yaml_config)               | Load a YAML configuration file and convert it to a ConfigNode.          |
| [`resolve_yaml_env_vars`](#nemo_automodel-components-config-loader-resolve_yaml_env_vars)     | Resolve env var references inside a YAML-loaded container.              |
| [`set_enable_user_modules`](#nemo_automodel-components-config-loader-set_enable_user_modules) | Enable or disable loading user-defined code at runtime.                 |
| [`translate_value`](#nemo_automodel-components-config-loader-translate_value)                 | Convert a string token into the corresponding Python object.            |

### Data

[`ALLOWED_IMPORT_PREFIXES`](#nemo_automodel-components-config-loader-ALLOWED_IMPORT_PREFIXES)

[`ENABLE_USER_MODULES`](#nemo_automodel-components-config-loader-ENABLE_USER_MODULES)

[`SAFE_BASE_DIR`](#nemo_automodel-components-config-loader-SAFE_BASE_DIR)

[`SENSITIVE_KEY_SUBSTRINGS`](#nemo_automodel-components-config-loader-SENSITIVE_KEY_SUBSTRINGS)

### API

```python
class nemo_automodel.components.config.loader.ConfigNode(
    d: dict[str, typing.Any],
    raise_on_missing_attr: bool = True
)
```

A configuration node that wraps a dictionary (or parts of it) from a YAML file.

This class allows nested dictionaries and lists to be accessed as attributes and
provides functionality to instantiate objects from configuration.

Get the raw configuration dictionary.

```python
nemo_automodel.components.config.loader.ConfigNode.__contains__(
    key: object
) -> bool
```

Check if a dotted key exists in the configuration.

**Parameters:**

The dotted key to check.

**Returns:** `bool`

True if the key exists, False otherwise.

```python
nemo_automodel.components.config.loader.ConfigNode.__getattr__(
    key: str
) -> typing.Any
```

```python
nemo_automodel.components.config.loader.ConfigNode.__repr__() -> str
```

```python
nemo_automodel.components.config.loader.ConfigNode.__str__() -> str
```

```python
nemo_automodel.components.config.loader.ConfigNode._format(
    level: int = 0,
    use_orig_values: bool = True
) -> str
```

Return a string representation of the configuration node with indentation.

**Parameters:**

The current indentation level.

If True, prefer original placeholder strings (e.g. `$&#123;VAR&#125;`)
stored on `_OrigValueStr` values for safe logging.

```python
nemo_automodel.components.config.loader.ConfigNode._instantiate_value(
    v: typing.Any
) -> typing.Any
```

Recursively instantiate configuration values.

**Parameters:**

The configuration value.

**Returns:** `Any`

The instantiated value.

```python
nemo_automodel.components.config.loader.ConfigNode._repr_value(
    value: typing.Any,
    level: int,
    use_orig_values: bool = True
) -> str
```

```python
nemo_automodel.components.config.loader.ConfigNode._to_dotted_path(
    obj: typing.Any
) -> str
```

Convert a callable/class/method object to a dotted path string.

Best-effort normalization for a few common cases to produce concise, user-friendly paths.

```python
nemo_automodel.components.config.loader.ConfigNode._unwrap(
    v: typing.Any
) -> typing.Any
```

Recursively convert wrapped configuration values to basic Python types.

**Parameters:**

The configuration value.

**Returns:** `Any`

The unwrapped value.

```python
nemo_automodel.components.config.loader.ConfigNode._wrap(
    k: str,
    v: typing.Any
) -> typing.Any
```

Wrap a configuration value based on its type.

**Parameters:**

The key corresponding to the value.

The value to be wrapped.

**Returns:** `Any`

The wrapped value.

```python
nemo_automodel.components.config.loader.ConfigNode.get(
    key: str,
    default: typing.Any = None
) -> typing.Any
```

Retrieve a configuration value using a dotted key.

If any component of the path is missing, returns the specified default value.

**Parameters:**

The dotted path key.

A default value to return if the key is not found.

**Returns:** `Any`

The configuration value or the default value.

```python
nemo_automodel.components.config.loader.ConfigNode.get_as_string(
    key: str,
    default: str | None = None
) -> str
```

Get the string representation of a configuration value.

If the value is a function or class (resolved from an import path),
returns the original import path string. Otherwise returns the value
as a string.

**Parameters:**

The key to look up.

**Returns:** `str`

The string representation of the value, or None if key not found.

```python
nemo_automodel.components.config.loader.ConfigNode.instantiate(
    args: typing.Any = (),
    kwargs: typing.Any = {}
) -> typing.Any
```

Instantiate the target object specified in the configuration.

This method looks for the "*target*" attribute in the configuration and resolves
it to a callable function or class which is then instantiated.

**Parameters:**

Positional arguments for the target instantiation.

Keyword arguments to override or add to the configuration values.

**Returns:** `Any`

The instantiated object.

**Raises:**

* `AttributeError`: If no "*target*" attribute is found in the configuration.

```python
nemo_automodel.components.config.loader.ConfigNode.instantiate_path(
    dotted_path: str,
    default: typing.Any = None,
    args: typing.Any = (),
    kwargs: typing.Any = {}
) -> typing.Any
```

Instantiate the target object specified in the configuration by path.

If the path is not found, returns the default value.

For example, this is useful when you want to do something like:

cfg\_peft = self.cfg.get("peft", None)
if cfg\_peft is not None:
cfg\_peft = cfg\_peft.instantiate()

In this case, you first check if the dotted path (in this case "peft") is in the configuration.
If it is, you instantiate it, otherwise you return the default value (in this case None).

With instantiate\_path, you can do:
cfg\_peft = self.cfg.instantiate\_path("peft", default=None)

**Parameters:**

The path to the target object (e.g., "model.config").

A default value to return if the path is not found.

Positional arguments for the target instantiation.

Keyword arguments to override or add to the configuration values.

**Returns:** `Any`

The instantiated object.

```python
nemo_automodel.components.config.loader.ConfigNode.set_by_dotted(
    dotted_key: str,
    value: typing.Any
) -> None
```

Set (or append) a value in the config using a dotted key.

e.g. set\_by\_dotted("foo.bar.abc", 1) will ensure self.foo.bar.abc == 1

```python
nemo_automodel.components.config.loader.ConfigNode.to_dict() -> dict[str, typing.Any]
```

Convert the configuration node back to a dictionary.

**Returns:** `dict[str, Any]`

A dictionary representation of the configuration node.

```python
nemo_automodel.components.config.loader.ConfigNode.to_yaml_dict(
    resolve_env: bool = False,
    redact_sensitive: bool = False,
    use_orig_values: bool = False
) -> dict[str, typing.Any]
```

Convert configuration to a YAML-ready dictionary:

* Preserves typed scalars (ints, floats, bools)
* Converts callables/classes/methods (e.g., *target*, \*\_fn) to dotted path strings
* Recurses through nested ConfigNodes and lists

**Parameters:**

If True, resolve `$&#123;oc.env:VAR&#125;` interpolations in the returned dict.
This does not mutate the in-memory config.

If True, redact values for keys that look sensitive (token/secret/etc).

If True, prefer `_orig_value` (when present) for safe printing/logging.

```python
class nemo_automodel.components.config.loader._OrigValueStr()
```

**Bases:** `str`

String that keeps its original (unresolved) value for safe printing.

```python
nemo_automodel.components.config.loader._OrigValueStr.__new__(
    value: str,
    orig_value: str
) -> nemo_automodel.components.config.loader._OrigValueStr
```

```python
nemo_automodel.components.config.loader._is_allowed_module(
    module_name: str
) -> bool
```

Return True if a module is safe/allowed to import.

Security policy (balanced for functionality and tests):

* If user modules are explicitly enabled, allow everything.
* Always allow modules that are already imported in this process.
* Allow modules that are importable from the current PYTHONPATH/sys.path.
  This keeps behavior intuitive for local/test modules while still blocking
  truly unknown targets.
* Fallback to explicit allowlist as a final gate (mostly relevant if
  find\_spec returns None for the top-level name).

```python
nemo_automodel.components.config.loader._is_safe_attr(
    name: str
) -> bool
```

```python
nemo_automodel.components.config.loader._is_safe_path(
    p: pathlib.Path
) -> bool
```

```python
nemo_automodel.components.config.loader._redact(
    obj: typing.Any
) -> typing.Any
```

```python
nemo_automodel.components.config.loader._resolve_target(
    dotted_path: str
) -> typing.Any
```

Resolve a dotted path to a Python object with safety checks.

```python
nemo_automodel.components.config.loader.config_to_yaml_str(
    cfg_obj: typing.Any,
    use_orig_values: bool = True
) -> str
```

Convert a config object to a YAML string suitable for logging/printing.

Uses original placeholder strings (e.g. `$&#123;VAR&#125;`) and original *target*/\*\_fn
strings when use\_orig\_values is True; never includes internal keys like
\_original\_strings. If cfg\_obj is a ConfigNode, uses to\_yaml\_dict(); otherwise
falls back to to\_dict() or a plain dict.

```python
nemo_automodel.components.config.loader.load_module_from_file(
    file_path: str | pathlib.Path
) -> types.ModuleType
```

Dynamically imports a module from a given file path.

Intentionally permissive to support test/temporary modules. Caller is
responsible for any higher-level policy checks.

```python
nemo_automodel.components.config.loader.load_yaml_config(
    path: str | pathlib.Path
) -> nemo_automodel.components.config.loader.ConfigNode
```

Load a YAML configuration file and convert it to a ConfigNode.

**Parameters:**

The path to the YAML configuration file.

**Returns:** `ConfigNode`

A configuration node representing the YAML file.

```python
nemo_automodel.components.config.loader.resolve_yaml_env_vars(
    obj: typing.Any
) -> typing.Any
```

Resolve env var references inside a YAML-loaded container.

Supported forms inside strings:

* `$&#123;VAR&#125;` / `$&#123;VAR,default&#125;`
* `$&#123;var.dot.var&#125;` (dots are treated as part of the env var name)
* `$VAR` / `$var.dot.var`
* Back-compat: `$&#123;oc.env:VAR&#125;` / `$&#123;oc.env:VAR,default&#125;`

```python
nemo_automodel.components.config.loader.set_enable_user_modules(
    allow: bool
) -> None
```

Enable or disable loading user-defined code at runtime.

Users can also set environment variable NEMO\_ENABLE\_USER\_MODULES=1 to enable.

```python
nemo_automodel.components.config.loader.translate_value(
    v: typing.Any
) -> typing.Any
```

Convert a string token into the corresponding Python object.

This function first checks for a handful of special symbols (None/true/false),
then falls back to `ast.literal_eval`, and finally to returning the original
string if parsing fails.

**Parameters:**

The raw string value to translate.

**Returns:** `Any`

The translated Python value, which may be:

* None, True, or False for the special symbols
* an int, float, tuple, list, dict, etc. if `ast.literal_eval` succeeds
* the original string `v` if all parsing attempts fail

```python
nemo_automodel.components.config.loader.ALLOWED_IMPORT_PREFIXES = ('nemo_automodel', 'torch', 'transformers', 'torchdata', 'torchao', 'liger_kerne...
```

```python
nemo_automodel.components.config.loader.ENABLE_USER_MODULES = os.environ.get('NEMO_ENABLE_USER_MODULES', '').lower() in ('1', 'true', 'yes')
```

```python
nemo_automodel.components.config.loader.SAFE_BASE_DIR = Path(__file__).resolve().parents[2]
```

```python
nemo_automodel.components.config.loader.SENSITIVE_KEY_SUBSTRINGS = ('password', 'secret', 'token', 'apikey', 'api_key', 'authorization', 'auth')
```