# Check if cypher is correct and according to the schema provided for Cypher generated by LLM

**URL:** <https://community.neo4j.com/t/check-if-cypher-is-correct-and-according-to-the-schema-provided-for-cypher-generated-by-llm/67581>\
**Category:** Cypher\
**Tags:** performance, cypher\
**Created:** [April 25, 2024, 8:24am UTC](https://community.neo4j.com/t/check-if-cypher-is-correct-and-according-to-the-schema-provided-for-cypher-generated-by-llm/67581 "2024-04-25T08:24:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrestha.siddhartha0](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shrestha.siddhartha0/32/31536_2.png) [@shrestha.siddhartha0](https://community.neo4j.com/u/shrestha.siddhartha0)\
**Post date:** [April 25, 2024, 8:24am UTC](https://community.neo4j.com/t/check-if-cypher-is-correct-and-according-to-the-schema-provided-for-cypher-generated-by-llm/67581/1 "2024-04-25T08:24:38Z")

</div>

Hello everyone,

I am using LLM for generating cypher for the RAG use case. Right now I get two cases where i need to validate cypher.

1. In some cases, it generates some cypher queries stating properties that don't even exist.
2. Also, in some cases generated cypher itself is invalid and throws issue while running the query.

Is there any way we know if the generated cypher is correct in both properties/schema as well as syntax-wise?

---

<div class="post-metadata">

**Author:** ![v2belleville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/v2belleville/32/29050_2.png) [@v2belleville](https://community.neo4j.com/u/v2belleville)\
**Post date:** [April 25, 2024, 8:44am UTC](https://community.neo4j.com/t/check-if-cypher-is-correct-and-according-to-the-schema-provided-for-cypher-generated-by-llm/67581/2 "2024-04-25T08:44:26Z")

</div>

hi, you can look into this

> **[Competition for Optimizing Cypher-based RAG](https://bratanic-tomaz.medium.com/competition-for-optimizing-cypher-based-rag-b4d3ad2ad3f0)**
>
> LLMs occasionally mess up the relationship direction in Cypher statements, but we can fix that

> **[GitHub - tomasonjo/cypher-direction-competition: Repository that hold the...](https://github.com/tomasonjo/cypher-direction-competition)**
>
> Repository that hold the information about the competition about validating and fixing relationship direction in Cypher statements based on the given schema - tomasonjo/cypher-direction-competition

---

<div class="post-metadata">

**Author:** ![shrestha.siddhartha0](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shrestha.siddhartha0/32/31536_2.png) [@shrestha.siddhartha0](https://community.neo4j.com/u/shrestha.siddhartha0)\
**Post date:** [May 7, 2024, 8:21am UTC](https://community.neo4j.com/t/check-if-cypher-is-correct-and-according-to-the-schema-provided-for-cypher-generated-by-llm/67581/3 "2024-05-07T08:21:25Z")

</div>

Hello @v2belleville , thanks for the help. But this is not what I was looking forward to. My actual need is to find a way to validate the entire cypher query.

My concern is while LLM is generating cypher queries, a common issue I found was LLM frequently generates a query that tries to return or extract the properties that don't exist on the graph. Thus, resulting in None value while running the query.

E.g.

1. MATCH (x: Person{name:"Tom"}) RETURN x.address. But in the actual graph Person node doesn't have the address as its properties.
2. MATCH (x: Organization {"location":"Nepal"}) RETURN (x). Here actually Organization node doesn't have location as its properties.

So, basically, I am trying to figure out if there is a way I can validate the entire cypher query not only the relationship based on the schema provided.
