{"record":{"id":"1a28f47e296f34ea","repo":"dbt-labs/dbt-core","slug":"failed-to-convert-value-to-yaml-err","errorCode":null,"errorMessage":"Failed to convert value to YAML: {err}","messagePattern":"Failed to convert value to YAML: (.+?)","errorType":"exception","errorClass":"InvalidOperation","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/functions/base.rs","lineNumber":577,"sourceCode":"    let sort_keys = iter\n        .next_kwarg::<Option<bool>>(\"sort_keys\")?\n        .unwrap_or(false);\n\n    // If the value is undefined or none and there's a default, return it\n    if value.is_undefined() || value.is_none() {\n        return match default {\n            Some(def) => Ok(def.clone()),\n            // Return none for undefined/none values when no default is provided\n            None => Ok(Value::from(())),\n        };\n    }\n\n    // Convert the Minijinja Value to a serde_json::Value\n    // Should this say YAML or JSON cause this is toyaml function, not sure\n    let mut json_value = match serde_json::to_value(value) {\n        Ok(val) => val,\n        Err(err) => {\n            return Err(Error::new(\n                ErrorKind::InvalidOperation,\n                format!(\"Failed to convert value to YAML: {err}\"),\n            ));\n        }\n    };\n\n    // Sort keys if requested\n    if sort_keys && let Some(obj) = json_value.as_object_mut() {\n        let sorted_map: BTreeMap<_, _> = obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect();\n        *obj = sorted_map.into_iter().collect();\n    }\n\n    match dbt_yaml::to_string(&json_value) {\n        Ok(yaml_str) => Ok(Value::from(yaml_str)),\n        Err(err) => Err(Error::new(\n            ErrorKind::InvalidOperation,\n            format!(\"Failed to convert value to YAML: {err}\"),\n        )),","sourceCodeStart":559,"sourceCodeEnd":595,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/functions/base.rs#L559-L595","documentation":"The toyaml Jinja filter first converts its input Minijinja Value into a serde_json::Value as an intermediate representation. If that conversion fails, this InvalidOperation error is raised (the message says YAML but the failure is in the JSON conversion step).","triggerScenarios":"{{ value | toyaml }} where the Minijinja value cannot be converted to a serde_json value (unsupported/opaque types, cyclic or non-representable structures).","commonSituations":"Piping undefined variables or objects (e.g. relations, macros results) into toyaml; templates expecting implicit coercion of exotic types.","solutions":["Ensure the input is plain data (mappings, sequences, scalars) before piping to toyaml.","Guard the variable with is defined / is mapping before conversion.","Serialize only plain attributes of complex objects.","Read the inner err message to identify which value component failed conversion."],"exampleFix":"// before\n{{ undefined_var | toyaml }}\n// after\n{% if undefined_var is defined %}{{ undefined_var | toyaml }}{% endif %}","handlingStrategy":"validation","validationCode":"{% if value is not defined %}{% do exceptions.raise_compiler_error('toyaml input undefined') %}{% endif %}","typeGuard":null,"tryCatchPattern":"{% if value is defined and value is mapping %}{{ value | toyaml }}{% else %}{}{% endif %}","preventionTips":["Pipe only mappings/sequences/scalars into toyaml.","Guard with is defined / is mapping before rendering.","Do not serialize dbt objects (relations, macros) directly."],"tags":["jinja","yaml","serialization","toyaml"],"backgroundTag":"json-marshal-failed","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"}