From f849305a56d9f9eac32f2f9a8e9169d10cb4c7c1 Mon Sep 17 00:00:00 2001 From: Mohammad Anash <78532895+Anash3@users.noreply.github.com> Date: Thu, 6 Feb 2025 07:50:26 +0530 Subject: [PATCH] fixed Bug in PreFilter of AzureCosmosDBNoSqlVectorSearch (#29613) Description: Fixes PreFilter value handling in Azure Cosmos DB NoSQL vectorstore. The current implementation fails to handle numeric values in filter conditions, causing an undefined value variable error. This PR adds support for numeric, boolean, and NULL values while maintaining the existing string and list handling. Changes: Added handling for numeric types (int/float) Added boolean value support Added NULL value handling Added type validation for unsupported values Fixed scope of value variable initialization Issue: Fixes #29610 Implementation Notes: No changes to public API Backwards compatible Maintains consistent behavior with existing MongoDB-style filtering Preserves SQL injection prevention through proper value handling --------- Co-authored-by: Chester Curme --- .../vectorstores/azure_cosmos_db_no_sql.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libs/community/langchain_community/vectorstores/azure_cosmos_db_no_sql.py b/libs/community/langchain_community/vectorstores/azure_cosmos_db_no_sql.py index 721994f81a4..90b3deb6ab3 100644 --- a/libs/community/langchain_community/vectorstores/azure_cosmos_db_no_sql.py +++ b/libs/community/langchain_community/vectorstores/azure_cosmos_db_no_sql.py @@ -789,6 +789,13 @@ class AzureCosmosDBNoSqlVectorSearch(VectorStore): elif isinstance(condition.value, list): # e.g., for IN clauses value = f"({', '.join(map(str, condition.value))})" + elif isinstance(condition.value, (int, float, bool)): + value = str(condition.value) + elif condition.value is None: + value = "NULL" + else: + raise ValueError(f"Unsupported value type: {type(condition.value)}") + clauses.append(f"c.{condition.property} {sql_operator} {value}") return f""" WHERE {' {} '.format(sql_logical_operator).join(clauses)}""".strip()