neo4j-graphrag-python icon indicating copy to clipboard operation
neo4j-graphrag-python copied to clipboard

search function within GraphRAG class does not validate empty answers.

Open CrisBMoya opened this issue 1 year ago • 1 comments

In the search function of GraphRAG class, theres a step to retrieve data: https://github.com/neo4j/neo4j-graphrag-python/blob/4db2c459d6de6bb98276cba260eb93536ea9dc42/src/neo4j_graphrag/generation/graphrag.py#L134

If the result comes empty, then the value for retriever_result.items is an empty list ([]). This is fine.

Problem is the function later does not validate for this scenario, finally passing the input, message_history and system_instruction to the function: https://github.com/neo4j/neo4j-graphrag-python/blob/4db2c459d6de6bb98276cba260eb93536ea9dc42/src/neo4j_graphrag/generation/graphrag.py#L143

Which could lead to potential problems, hallucinations and undesired answers.

A simple solution could be to just validate the items:

if not retriever_result.items:
    result: dict[str, Any] = {"answer": ""}

A more robust solution would be to return either a predefined message or user defined message (as an argument to the function) before the invoke of the llm (as to not invoke an llm over an empty answer) in scenarios where the result is an empty list.

CrisBMoya avatar Mar 28 '25 16:03 CrisBMoya

Hi @CrisBMoya ,

Thank you for raising the issue. You're right that having a test to prevent a useless LLM call if you don't want any answer if the context is empty would be a good to have feature. I think your second option with a user defined message is the cleanest option. I've just labelled this issue as "Good first issue" for people who might want to contribute to the repo.

stellasia avatar May 11 '25 13:05 stellasia

In version 1.8.0 released yesterday, you can now add a response fallback that will be returned if the context is empty:

rag = GraphRAG(retriever=retriever, llm=llm)

result = rag.search(
    "Tell me more about Avatar movies",
    return_context=True,
    response_fallback="I can't answer this question without context",  # NEW
)

stellasia avatar Jul 04 '25 11:07 stellasia