Graphiti’s knowledge graph is composed of nodes (entities, episodes, communities, sagas) and edges (relationships between nodes). Understanding this schema is essential for working with Graphiti effectively.
EntityNode( uuid="550e8400-e29b-41d4-a716-446655440000", name="Kamala Harris", labels=["Person", "Politician"], summary="Kamala Harris served as Attorney General of California from 2011-2017 and became Vice President in 2021.", attributes={ "birth_year": 1964, "education": "Howard University, UC Hastings" }, group_id="default", created_at=datetime(2024, 3, 15, tzinfo=timezone.utc))
# Get by UUIDnode = await EntityNode.get_by_uuid(driver, uuid="...")# Get by group IDsnodes = await EntityNode.get_by_group_ids( driver, group_ids=["user_123"], limit=100)# Save a nodeawait node.save(driver)# Delete a node (and connected edges)await node.delete(driver)
CommunityNode( uuid="770e8400-e29b-41d4-a716-446655440002", name="California Political Leadership", summary="Community of political figures who have served in California government positions including Governor, Attorney General, and Lieutenant Governor.", group_id="default", created_at=datetime(2024, 3, 15, tzinfo=timezone.utc))
Represents semantic relationships between entities, with temporal validity tracking.
class EntityEdge(Edge): uuid: str source_node_uuid: str # Source EntityNode UUID target_node_uuid: str # Target EntityNode UUID name: str # Relationship name fact: str # Natural language fact fact_embedding: list[float] | None episodes: list[str] # Episode UUIDs that mention this edge group_id: str # Temporal fields created_at: datetime # When edge was first created expired_at: datetime | None # When edge was invalidated by new info valid_at: datetime | None # When the fact became true invalid_at: datetime | None # When the fact stopped being true # Custom attributes attributes: dict[str, Any]
EntityEdge( uuid="990e8400-e29b-41d4-a716-446655440004", source_node_uuid="kamala-harris-uuid", target_node_uuid="california-ag-uuid", name="held_position", fact="Kamala Harris served as Attorney General of California from January 2011 to January 2017.", fact_embedding=[0.123, -0.456, ...], # 1536-dim vector episodes=["episode-uuid-1", "episode-uuid-2"], group_id="default", created_at=datetime(2024, 3, 15, tzinfo=timezone.utc), expired_at=None, # Still current in the graph valid_at=datetime(2011, 1, 3, tzinfo=timezone.utc), invalid_at=datetime(2017, 1, 3, tzinfo=timezone.utc), attributes={"position_type": "elected_official"})
# Get by UUIDedge = await EntityEdge.get_by_uuid(driver, uuid="...")# Get edges connected to a nodeedges = await EntityEdge.get_by_node_uuid(driver, node_uuid="...")# Get edges between two nodesedges = await EntityEdge.get_between_nodes( driver, source_node_uuid="alice-uuid", target_node_uuid="acme-uuid")# Save an edgeawait edge.save(driver)# Delete an edgeawait edge.delete(driver)
# Find all entities in a communityrecords, _, _ = await driver.execute_query(""" MATCH (c:Community {uuid: $community_uuid})-[:HAS_MEMBER]->(e:Entity) RETURN e""", community_uuid=community.uuid)
# Get all relationships for an entityedges = await EntityEdge.get_by_node_uuid(driver, node_uuid=alice.uuid)# Find entities related to Alicerecords, _, _ = await driver.execute_query(""" MATCH (alice:Entity {uuid: $alice_uuid})-[:RELATES_TO]-(related:Entity) RETURN related""", alice_uuid=alice.uuid)
# Find facts valid at a specific timerecords, _, _ = await driver.execute_query(""" MATCH (n)-[e:RELATES_TO]->(m) WHERE e.valid_at <= $query_time AND (e.invalid_at > $query_time OR e.invalid_at IS NULL) AND e.expired_at IS NULL RETURN e""", query_time=datetime(2023, 6, 1, tzinfo=timezone.utc))
Every node and edge has a group_id field for partitioning:
# Add data to different groupsawait graphiti.add_episode( name="User 1 Data", episode_body="Alice likes hiking.", group_id="user_1", reference_time=datetime.now(timezone.utc))await graphiti.add_episode( name="User 2 Data", episode_body="Bob likes coding.", group_id="user_2", reference_time=datetime.now(timezone.utc))# Search within a groupresults = await graphiti.search( "What does the user like?", group_ids=["user_1"] # Only searches user_1's data)