Cypher debugging when the LLM hallucinates relationships

GraphCypherQAChain is happy to invent a relationship type that does not exist in your schema. Cypher runs, returns zero rows, and the chain politely says "I do not know." The fix is not a bigger model. It is treating the generated Cypher as untrusted output and sanity-checking it against your real schema.

Where Cypher generation goes wrong

Three classes of silent failure that all look the same to the user.

graph_rag.py
python
result = chain.invoke({"query": question})

# Always log what the LLM actually wrote
print("Generated Cypher:")
print(result["intermediate_steps"][0]["query"])
print("First row of context:")
print((result["intermediate_steps"][1]["context"] or [None])[0])

Print the generated Cypher and the first row before anything else. If you cannot read the query, you cannot debug the answer.

Quiz: Quiz

Loading practice…