{"record":{"id":"929af450da120483","repo":"BoundaryML/baml","slug":"cannotunpack","errorCode":"CannotUnpack","errorMessage":"Media variable had unrecognizable data: {media_data}","messagePattern":"Media variable had unrecognizable data: (.+?)","errorType":"exception","errorClass":"minijinja::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/jinja-runtime/src/lib.rs","lineNumber":443,"sourceCode":"                }\n            }\n        } else if role.is_none() && chunk.is_empty() {\n            // If there's only whitespace before the first `_.chat()` directive, we discard that chunk\n        } else {\n            let mut parts = vec![];\n            for part in chunk.split(MAGIC_MEDIA_DELIMITER) {\n                let part = if part.starts_with(\":baml-start-media:\")\n                    && part.ends_with(\":baml-end-media:\")\n                {\n                    let media_data = part\n                        .strip_prefix(\":baml-start-media:\")\n                        .unwrap_or(part)\n                        .strip_suffix(\":baml-end-media:\")\n                        .unwrap_or(part);\n\n                    match serde_json::from_str::<BamlMedia>(media_data) {\n                        Ok(m) => Some(ChatMessagePart::Media(m)),\n                        Err(_) => Err(minijinja::Error::new(\n                            ErrorKind::CannotUnpack,\n                            format!(\"Media variable had unrecognizable data: {media_data}\"),\n                        ))?,\n                    }\n                } else if !part.trim().is_empty() {\n                    Some(ChatMessagePart::Text(part.trim().to_string()))\n                } else {\n                    None\n                };\n\n                if let Some(part) = part {\n                    if let Some(meta) = &meta {\n                        parts.push(part.with_meta(meta.clone()));\n                    } else {\n                        parts.push(part);\n                    }\n                }\n            }","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/jinja-runtime/src/lib.rs#L425-L461","documentation":"BAML passes media (images/audio/etc.) through chat templates using special `:baml-media:`-prefixed, `:baml-end-media:`-suffixed string markers. When splitting message parts, each media marker's payload must parse as a BamlMedia JSON object; if serde_json::from_str::<BamlMedia> fails, the raw payload cannot be interpreted as media and this CannotUnpack error is thrown.","triggerScenarios":"A media variable's marker data is corrupted or truncated before rendering — e.g. the value was string-transformed (trimmed, escaped, re-serialized) between BAML inserting the marker and minijinja rendering it, or an object that isn't a BamlMedia was embedded with the media marker.","commonSituations":"Applying string filters (upper, trim, replace) to an image variable, which mangles the marker JSON; passing a non-media value where an image is expected; BAML version mismatches changing the internal marker encoding.","solutions":["Pass media variables to the template unmodified — never apply string filters or transformations to image/audio parameters.","Verify the value is an actual BAML media object (created via image(...)/audio(...) helpers) rather than an arbitrary object.","Print/log the media_data in the error to check for truncation or mangling; fix whatever altered the string.","Align BAML client and runtime versions if the marker format changed between releases."],"exampleFix":"// before\n{{ image_var | upper }}\n\n// after\n{{ image_var }}","handlingStrategy":"type-guard","validationCode":"// ensure the value is an unmodified BAML media object before templating\nif (typeof mediaVar === \"string\" && !mediaVar.includes(\":baml-media:\")) throw new Error(\"expected raw BAML media variable\");","typeGuard":"function isBamlMedia(v) { return v && typeof v === \"object\" && typeof v.kind === \"string\"; }","tryCatchPattern":"match serde_json::from_str::<BamlMedia>(media_data) { Ok(m) => ..., Err(e) => log::error!(\"media marker corrupt: {media_data}\") }","preventionTips":["Never apply string filters (upper/trim/replace) to media variables in templates.","Create media with BAML's image()/audio() helpers only.","Keep BAML client/runtime versions aligned so marker encodings match."],"tags":["jinja","media","deserialization","rust"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}