{"record":{"id":"fab572f03631cd3e","repo":"nautechsystems/nautilus_trader","slug":"failed-to-create-config-instance-tried-kwargs-k-fab572","errorCode":null,"errorMessage":"Failed to create config instance. Tried kwargs: {kwargs_err}, default: {default_err}","messagePattern":"Failed to create config instance\\. Tried kwargs: (.+?), default: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/python/registration.rs","lineNumber":609,"sourceCode":"        Ok(instance) => instance,\n        Err(kwargs_err) => match config_class.call0() {\n            Ok(instance) => {\n                for (key, value) in config {\n                    let py_value = config_value_to_py(py, key, value)?;\n\n                    if let Err(setattr_err) = instance.setattr(key, py_value) {\n                        anyhow::bail!(\"Failed to set attribute {key}: {setattr_err}\");\n                    }\n                }\n\n                if instance.hasattr(\"__post_init__\")? {\n                    instance.call_method0(\"__post_init__\")?;\n                }\n\n                instance\n            }\n            Err(default_err) => {\n                anyhow::bail!(\n                    \"Failed to create config instance. Tried kwargs: {kwargs_err}, default: {default_err}\"\n                );\n            }\n        },\n    };\n\n    Ok(Some(config_instance))\n}\n\nfn config_value_to_py<'py>(\n    py: Python<'py>,\n    key: &str,\n    value: &serde_json::Value,\n) -> anyhow::Result<Bound<'py, PyAny>> {\n    if key == \"actor_id\"\n        && let Some(actor_id) = value.as_str()\n    {\n        return Ok(ActorId::new_checked(actor_id)?","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/python/registration.rs#L591-L627","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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.","If default construction also fails, add defaults (or make fields optional) to the config class so the fallback path can succeed.","Validate the config with the class schema before registration (e.g. inspect __init__/dataclass fields).","Check for renamed config fields after a nautilus upgrade."],"exampleFix":"// before\nconfig = MyStrategyConfig(ma_fast=10, ma_slow_period=50)  # wrong field name\n// after\nconfig = MyStrategyConfig(fast_ma=10, slow_ma=50)","handlingStrategy":"validation","validationCode":"import inspect\nfields = inspect.signature(ConfigCls.__init__).parameters\nbad = [k for k in kwargs if k not in fields]\nassert not bad, f\"Unknown config kwargs: {bad}\"\nassert all(k in fields or f.default is not inspect.Parameter.empty\n           for k, f in fields.items() if k != 'self')","typeGuard":"def is_valid_config(cls, kwargs: dict) -> bool:\n    import inspect\n    params = inspect.signature(cls.__init__).parameters\n    return all(k in params for k in kwargs)","tryCatchPattern":"try:\n    strategy = factory.create(config)\nexcept Exception as e:\n    if \"Failed to create config instance\" in str(e):\n        logger.error(\"Config kwargs invalid: %s\", e)\n        raise ValueError(\"Fix strategy/actor config fields\") from e\n    raise","preventionTips":["Match config field names to the dataclass/schema exactly after upgrades","Give config fields defaults so fallback construction can succeed","Validate kwargs against the class signature before registering actors/strategies"],"tags":["python","config","instantiation","registration"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}