hashicorp/terraform · error

could not marshal output

Error message

could not marshal output %s type: %w

What it means

Thrown inside tfeOutputToCtyValue when Go's json.Marshal fails to serialize the output's DetailedType field. DetailedType is an interface{} holding the type description from TFE; if it contains a Go value json.Marshal cannot handle, this error fires. The %w wraps the json.Marshal error and includes the output ID.

Solutions

  1. Upgrade terraform to the latest release to get a compatible go-tfe client
  2. Enable TF_LOG=TRACE to inspect the raw DetailedType value before marshaling
  3. Remove the problematic output from configuration and re-apply to rewrite the state version output
  4. Report the issue with the output ID and terraform/TFE versions
Defensive patterns

Strategy: validation

Validate before calling

// Validate that the DetailedType is JSON-serializable before conversion
// (requires access to the tfe.StateVersionOutput before calling tfeOutputToCtyValue):
for _, output := range outputs.Items {
    if _, err := json.Marshal(output.DetailedType); err != nil {
        log.Printf("[WARN] output %s has non-serializable DetailedType: %v", output.ID, err)
    }
}

Try / catch

// This error originates inside tfeOutputToCtyValue which is called by GetRootOutputValues.
// Catch at the GetRootOutputValues call boundary:
_, err := state.GetRootOutputValues(ctx)
if err != nil && strings.Contains(err.Error(), "could not marshal output") {
    // DetailedType from TFE is unserializable — likely a client/server version mismatch
    return fmt.Errorf("upgrade terraform to match the TFE server version: %w", err)
}

Prevention

When it happens

Trigger: The TFE API returned a DetailedType containing an unserializable Go value (e.g. a map with non-string keys, a func, or a circular reference); a go-tfe client version deserialized the type into an unexpected Go type; corrupt type data in the state version output.

Common situations: Version mismatch between the go-tfe client embedded in terraform and the TFE server API producing the DetailedType; a new output type added server-side not yet understood by the client; very rare in stable releases.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/8f6e7adefde9c5ae. Report an issue: GitHub.

Appendix: source

Thrown at internal/cloud/state.go:667

		// If the header field is either absent or invalid then we'll
		// just choose zero, which effectively means that we'll just use
		// the caller's requested interval instead. If the caller has no
		// requested interval or it is zero, then we will disable snapshots.
		s.stateSnapshotInterval = time.Duration(0)
	}

	// We will only enable snapshots for intervals greater than zero
	log.Printf("[TRACE] Intermediate state interval is set by header to %v", s.stateSnapshotInterval)
	s.enableIntermediateSnapshots = s.stateSnapshotInterval > 0
}

// tfeOutputToCtyValue decodes a combination of TFE output value and detailed-type to create a
// cty value that is suitable for use in terraform.
func tfeOutputToCtyValue(output tfe.StateVersionOutput) (cty.Value, error) {
	var result cty.Value
	bufType, err := json.Marshal(output.DetailedType)
	if err != nil {
		return result, fmt.Errorf("could not marshal output %s type: %w", output.ID, err)
	}

	var ctype cty.Type
	err = ctype.UnmarshalJSON(bufType)
	if err != nil {
		return result, fmt.Errorf("could not interpret output %s type: %w", output.ID, err)
	}

	result, err = gocty.ToCtyValue(output.Value, ctype)
	if err != nil {
		return result, fmt.Errorf("could not interpret value %v as type %s for output %s: %w", result, ctype.FriendlyName(), output.ID, err)
	}

	return result, nil
}

View on GitHub (pinned to d32a084675)