nautechsystems/nautilus_trader · error · anyhow::Error

Failed to create config instance. Tried kwargs: {kwargs_err}

Error message

Failed to create config instance. Tried kwargs: {kwargs_err}, default: {default_err}

What it means

create_config_instance attempts two construction paths: kwargs passed to the class constructor, then a default instance with attributes set. If BOTH fail, it bails with a combined message showing each path's error, so the developer can see why kwargs construction and default construction both failed.

Source

Thrown at crates/backtest/src/python/node.rs:562

                    log::debug!("Created default config instance, setting attributes");
                    for (key, value) in config {
                        let py_value = config_value_to_py(py, key, value)?;

                        if let Err(setattr_err) = instance.setattr(key, py_value) {
                            anyhow::bail!("Failed to set attribute {key}: {setattr_err}");
                        }
                    }

                    // Only call __post_init__ if it exists (setattr path
                    // needs it, kwargs path already triggered it via __init__)
                    if instance.hasattr("__post_init__")? {
                        instance.call_method0("__post_init__")?;
                    }

                    instance
                }
                Err(default_err) => {
                    anyhow::bail!(
                        "Failed to create config instance. \
                         Tried kwargs: {kwargs_err}, default: {default_err}"
                    );
                }
            }
        }
    };

    log::debug!("Created config instance: {config_instance:?}");

    Ok(Some(config_instance))
}

fn config_value_to_py<'py>(
    py: Python<'py>,
    key: &str,
    value: &serde_json::Value,
) -> anyhow::Result<Bound<'py, PyAny>> {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read both sub-errors in the message: fix the kwargs_err first — supply all required constructor arguments
  2. Verify the target class is a valid NautilusTrader Config dataclass with defaults for all fields
  3. Check for version drift between your saved config and the installed nautilus_trader package
  4. Construct the config in Python directly to get a clearer traceback, then adjust your serialized config

Example fix

// before
"config_path": "my.module:MyConfig"  # MyConfig requires 'strategy_id' with no default
// after
config["strategy_id"] = "S-001"  # or give MyConfig.strategy_id a default in Python
Defensive patterns

Strategy: validation

Validate before calling

import dataclasses, inspect
def validate_config_constructible(config_cls, config: dict):
    missing = [f.name for f in dataclasses.fields(config_cls)
               if f.default is dataclasses.MISSING and f.default_factory is dataclasses.MISSING
               and f.name not in config]
    if missing:
        raise ValueError(f"{config_cls.__name__} missing required fields: {missing}")
    if not inspect.isclass(config_cls):
        raise ValueError("config_path did not resolve to a class")

Try / catch

try:
    instance = create_config_instance(config_cls, config)
except Exception as e:
    # message contains both kwargs_err and default_err
    log.error("config construction failed (both paths): %s", e)

Prevention

When it happens

Trigger: The config class import succeeds but the class cannot be constructed: required __init__ kwargs missing (kwargs_err) AND default construction fails, e.g. dataclass has required fields without defaults (default_err).

Common situations: Pointing config_path at a class that is not a NautilusTrader Config dataclass; version mismatch where the class gained a new required field; wrong class name importing an abstract base config.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/c5703f4e00c0ba40. Report an issue: GitHub.