weaviate/weaviate · error

marshal request: %w

Error message

marshal request: %w

What it means

After validation, AddClass serializes cmd.AddClassRequest (class plus sharding state) with json.Marshal to use as the SubCommand of an ApplyRequest. This error wraps a marshal failure that aborts the write before it reaches RAFT. As the struct holds only JSON-safe types, hitting this in a stock build points to non-serializable data injected into the request (custom code, cyclic references in sharding state, or a patched struct).

Source

Thrown at cluster/raft_apply_endpoints.go:40

	"github.com/prometheus/client_golang/prometheus"
	cmd "github.com/weaviate/weaviate/cluster/proto/api"
	"github.com/weaviate/weaviate/cluster/schema"
	"github.com/weaviate/weaviate/cluster/types"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/usecases/monitoring"
	"github.com/weaviate/weaviate/usecases/sharding"
	"google.golang.org/protobuf/proto"
)

func (s *Raft) AddClass(ctx context.Context, cls *models.Class, ss *sharding.State) (uint64, error) {
	if cls == nil || cls.Class == "" {
		return 0, fmt.Errorf("nil class or empty class name: %w", schema.ErrBadRequest)
	}

	req := cmd.AddClassRequest{Class: cls, State: ss}
	subCommand, err := json.Marshal(&req)
	if err != nil {
		return 0, fmt.Errorf("marshal request: %w", err)
	}
	command := &cmd.ApplyRequest{
		Type:       cmd.ApplyRequest_TYPE_ADD_CLASS,
		Class:      cls.Class,
		SubCommand: subCommand,
	}
	return s.Execute(ctx, command)
}

func (s *Raft) UpdateClass(ctx context.Context, cls *models.Class, _ *sharding.State) (uint64, error) {
	if cls == nil || cls.Class == "" {
		return 0, fmt.Errorf("nil class or empty class name: %w", schema.ErrBadRequest)
	}

	req := cmd.UpdateClassRequest{Class: cls}
	subCommand, err := json.Marshal(&req)
	if err != nil {
		return 0, fmt.Errorf("marshal request: %w", err)

View on GitHub (pinned to 75aa4b6d11)

Solutions

  1. Inspect the wrapped %w cause to see exactly what json.Marshal rejected.
  2. Audit the models.Class payload and sharding state for chan, func, complex, or cyclic fields; remove them or mark them json:"-".
  3. Verify custom module config passed in the class is plain JSON-serializable data.
  4. Rebuild from a clean checkout; if reproducible on stock code, report upstream.

Example fix

// before: cyclic pointer in class module config
cfg.Inverter = cfg // cycle -> json.Marshal fails
// after
var cfg ModuleConfig // keep config a plain JSON tree
class.ModuleConfig = cfg
Defensive patterns

Strategy: validation

Validate before calling

if _, err := json.Marshal(cmd.AddClassRequest{Class: cls, State: ss}); err != nil {
    return fmt.Errorf("class payload not serializable: %w", err)
}
// then call AddClass

Type guard

func jsonSafe(v any) bool {
    _, err := json.Marshal(v)
    return err == nil
}

Try / catch

err := schema.AddClass(ctx, cls, ss)
if err != nil && strings.HasPrefix(err.Error(), "marshal request") {
    logger.Errorf("AddClass payload unserializable: %v", err)
    return err
}

Prevention

When it happens

Trigger: Calling AddClass when json.Marshal of cmd.AddClassRequest fails — non-JSON-serializable values (chan/func/cycle) inside the models.Class (e.g. in custom invectors/module config) or the *sharding.State argument.

Common situations: A fork added a field with a func or channel to models.Class or sharding.State; a cyclic reference introduced via module-config pointers; corrupted build after partial code regeneration of the cmd package.

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/0bc9d506e225f458. Report an issue: GitHub.