{"record":{"id":"14c95c237145f7db","repo":"dbt-labs/dbt-core","slug":"argument-name-to-var-has-incompatible-type-va","errorCode":null,"errorMessage":"argument 'name' to var() has incompatible type; value is not a string","messagePattern":"argument 'name' to var\\(\\) has incompatible type; value is not a string","errorType":"validation","errorClass":"minijinja::Error (InvalidOperation)","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-vars/src/var.rs","lineNumber":46,"sourceCode":"        // NOTE: Minijinja encodes keyword arguments into `args` as a final map value.\n        // Using `ArgsIter` ensures we correctly support both:\n        // - var(\"name\", \"default\")\n        // - var(\"name\", default=\"default\")\n        // and we don't accidentally treat the kwargs map itself as the default value.\n        let iter = ArgsIter::new(\"var\", &[\"name\", \"default\"], args);\n        // Jinja will happily pass \"undefined\" into functions if the caller writes `var(x)`\n        // and `x` is not defined. Jinja2's Undefined is string-coercible, so dbt-core will\n        // end up treating it like an empty string key and still honor the provided default.\n        //\n        // For compatibility (and to keep tests/fixtures stable), we explicitly coerce\n        // undefined/none to an empty string here instead of raising a type error.\n        let var_name_value = iter.next_arg::<Value>()?;\n        let var_name = if let Some(s) = var_name_value.as_str() {\n            s.to_string()\n        } else if var_name_value.is_undefined() || var_name_value.is_none() {\n            \"\".to_string()\n        } else {\n            return Err(Error::new(\n                ErrorKind::InvalidOperation,\n                \"argument 'name' to var() has incompatible type; value is not a string\",\n            ));\n        };\n\n        // IMPORTANT: ArgsIter's `next_kwarg::<Option<&Value>>` cannot distinguish between:\n        // - kwarg missing\n        // - kwarg present with value `none`\n        //\n        // dbt projects commonly do `var(\"x\", default=None)`, so we need to preserve an\n        // explicit `none` default.\n        //\n        // We therefore parse the kwargs map ourselves (if present), and fall back to\n        // a true positional default only when the 2nd argument is not that kwargs map.\n        let mut default_value: Option<Value> = None;\n\n        // there are two ways for the second argument to be a map:\n        // 1. var(\"x\", {\"a\": 1})","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-vars/src/var.rs#L28-L64","documentation":"The `var()` Jinja function requires its `name` argument to be a string. When the first positional argument is neither a string nor undefined/None, `parse_args` rejects it with this error. Non-string values (numbers, lists, dicts, booleans) are not valid variable names.","triggerScenarios":"Calling `{{ var(123) }}`, `{{ var(some_dict) }}`, or `{{ var(config.some_list) }}` — i.e. the first argument to `var()` evaluates to a non-string, non-None minijinja Value.","commonSituations":"Passing a numeric or boolean var name built from another context value; forgetting quotes so Jinja resolves an identifier instead of a literal string; a `vars` dict lookup producing a non-string key.","solutions":["Quote the variable name: `var('my_var')` instead of `var(my_var)`.","Convert dynamic names to strings before calling: `var(other | string)`.","Ensure the value you intend to pass as a name is actually the key, not the value, of your vars mapping."],"exampleFix":"// before (Jinja)\n{{ var(config.cluster_id) }}\n// after\n{{ var(config.cluster_id | string) }}","handlingStrategy":"type-guard","validationCode":"{% if my_key is string %}{{ var(my_key) }}{% else %}{{ exceptions.raise_compiler_error('var name must be a string') }}{% endif %}","typeGuard":"// Jinja\n{% macro safe_var(name) %}\n  {% if name is not string %}{{ exceptions.raise_compiler_error('var() name must be a string') }}{% endif %}\n  {{ var(name | string) }}\n{% endmacro %}","tryCatchPattern":null,"preventionTips":["Always quote literal var names: var('name')","Apply | string when names are computed dynamically","Keep var keys as strings in --vars / dbt_project.yml"],"tags":["jinja","type-error","var","dbt"],"backgroundTag":"invalid-argument-value","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}