gohugoio/hugo · error

failed to detect format from content

Error message

failed to detect format from content

What it means

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.

Source

Thrown at tpl/transform/remarshal.go:44

		return "", err
	}

	if m, ok := data.(map[string]any); ok {
		meta = m
	} else {
		from, err := cast.ToStringE(data)
		if err != nil {
			return "", err
		}

		from = strings.TrimSpace(from)
		if from == "" {
			return "", nil
		}

		fromFormat := metadecoders.Default.FormatFromContentString(from)
		if fromFormat == "" {
			return "", errors.New("failed to detect format from content")
		}

		meta, err = metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
		if err != nil {
			return "", err
		}
	}

	// Make it so 1.0 float64 prints as 1 etc.
	applyMarshalTypes(meta)

	var result bytes.Buffer
	if err := parser.InterfaceToConfig(meta, mark, &result); err != nil {
		return "", err
	}

	return result.String(), nil
}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Verify the string you pass is a complete, well-formed JSON/YAML/TOML document (top-level key/value structure, not a bare scalar).
  2. If the data is already a map (e.g. from `dict` or `.Params`), pass the map directly — format detection is skipped for maps.
  3. Print the value with `{{ warnf "%q" $str }}` to confirm you're passing content, not a path or empty string.
  4. Use `transform.Unmarshal` with an explicit decode if you control the source format, then Remarshal the resulting map.

Example fix

<!-- before: bare scalar, no detectable format -->
{{ transform.Remarshal "toml" "hello" }}
<!-- after: valid YAML input -->
{{ transform.Remarshal "toml" "greeting: hello" }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $raw := .content }}
{{ if not (strings.TrimSpace $raw) }}
  {{ errorf "empty content passed to transform.Remarshal" }}
{{ end }}
{{/* ensure content actually parses as one of TOML/YAML/JSON/XML */}}
{{ $parsed := $raw | transform.Unmarshal }}

Try / catch

{{ with try (transform.Remarshal "yaml" $raw) }}
  {{ with .Err }}{{ warnf "remarshal failed: %s" . }}{{ else }}{{ .Value }}{{ end }}
{{ end }}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/45d135abf0b3eb20.json. Report an issue: GitHub ↗.