{"record":{"id":"562173f05b6cf052","repo":"litedb-org/LiteDB","slug":"target-vector-must-be-provided","errorCode":null,"errorMessage":"Target vector must be provided.","messagePattern":"Target vector must be provided\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Database/LiteQueryable.cs","lineNumber":242,"sourceCode":"        {\n            _query.Select = selector;\n\n            return new LiteQueryable<BsonDocument>(_engine, _mapper, _collection, _query);\n        }\n\n        /// <summary>\n        /// Project each document of resultset into a new document/value based on selector expression\n        /// </summary>\n        public ILiteQueryable<K> Select<K>(Expression<Func<T, K>> selector)\n        {\n            _query.Select = _mapper.GetExpression(selector);\n\n            return new LiteQueryable<K>(_engine, _mapper, _collection, _query);\n        }\n\n        private static void ValidateVectorArguments(float[] target, double maxDistance)\n        {\n            if (target == null || target.Length == 0) throw new ArgumentException(\"Target vector must be provided.\", nameof(target));\n            // Dot-product queries interpret \"maxDistance\" as a minimum similarity score and may therefore pass negative values.\n            if (double.IsNaN(maxDistance)) throw new ArgumentOutOfRangeException(nameof(maxDistance), \"Similarity threshold must be a valid number.\");\n        }\n\n        private static BsonExpression CreateVectorSimilarityFilter(BsonExpression fieldExpr, float[] target, double maxDistance)\n        {\n            if (fieldExpr == null) throw new ArgumentNullException(nameof(fieldExpr));\n\n            ValidateVectorArguments(target, maxDistance);\n\n            var targetArray = new BsonArray(target.Select(v => new BsonValue(v)));\n            return BsonExpression.Create($\"{fieldExpr.Source} VECTOR_SIM @0 <= @1\", targetArray, new BsonValue(maxDistance));\n        }\n\n        internal ILiteQueryable<T> VectorWhereNear(string vectorField, float[] target, double maxDistance)\n        {\n            if (string.IsNullOrWhiteSpace(vectorField)) throw new ArgumentNullException(nameof(vectorField));\n","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Database/LiteQueryable.cs#L224-L260","documentation":"Thrown by ValidateVectorArguments (called from CreateVectorSimilarityFilter, used by VectorWhereNear) when the target float[] is null or has zero length. A vector similarity query requires a concrete reference vector to compare stored embeddings against; an absent vector cannot produce distances.","triggerScenarios":"Calling VectorWhereNear(field, null, maxDistance), VectorWhereNear(field, new float[0], maxDistance), or passing a vector that was not yet loaded from an embedding model. Reached via both the string-field and BsonExpression overloads.","commonSituations":"An embedding generation service returned null/empty, a deserialized vector array came back empty, or the embedding step was skipped for a query path. Also when wiring up an ANN search before embeddings are populated.","solutions":["Ensure the embedding model returns a non-empty float[] before issuing the query.","Null/length-check the vector at the call site and short-circuit with a sensible default (e.g. skip the vector filter).","Verify the embedding dimension matches what was stored; an empty array often signals a truncated payload."],"exampleFix":"// before\nvar results = col.Query().VectorWhereNear(\"$.embedding\", embedding, 0.5).ToList();\n\n// after\nif (embedding == null || embedding.Length == 0)\n    throw new InvalidOperationException(\"Embedding not available for this query.\");\nvar results = col.Query().VectorWhereNear(\"$.embedding\", embedding, 0.5).ToList();","handlingStrategy":"validation","validationCode":"if (target == null || target.Length == 0)\n    throw new InvalidOperationException(\"A non-empty embedding vector is required.\");","typeGuard":"static bool IsValidVector(float[] v) => v != null && v.Length > 0;","tryCatchPattern":null,"preventionTips":["Assert embedding length matches the stored dimension before querying.","Make embedding generators never return null; return empty only on explicit failure and handle upstream."],"tags":["vector","vector-search","argument","embedding"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}