{"record":{"id":"ba9c245e4d8ee06e","repo":"dbt-labs/dbt-core","slug":"failed-to-convert-payload-to-string","errorCode":null,"errorMessage":"Failed to convert payload to string","messagePattern":"Failed to convert payload to string","errorType":"exception","errorClass":"minijinja::Error (InvalidOperation)","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/phases/run/run_node_context.rs","lineNumber":700,"sourceCode":"impl Object for WriteConfig {\n    fn call(\n        self: &Arc<Self>,\n        _state: &State<'_, '_>,\n        args: &[MinijinjaValue],\n        _listeners: &[Rc<dyn RenderingEventListener>],\n    ) -> Result<MinijinjaValue, Error> {\n        if args.is_empty() {\n            return Err(Error::new(\n                ErrorKind::InvalidOperation,\n                \"write function requires payload argument\".to_string(),\n            ));\n        }\n\n        // Extract payload from args\n        let payload = match args[0].as_str() {\n            Some(s) => s,\n            None => {\n                return Err(Error::new(\n                    ErrorKind::InvalidOperation,\n                    \"Failed to convert payload to string\".to_string(),\n                ));\n            }\n        };\n\n        // Write the file\n        match write_file(&self.run_file_path, &self.resource_type, payload) {\n            Ok(_) => {}\n            Err(e) => {\n                return Err(Error::new(\n                    ErrorKind::InvalidOperation,\n                    format!(\"Failed to write file: {e}\"),\n                ));\n            }\n        }\n\n        // Return empty string on success","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/phases/run/run_node_context.rs#L682-L718","documentation":"After checking arity, write() converts the first argument with as_str(); if the payload is not a string (e.g. a dict, list, or number value from Jinja), conversion fails and this error is raised instead of writing garbage to disk.","triggerScenarios":"Calling write() with a non-string first argument, e.g. {{ write({'key': 'value'}) }} or {{ write(my_list) }}, or a numeric value without |string.","commonSituations":"Passing structured data (dicts/lists) straight to write instead of rendering it; forgetting the |tojson or |string filter; macro returning an object rather than text.","solutions":["Stringify the payload before calling: {{ write(payload | tojson) }} for structured data.","Use {{ write(payload | string) }} for simple values.","Inspect the payload type with |tojson or a type test to confirm what you are passing."],"exampleFix":"-- before\n{{ write({'status': 'ok'}) }}\n-- after\n{{ write({'status': 'ok'} | tojson) }}","handlingStrategy":"type-guard","validationCode":"-- Jinja\n{% if payload is not string %}\n  {{ write(payload | tojson) }}\n{% else %}\n  {{ write(payload) }}\n{% endif %}","typeGuard":"-- Jinja: only call write when payload is a string\n{% if payload is string %}{{ write(payload) }}{% endif %}","tryCatchPattern":"match args[0].as_str() {\n    Some(s) => write_file(&path, &rt, s)?,\n    None => Err(Error::new(ErrorKind::InvalidOperation, \"payload must be a string; use |tojson or |string\")),\n}","preventionTips":["Apply |tojson to any dict/list payloads and |string to scalars before write().","Never pass raw macro return objects to write().","Add a Jinja test covering non-string payloads for write()."],"tags":["type-mismatch","jinja","file-write","dbt"],"backgroundTag":"type-mismatch","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"}