nautechsystems/nautilus_trader · 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 builds a Python config object from user kwargs. It first tries to construct the class with the provided kwargs, and on failure retries with a default construction; if BOTH attempts fail it bails with this combined message showing each underlying error. It means the config class could not be instantiated with the given parameters nor with defaults.

Source

Thrown at crates/system/src/python/registration.rs:609

        Ok(instance) => instance,
        Err(kwargs_err) => match config_class.call0() {
            Ok(instance) => {
                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}");
                    }
                }

                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}"
                );
            }
        },
    };

    Ok(Some(config_instance))
}

fn config_value_to_py<'py>(
    py: Python<'py>,
    key: &str,
    value: &serde_json::Value,
) -> anyhow::Result<Bound<'py, PyAny>> {
    if key == "actor_id"
        && let Some(actor_id) = value.as_str()
    {
        return Ok(ActorId::new_checked(actor_id)?

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the kwargs_err part of the message first; fix the offending kwarg name or value in the config dict passed to the actor/strategy factory.
  2. If default construction also fails, add defaults (or make fields optional) to the config class so the fallback path can succeed.
  3. Validate the config with the class schema before registration (e.g. inspect __init__/dataclass fields).
  4. Check for renamed config fields after a nautilus upgrade.

Example fix

// before
config = MyStrategyConfig(ma_fast=10, ma_slow_period=50)  # wrong field name
// after
config = MyStrategyConfig(fast_ma=10, slow_ma=50)
Defensive patterns

Strategy: validation

Validate before calling

import inspect
fields = inspect.signature(ConfigCls.__init__).parameters
bad = [k for k in kwargs if k not in fields]
assert not bad, f"Unknown config kwargs: {bad}"
assert all(k in fields or f.default is not inspect.Parameter.empty
           for k, f in fields.items() if k != 'self')

Type guard

def is_valid_config(cls, kwargs: dict) -> bool:
    import inspect
    params = inspect.signature(cls.__init__).parameters
    return all(k in params for k in kwargs)

Try / catch

try:
    strategy = factory.create(config)
except Exception as e:
    if "Failed to create config instance" in str(e):
        logger.error("Config kwargs invalid: %s", e)
        raise ValueError("Fix strategy/actor config fields") from e
    raise

Prevention

When it happens

Trigger: Calling create_python_actor/create_python_strategy whose config class constructor rejects the kwargs (unknown field, wrong type, missing required __init__ arg) and whose no-arg default construction also fails (required fields without defaults).

Common situations: Typo'd config field names in a TOML/kwargs dict, passing Python kwargs to a Rust-side dataclass that doesn't accept them, config dataclasses with required fields lacking defaults, version drift where config fields were renamed.

Related errors


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