weaviate/weaviate · error

failed to marshal response: %+v, error: %v

Error message

failed to marshal response: %+v, error: %v

What it means

The handler marshals the replicator's response to JSON before writing it; a marshal failure (essentially impossible for the supported response types) yields HTTP 500 with the response value and the underlying error embedded in the body.

Source

Thrown at adapters/handlers/rest/clusterapi/indices_replicas.go:313

		var resp interface{}

		switch cmd {
		case "commit":
			resp = i.replicator.CommitReplication(r.Context(), index, shard, requestID)
		case "abort":
			resp = i.replicator.AbortReplication(r.Context(), index, shard, requestID)
		default:
			http.Error(w, fmt.Sprintf("unrecognized command: %s", cmd), http.StatusNotImplemented)
			return
		}
		if resp == nil { // could not find request with specified id
			http.Error(w, "request not found", http.StatusNotFound)
			return
		}
		b, err := json.Marshal(resp)
		if err != nil {
			http.Error(w, fmt.Sprintf("failed to marshal response: %+v, error: %v", resp, err),
				http.StatusInternalServerError)
			return
		}
		w.Write(b)
	})
}

func (i *replicatedIndices) postObject() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		args := regxObjects.FindStringSubmatch(r.URL.Path)
		if len(args) != 3 {
			http.Error(w, "invalid URI", http.StatusBadRequest)
			return
		}

		requestID := r.URL.Query().Get(replica.RequestKey)
		if requestID == "" {
			http.Error(w, "request_id not provided", http.StatusBadRequest)

View on GitHub (pinned to 75aa4b6d11)

Solutions

  1. Inspect the error in the response body to identify the offending type
  2. Ensure the replicator response type contains only JSON-serializable fields (add json tags, remove func/channel fields)
  3. File/fix a bug in the replicator if a stock response type fails to marshal

Example fix

// before
type resp struct { Done chan bool }
// after
type resp struct { Done bool `json:"done"` }
Defensive patterns

Strategy: try-catch

Try / catch

resp, err := doReplicationCall(url)
if err != nil {
    if strings.Contains(err.Error(), "failed to marshal response") {
        // server-side serialization bug: capture body and report
        return fmt.Errorf("replicator returned unserializable response: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: A replicator.CommitReplication/AbortReplication implementation returning a value json.Marshal cannot encode (e.g. a type containing a channel, func, or cyclic reference) — theoretically a code bug rather than caller error.

Common situations: After modifying the replicator to return a custom response type that is not JSON-serializable, or a dependency upgrade introducing an incompatible response type.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of weaviate/weaviate@75aa4b6d11 (2026-09-04). Data as JSON: /api/errors/e50cf48ad16a2b87. Report an issue: GitHub.