{"record":{"id":"b2768bb28f74655a","repo":"Tencent/WeKnora","slug":"failed-to-marshal-query-embedding-w","errorCode":null,"errorMessage":"failed to marshal query embedding: %w","messagePattern":"failed to marshal query embedding: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/repository/retriever/elasticsearch/v8/repository.go","lineNumber":421,"sourceCode":"\treturn nil, err\n}\n\n// VectorRetrieve performs vector similarity search using cosine similarity\n// Returns a slice of RetrieveResult containing matching documents\nfunc (e *elasticsearchRepository) VectorRetrieve(ctx context.Context,\n\tparams typesLocal.RetrieveParams,\n) ([]*typesLocal.RetrieveResult, error) {\n\tlog := logger.GetLogger(ctx)\n\tlog.Infof(\"[Elasticsearch] Vector retrieval: dim=%d, topK=%d, threshold=%.4f\",\n\t\tlen(params.Embedding), params.TopK, params.Threshold)\n\n\tfilter := e.getBaseConds(params)\n\n\t// Build script scoring query with cosine similarity\n\tqueryVectorJSON, err := json.Marshal(params.Embedding)\n\tif err != nil {\n\t\tlog.Errorf(\"[Elasticsearch] Failed to marshal query vector: %v\", err)\n\t\treturn nil, fmt.Errorf(\"failed to marshal query embedding: %w\", err)\n\t}\n\n\tscoreSource := \"cosineSimilarity(params.query_vector, 'embedding')\"\n\tminScore := float32(params.Threshold)\n\tscriptScore := &types.ScriptScoreQuery{\n\t\tQuery: types.Query{Bool: &types.BoolQuery{Filter: filter}},\n\t\tScript: types.Script{\n\t\t\tSource: &scoreSource,\n\t\t\tParams: map[string]json.RawMessage{\n\t\t\t\t\"query_vector\": json.RawMessage(queryVectorJSON),\n\t\t\t},\n\t\t},\n\t\tMinScore: &minScore,\n\t}\n\t// Exclude embedding field from source to reduce response size\n\tsourceFilter := &types.SourceFilter{\n\t\tExcludes: []string{\"embedding\"},\n\t}","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/repository/retriever/elasticsearch/v8/repository.go#L403-L439","documentation":"VectorRetrieve builds a script_score query using cosineSimilarity against the embedding field; the query embedding must be marshaled to JSON for the script params. If json.Marshal of params.Embedding fails, it returns 'failed to marshal query embedding: %w'. Marshaling fails when Embedding is of an unsupported type (e.g. containing NaN/Inf floats, channels, funcs, or a custom type with a broken MarshalJSON).","triggerScenarios":"Calling VectorRetrieve with params.Embedding that is nil-wrapped in a type json.Marshal rejects, contains NaN/+Inf/-Inf float32/float64 values, or has an element type (e.g. []int from a bad conversion) that a custom marshaler chokes on.","commonSituations":"Embeddings produced by a model returning NaN on degenerate input; downstream code converting []float32 through string/any losing fidelity; vendor SDK change altering the Embedding field type from []float32 to []any.","solutions":["Validate the embedding before the call: non-nil, correct length, and all values finite (no NaN/Inf).","Check the actual type of params.Embedding matches what the API expects ([]float32); fix the conversion upstream.","If NaN persists, regenerate the embedding or clamp/replace non-finite values.","Inspect the wrapped marshal error — json.MarshalUnsupportedType tells exactly which value/type failed."],"exampleFix":"// before\nparams.Embedding = normalize(rawOutput) // may contain NaN\nres, err := retriever.VectorRetrieve(ctx, params)\n// after\nfor i, v := range embedding {\n\tif math.IsNaN(float64(v)) || math.IsInf(float64(v), 0) {\n\t\tembedding[i] = 0\n\t}\n}\nparams.Embedding = embedding\nres, err := retriever.VectorRetrieve(ctx, params)","handlingStrategy":"validation","validationCode":"func validEmbedding(e []float32, dim int) bool {\n\tif len(e) != dim { return false }\n\tfor _, v := range e {\n\t\tif math.IsNaN(float64(v)) || math.IsInf(float64(v), 0) { return false }\n\t}\n\treturn true\n}\nif !validEmbedding(params.Embedding, expectedDim) { return errors.New(\"invalid query embedding\") }","typeGuard":"func isFloat32Slice(v any) bool {\n\t_, ok := v.([]float32)\n\treturn ok\n}","tryCatchPattern":null,"preventionTips":["Sanitize model outputs: replace NaN/Inf with 0 or regenerate the embedding.","Keep params.Embedding typed as []float32 end-to-end; avoid any/string conversions.","Pin the embedding model and dimension; validate length before search.","Check the concrete type when Embedding crosses an any-typed boundary."],"tags":["elasticsearch","go","json-marshal","vector-search"],"backgroundTag":"json-marshal-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}