{"id":"45d135abf0b3eb20","repo":"gohugoio/hugo","slug":"failed-to-detect-format-from-content","errorCode":null,"errorMessage":"failed to detect format from content","messagePattern":"failed to detect format from content","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/transform/remarshal.go","lineNumber":44,"sourceCode":"\t\treturn \"\", err\n\t}\n\n\tif m, ok := data.(map[string]any); ok {\n\t\tmeta = m\n\t} else {\n\t\tfrom, err := cast.ToStringE(data)\n\t\tif err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\n\t\tfrom = strings.TrimSpace(from)\n\t\tif from == \"\" {\n\t\t\treturn \"\", nil\n\t\t}\n\n\t\tfromFormat := metadecoders.Default.FormatFromContentString(from)\n\t\tif fromFormat == \"\" {\n\t\t\treturn \"\", errors.New(\"failed to detect format from content\")\n\t\t}\n\n\t\tmeta, err = metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)\n\t\tif err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\t}\n\n\t// Make it so 1.0 float64 prints as 1 etc.\n\tapplyMarshalTypes(meta)\n\n\tvar result bytes.Buffer\n\tif err := parser.InterfaceToConfig(meta, mark, &result); err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn result.String(), nil\n}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/transform/remarshal.go#L26-L62","documentation":"The `transform.Remarshal` template function converts config snippets between JSON, YAML and TOML. When given a string instead of a map, it sniffs the source format with `FormatFromContentString`; if the content doesn't look like any supported format, detection returns empty and this error is raised (tpl/transform/remarshal.go:42-45). It exists because Remarshal has no explicit source-format parameter — it must infer it.","triggerScenarios":"Calling `{{ transform.Remarshal \"toml\" $str }}` (or the `remarshal` alias) with a string whose content is not recognizable as JSON, YAML, TOML or XML — e.g. plain prose, a bare scalar like `foo`, a broken snippet missing its `key = value`/`key: value` structure, or CSV data.","commonSituations":"Docs sites piping arbitrary file contents through remarshal; passing a file path instead of the file's contents; templating that accidentally produces an empty-ish or truncated config string; YAML that lost its `key:` colons during copy-paste.","solutions":["Verify the string you pass is a complete, well-formed JSON/YAML/TOML document (top-level key/value structure, not a bare scalar).","If the data is already a map (e.g. from `dict` or `.Params`), pass the map directly — format detection is skipped for maps.","Print the value with `{{ warnf \"%q\" $str }}` to confirm you're passing content, not a path or empty string.","Use `transform.Unmarshal` with an explicit decode if you control the source format, then Remarshal the resulting map."],"exampleFix":"<!-- before: bare scalar, no detectable format -->\n{{ transform.Remarshal \"toml\" \"hello\" }}\n<!-- after: valid YAML input -->\n{{ transform.Remarshal \"toml\" \"greeting: hello\" }}","handlingStrategy":"validation","validationCode":"{{ $raw := .content }}\n{{ if not (strings.TrimSpace $raw) }}\n  {{ errorf \"empty content passed to transform.Remarshal\" }}\n{{ end }}\n{{/* ensure content actually parses as one of TOML/YAML/JSON/XML */}}\n{{ $parsed := $raw | transform.Unmarshal }}","typeGuard":null,"tryCatchPattern":"{{ with try (transform.Remarshal \"yaml\" $raw) }}\n  {{ with .Err }}{{ warnf \"remarshal failed: %s\" . }}{{ else }}{{ .Value }}{{ end }}\n{{ end }}","preventionTips":["Only pass strings that are valid JSON, YAML, TOML, or XML — arbitrary text has no detectable format","Trim BOMs/leading garbage; format sniffing depends on the first meaningful bytes","Test remarshal calls with representative real content, not just happy-path samples"],"tags":["hugo","templates","transform","yaml","toml","json"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}