Tencent/WeKnora · error

failed to marshal condition at index %d: %w

Error message

failed to marshal condition at index %d: %w

What it means

During UnmarshalJSON of an AND/OR condition, each array element is re-marshaled to JSON bytes before being decoded into universalFilterCondition. This error wraps a json.Marshal failure on the element at the given index (%w preserves the underlying error).

Source

Thrown at internal/application/repository/retriever/milvus/filter.go:274

		return err
	}

	c.Field = aux.Field
	c.Operator = strings.ToLower(aux.Operator)

	// Handle logical operators (and/or) - Value should be []*UniversalFilterCondition
	if c.Operator == operatorAnd || c.Operator == operatorOr {
		// Value can be an array of conditions
		valueSlice, ok := aux.Value.([]any)
		if !ok {
			return fmt.Errorf("logical operator %s requires an array of conditions", c.Operator)
		}

		conditions := make([]*universalFilterCondition, 0, len(valueSlice))
		for i, v := range valueSlice {
			condBytes, err := json.Marshal(v)
			if err != nil {
				return fmt.Errorf("failed to marshal condition at index %d: %w", i, err)
			}

			var cond universalFilterCondition
			if err := json.Unmarshal(condBytes, &cond); err != nil {
				return fmt.Errorf("failed to unmarshal condition at index %d: %w", i, err)
			}
			conditions = append(conditions, &cond)
		}
		c.Value = conditions
	} else {
		c.Value = aux.Value
	}

	return nil
}

// MarshalJSON implements custom JSON marshaling for UniversalFilterCondition.
func (c *universalFilterCondition) MarshalJSON() ([]byte, error) {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Inspect the wrapped error and index to find the offending element
  2. Ensure each array element is a plain JSON-compatible object (map[string]any)
  3. Avoid injecting non-JSON-serializable Go values into the decoded structure
Defensive patterns

Strategy: try-catch

Try / catch

var f Filter
if err := json.Unmarshal(data, &f); err != nil {
  var idxErr *fmt.WrapError
  if strings.Contains(err.Error(), "failed to marshal condition at index") {
    return nil, fmt.Errorf("unserializable element in logical value: %w", err)
  }
  return nil, err
}

Prevention

When it happens

Trigger: An array element that cannot be marshaled back to JSON — practically rare since elements came from unmarshaled JSON, but possible with channels/funcs/cycles injected programmatically into the raw value map.

Common situations: Custom JSON.Unmarshaler interactions or test fixtures building aux.Value maps containing unsupported Go types; corrupt intermediate values from upstream decoding hooks.

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 Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/128b45ffb77b16f8. Report an issue: GitHub.