{"record":{"id":"7c3b4a8cd63c8069","repo":"mem0ai/mem0","slug":"invalid-query-cannot-be-empty-or-whitespace-only-7c3b4a","errorCode":null,"errorMessage":"Invalid query: cannot be empty or whitespace-only.","messagePattern":"Invalid query: cannot be empty or whitespace-only\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/memory/main.py","lineNumber":250,"sourceCode":"            raise ValueError(\"top_k must be a valid integer\")\n        if top_k < 0:\n            raise ValueError(\n                f\"Invalid top_k: {top_k}. Must be a non-negative integer.\"\n            )\n\n\ndef _validate_and_trim_search_query(query: str) -> str:\n    \"\"\"\n    Validates and normalizes a search query before embedding/vector search.\n\n    Raises:\n        ValueError: If query is not a string or is empty/whitespace-only.\n    \"\"\"\n    if not isinstance(query, str):\n        raise ValueError(\"Invalid query: must be a non-empty string.\")\n    trimmed = query.strip()\n    if not trimmed:\n        raise ValueError(\"Invalid query: cannot be empty or whitespace-only.\")\n    return trimmed\n\n\ndef _is_sensitive_field(field_name: str) -> bool:\n    \"\"\"Check if a field should be redacted for telemetry safety.\n\n    Uses a layered approach:\n    1. Runtime fields (allowlist) — always preserved, highest priority.\n    2. Exact deny list — known secret field names.\n    3. Suffix deny list — catches patterns like db_password, auth_secret, etc.\n    \"\"\"\n    name = field_name.lower().strip()\n    if name in _RUNTIME_FIELDS:\n        return False\n    if name in _SENSITIVE_FIELDS_EXACT:\n        return True\n    return any(name.endswith(suffix) for suffix in _SENSITIVE_SUFFIXES)\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/memory/main.py#L232-L268","documentation":"Raised by _validate_and_trim_search_query when the query is a string but contains only whitespace after .strip() (e.g. '', '   ', '\\n\\t'). An empty query would produce a meaningless embedding, so the SDK rejects it up front. Note the helper also trims the query, so valid queries are normalized to stripped text before embedding.","triggerScenarios":"m.search('', filters=...); m.search('   ', ...); a query built by string formatting where a variable was empty: f\"{prefix} {suffix}\" with both empty; user pressing Enter on an empty chat input that is forwarded directly; whitespace-only text extracted from a document chunk.","commonSituations":"Chat/RAG frontends forwarding empty user input; PDF/web scrapers yielding whitespace-only chunks that get searched; f-string templates with optional parts all empty.","solutions":["Check query.strip() in your UI/wrapper before calling search and treat it as a no-op.","Fall back to a default retrieval query (e.g. last user message) when the computed query is blank.","Trim inputs at ingest time so stored/derived text is never whitespace-only.","Return an empty result list yourself instead of relying on the exception."],"exampleFix":"# before\nresults = m.search(raw_input, filters=f)  # raw_input = \"   \"\n\n# after\nq = (raw_input or \"\").strip()\nresults = m.search(q, filters=f) if q else []","handlingStrategy":"validation","validationCode":"q = (query or \"\").strip()\nif not q:\n    return []  # or raise your own domain error\nresults = m.search(q, filters=f)","typeGuard":"def is_searchable_query(q) -> bool:\n    return isinstance(q, str) and len(q.strip()) > 0","tryCatchPattern":null,"preventionTips":["Strip user input at the UI boundary and reject blank submissions there.","Skip the mem0 call entirely for empty queries instead of relying on exceptions.","Strip scraped document chunks at ingest time."],"tags":["validation","search","query","empty-input"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}