log warning and early exit when graph is empty and is queried

This commit is contained in:
Daulet Amirkhanov 2025-10-20 22:44:40 +01:00
parent 44345e7cf3
commit ee7db762e6
5 changed files with 39 additions and 0 deletions

View file

@ -44,6 +44,12 @@ class CypherSearchRetriever(BaseRetriever):
"""
try:
graph_engine = await get_graph_engine()
is_empty = await graph_engine.is_empty()
if is_empty:
logger.warning("Search attempt on an empty knowledge graph")
return []
result = await graph_engine.query(query)
except Exception as e:
logger.error("Failed to execture cypher search retrieval: %s", str(e))

View file

@ -124,6 +124,13 @@ class GraphCompletionRetriever(BaseGraphRetriever):
- str: A string representing the resolved context from the retrieved triplets, or an
empty string if no triplets are found.
"""
graph_engine = await get_graph_engine()
is_empty = await graph_engine.is_empty()
if is_empty:
logger.warning("Search attempt on an empty knowledge graph")
return []
triplets = await self.get_triplets(query)
if len(triplets) == 0:

View file

@ -122,6 +122,11 @@ class NaturalLanguageRetriever(BaseRetriever):
query.
"""
graph_engine = await get_graph_engine()
is_empty = await graph_engine.is_empty()
if is_empty:
logger.warning("Search attempt on an empty knowledge graph")
return []
return await self._execute_cypher_query(query, graph_engine)

View file

@ -1,12 +1,16 @@
from typing import Any, List, Optional, Tuple, Type, Union
from cognee.infrastructure.databases.graph import get_graph_engine
from cognee.modules.data.models.Dataset import Dataset
from cognee.modules.engine.models.node_set import NodeSet
from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge
from cognee.modules.search.types import SearchType
from cognee.shared.logging_utils import get_logger
from .get_search_type_tools import get_search_type_tools
logger = get_logger()
async def no_access_control_search(
query_type: SearchType,
@ -32,6 +36,12 @@ async def no_access_control_search(
save_interaction=save_interaction,
last_k=last_k,
)
graph_engine = await get_graph_engine()
is_empty = await graph_engine.is_empty()
if is_empty:
# TODO: we can log here, but not all search types use graph. Still keeping this here for reviewer input
logger.warning("Search attempt on an empty knowledge graph")
if len(search_tools) == 2:
[get_completion, get_context] = search_tools

View file

@ -5,6 +5,8 @@ from uuid import UUID
from fastapi.encoders import jsonable_encoder
from typing import Any, List, Optional, Tuple, Type, Union
from cognee.infrastructure.databases.graph import get_graph_engine
from cognee.shared.logging_utils import get_logger
from cognee.shared.utils import send_telemetry
from cognee.context_global_variables import set_database_global_context_variables
@ -27,6 +29,8 @@ from .get_search_type_tools import get_search_type_tools
from .no_access_control_search import no_access_control_search
from ..utils.prepare_search_result import prepare_search_result
logger = get_logger()
async def search(
query_text: str,
@ -329,6 +333,13 @@ async def search_in_datasets_context(
# Set database configuration in async context for each dataset user has access for
await set_database_global_context_variables(dataset.id, dataset.owner_id)
graph_engine = await get_graph_engine()
is_empty = await graph_engine.is_empty()
if is_empty:
# TODO: we can log here, but not all search types use graph. Still keeping this here for reviewer input
logger.warning("Search attempt on an empty knowledge graph")
specific_search_tools = await get_search_type_tools(
query_type=query_type,
query_text=query_text,