Auto-generated schema prompt
The Cypher chain writes queries against a schema string. If that string is stale, the queries are wrong. Exposing a /schema endpoint does two things: it forces you to keep the schema view fresh, and it lets you or any downstream service audit what the graph actually contains right now.
def schema(self):
graph = self.connect()
labels = [row["label"] for row in graph.query(
"CALL db.labels() YIELD label RETURN label"
)]
rel_types = [row["relationshipType"] for row in graph.query(
"CALL db.relationshipTypes() YIELD relationshipType RETURN relationshipType"
)]
prop_keys = [row["propertyKey"] for row in graph.query(
"CALL db.propertyKeys() YIELD propertyKey RETURN propertyKey"
)]
node_count = graph.query("MATCH (n) RETURN count(n) AS c")[0]["c"]
rel_count = graph.query("MATCH ()-[r]->() RETURN count(r) AS c")[0]["c"]
return {
"labels": labels,
"relationship_types": rel_types,
"property_keys": prop_keys,
"node_count": node_count,
"relationship_count": rel_count,
}db.labels(), db.relationshipTypes(), and db.propertyKeys() are built-in Neo4j procedures that walk the live database and return its structure. Counting nodes and relationships is a cheap health indicator. All of it comes from the running graph, never from a cached config file.
@router.get("/schema", response_model=GraphSchema)
async def schema():
"""Inspect labels and relationship types in the running Neo4j instance."""
try:
data = get_graph_store().schema()
except GraphStoreUnavailable as e:
raise _neo4j_unreachable(e)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Schema inspection failed: {e}")
return GraphSchema(**data)The route returns labels, relationship types, property keys, and counts. A product team can hit this endpoint before filing a bug; if a label they expected is missing, the answer is almost certainly an ingestion problem, not a retrieval problem.
For an internal RAG service, the schema is not a secret. It is shape information about what topics you can answer questions about. For a public-facing service you might gate it behind auth or summarize it. The workshop exposes it openly because transparency helps debugging. In production you decide based on your threat model, not as a default.
Quiz: Quiz
Loading practice…