fix(memory): reject empty search queries (#5258)
This commit is contained in:
@@ -33,6 +33,15 @@ setup_config()
|
||||
ENTITY_PARAMS = frozenset({"user_id", "agent_id", "app_id", "run_id"})
|
||||
|
||||
|
||||
def _validate_and_trim_search_query(query: str) -> str:
|
||||
if not isinstance(query, str):
|
||||
raise ValueError("Invalid query: must be a non-empty string.")
|
||||
trimmed = query.strip()
|
||||
if not trimmed:
|
||||
raise ValueError("Invalid query: cannot be empty or whitespace-only.")
|
||||
return trimmed
|
||||
|
||||
|
||||
def _maybe_alias_anon_to_email(user_email):
|
||||
"""Fire $identify per prior anon ID so PostHog merges them into email.
|
||||
|
||||
@@ -306,6 +315,7 @@ class MemoryClient:
|
||||
|
||||
kwargs = {**(options.model_dump(exclude_unset=True) if options else {}), **kwargs}
|
||||
params = self._prepare_params(kwargs)
|
||||
query = _validate_and_trim_search_query(query)
|
||||
payload = {"query": query, **params}
|
||||
|
||||
response = self.client.post("/v3/memories/search/", json=payload)
|
||||
@@ -1221,6 +1231,7 @@ class AsyncMemoryClient:
|
||||
|
||||
kwargs = {**(options.model_dump(exclude_unset=True) if options else {}), **kwargs}
|
||||
params = self._prepare_params(kwargs)
|
||||
query = _validate_and_trim_search_query(query)
|
||||
payload = {"query": query, **params}
|
||||
|
||||
response = await self.async_client.post("/v3/memories/search/", json=payload)
|
||||
|
||||
@@ -169,6 +169,21 @@ def _validate_search_params(threshold: Optional[float] = None, top_k: Optional[i
|
||||
)
|
||||
|
||||
|
||||
def _validate_and_trim_search_query(query: str) -> str:
|
||||
"""
|
||||
Validates and normalizes a search query before embedding/vector search.
|
||||
|
||||
Raises:
|
||||
ValueError: If query is not a string or is empty/whitespace-only.
|
||||
"""
|
||||
if not isinstance(query, str):
|
||||
raise ValueError("Invalid query: must be a non-empty string.")
|
||||
trimmed = query.strip()
|
||||
if not trimmed:
|
||||
raise ValueError("Invalid query: cannot be empty or whitespace-only.")
|
||||
return trimmed
|
||||
|
||||
|
||||
def _is_sensitive_field(field_name: str) -> bool:
|
||||
"""Check if a field should be redacted for telemetry safety.
|
||||
|
||||
@@ -1178,6 +1193,7 @@ class Memory(MemoryBase):
|
||||
|
||||
# Validate search parameters (before applying defaults)
|
||||
_validate_search_params(threshold=threshold, top_k=top_k)
|
||||
query = _validate_and_trim_search_query(query)
|
||||
|
||||
# Validate and trim entity IDs in filters
|
||||
effective_filters = filters.copy() if filters else {}
|
||||
@@ -2620,6 +2636,7 @@ class AsyncMemory(MemoryBase):
|
||||
|
||||
# Validate search parameters (before applying defaults)
|
||||
_validate_search_params(threshold=threshold, top_k=top_k)
|
||||
query = _validate_and_trim_search_query(query)
|
||||
|
||||
# Validate and trim entity IDs in filters
|
||||
effective_filters = filters.copy() if filters else {}
|
||||
|
||||
Reference in New Issue
Block a user