{"record":{"id":"71e4dc8747c1f9b1","repo":"Tencent/WeKnora","slug":"failed-to-unmarshal-condition-at-index-d-w","errorCode":null,"errorMessage":"failed to unmarshal condition at index %d: %w","messagePattern":"failed to unmarshal condition at index (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/repository/retriever/milvus/filter.go","lineNumber":279,"sourceCode":"\n\t// Handle logical operators (and/or) - Value should be []*UniversalFilterCondition\n\tif c.Operator == operatorAnd || c.Operator == operatorOr {\n\t\t// Value can be an array of conditions\n\t\tvalueSlice, ok := aux.Value.([]any)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"logical operator %s requires an array of conditions\", c.Operator)\n\t\t}\n\n\t\tconditions := make([]*universalFilterCondition, 0, len(valueSlice))\n\t\tfor i, v := range valueSlice {\n\t\t\tcondBytes, err := json.Marshal(v)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to marshal condition at index %d: %w\", i, err)\n\t\t\t}\n\n\t\t\tvar cond universalFilterCondition\n\t\t\tif err := json.Unmarshal(condBytes, &cond); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to unmarshal condition at index %d: %w\", i, err)\n\t\t\t}\n\t\t\tconditions = append(conditions, &cond)\n\t\t}\n\t\tc.Value = conditions\n\t} else {\n\t\tc.Value = aux.Value\n\t}\n\n\treturn nil\n}\n\n// MarshalJSON implements custom JSON marshaling for UniversalFilterCondition.\nfunc (c *universalFilterCondition) MarshalJSON() ([]byte, error) {\n\ttype Alias struct {\n\t\tField    string `json:\"field,omitempty\"`\n\t\tOperator string `json:\"operator\"`\n\t\tValue    any    `json:\"value,omitempty\"`\n\t}","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/repository/retriever/milvus/filter.go#L261-L297","documentation":"After re-marshaling an AND/OR child element, UnmarshalJSON decodes it into universalFilterCondition. This error wraps json.Unmarshal failures with the element's index and underlying cause (%w) — typically a child object missing required shape or having wrong field types.","triggerScenarios":"A sub-condition object whose \"field\"/\"operator\"/\"value\" keys have types incompatible with the target struct (e.g. \"value\" as an object where a scalar is expected, or operator as a number).","commonSituations":"Malformed nested filter JSON from clients; renamed JSON keys after a library upgrade; deeply nested AND/OR with a typo in one leaf condition.","solutions":["Read the wrapped error to identify the type mismatch at the given index","Fix the JSON at that index so each child has proper field/operator/value types","Validate nested filter JSON with a schema before unmarshaling"],"exampleFix":"// before\n{\"operator\":\"OR\",\"value\":[{\"field\":\"a\",\"operator\":\"EQUAL\",\"value\":{\"x\":1}}]}\n// after\n{\"operator\":\"OR\",\"value\":[{\"field\":\"a\",\"operator\":\"EQUAL\",\"value\":1}]}","handlingStrategy":"try-catch","validationCode":"var raw struct {\n  Operator string   `json:\"operator\"`\n  Value    []any    `json:\"value\"`\n}\nif err := json.Unmarshal(data, &raw); err != nil { return err }\nfor i, v := range raw.Value {\n  m, ok := v.(map[string]any)\n  if !ok || m[\"field\"] == nil || m[\"operator\"] == nil {\n    return fmt.Errorf(\"child %d missing field/operator\", i)\n  }\n}","typeGuard":"func childShapeOK(v any) bool {\n  m, ok := v.(map[string]any)\n  return ok && m[\"field\"] != nil && m[\"operator\"] != nil && m[\"value\"] != nil\n}","tryCatchPattern":"var f Filter\nif err := json.Unmarshal(data, &f); err != nil {\n  if strings.Contains(err.Error(), \"failed to unmarshal condition at index\") {\n    return nil, fmt.Errorf(\"invalid child condition in logical filter: %w\", err)\n  }\n  return nil, err\n}","preventionTips":["Verify nested child condition JSON key names and value types","Add schema validation for nested filters from external clients","Re-test filter JSON after any library version upgrade that changes key names"],"tags":["milvus","json","unmarshal"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}