Create a .env file or export environment variables:
.env
# Required for LLM and embeddingsOPENAI_API_KEY=your_openai_api_key# Neo4j connection (adjust if using different backend)NEO4J_URI=bolt://localhost:7687NEO4J_USER=neo4jNEO4J_PASSWORD=password
Keep your API keys secure. Never commit them to version control.
Episodes are the primary units of information in Graphiti. They can be text or structured JSON and are automatically processed to extract entities and relationships.
1
Define Your Episodes
Create a list of episodes with both text and JSON content:
episodes = [ { 'content': 'Kamala Harris is the Attorney General of California. ' 'She was previously the district attorney for San Francisco.', 'type': EpisodeType.text, 'description': 'podcast transcript', }, { 'content': 'As AG, Harris was in office from January 3, 2011 – January 3, 2017', 'type': EpisodeType.text, 'description': 'podcast transcript', }, { 'content': { 'name': 'Gavin Newsom', 'position': 'Governor', 'state': 'California', 'previous_role': 'Lieutenant Governor', }, 'type': EpisodeType.json, 'description': 'podcast metadata', },]
2
Add Episodes to the Graph
Process each episode and add it to your knowledge graph:
import jsonfor i, episode in enumerate(episodes): await graphiti.add_episode( name=f'Podcast Episode {i}', episode_body=episode['content'] if isinstance(episode['content'], str) else json.dumps(episode['content']), source=episode['type'], source_description=episode['description'], reference_time=datetime.now(timezone.utc), ) print(f"Added episode {i}: {episode['type'].value}")
Graphiti automatically:
Extracts entities (nodes) like “Kamala Harris” and “California”
Identifies relationships (edges) like “is Attorney General of”
print("\nSearching for: 'Who was the California Attorney General?'")results = await graphiti.search('Who was the California Attorney General?')# Print search resultsprint('\nSearch Results:')for result in results: print(f'Fact: {result.fact}') if result.valid_at: print(f'Valid from: {result.valid_at}') if result.invalid_at: print(f'Valid until: {result.invalid_at}') print('---')
The search() method performs hybrid search combining:
For more contextually relevant results, use a center node to rerank search results based on their graph distance:
if results and len(results) > 0: # Get the source node UUID from the top result center_node_uuid = results[0].source_node_uuid print(f'\nReranking with center node: {center_node_uuid}') reranked_results = await graphiti.search( 'Who was the California Attorney General?', center_node_uuid=center_node_uuid ) print('\nReranked Search Results:') for result in reranked_results: print(f'Fact: {result.fact}') print('---')
Graph-based reranking prioritizes results that are closer in the graph structure to a specific node, giving you more contextually relevant information.
Ensure your graph database is running:Neo4j: Check Neo4j Desktop and verify your DBMS is startedFalkorDB: Verify Docker container is running with docker psConnection refused: Double-check URI, username, and password in your environment variables
OpenAI API errors
Verify OPENAI_API_KEY is set correctly
Check you have sufficient API credits
Ensure you’re using a supported model (defaults to gpt-4o-mini)
429 Rate Limit Errors
Graphiti defaults to low concurrency (SEMAPHORE_LIMIT=10) to avoid rate limits. If you still encounter 429 errors:
export SEMAPHORE_LIMIT=5
If your LLM provider allows higher throughput, increase this value for better performance.
Graph not found errors
The Neo4j driver defaults to the neo4j database. To use a different database: