Skip to main content
A minimal guide to creating custom graph models and loading them into Cognee with remember(). Before you start:
  • Complete Quickstart to understand basic operations
  • Ensure you have LLM Providers configured
  • Have some structured data you want to model

What Custom Graph Models Do

  • Define a schema (Pydantic models inheriting from DataPoint) that constrains how Cognee extracts entities and relationships from text
  • Guide remember() to produce graph nodes and edges that match the defined domain model
  • Improve consistency of labels, properties, and relationship structure across ingested documents
  • Control search quality by choosing which fields are indexed via metadata

Code in Action

Step 1: Define Your Entity Classes

Create Pydantic models that inherit from DataPoint to represent the node types in your custom graph. The nested likes: List[Activity] field tells Cognee to extract Activity nodes and the likes edges connecting each Person to them. The metadata dict controls how each node is indexed and identified:
  • index_fields — the fields Cognee embeds and indexes for retrieval (here, name).
  • identity_fields — the fields used to derive a deterministic node id (namespaced by class name). Without them a node gets a random id, so repeated mentions never merge; with identity_fields=["name"], the same Alice or a shared activity like board games collapses into a single node instead of creating duplicates.

Step 2: Define Your Top-Level Graph Container

Wrap your top-level entities in a container model. This is the model you pass as graph_model, and it tells the LLM to return a list of Person entities (each with their nested activities).
When you pass graph_model=..., that model is the structured-output schema the LLM must fill in. Internally, Cognee hands your model to the LLM as the response_model for structured extraction, so the LLM can only return entities and relationships that fit the fields you declared — it is not free to invent an arbitrary shape.
  • Default: without graph_model, Cognee uses its general-purpose KnowledgeGraph schema (free-form nodes and edges).
  • Custom model: when you pass a DataPoint subclass, only the fields you declare on each subclass become part of the extraction schema. Inherited DataPoint infrastructure fields (like id) are stripped out, so they do not expand the LLM’s response schema. Adding a field (e.g. age: int on Person) tells the LLM to extract that value; nested DataPoint fields (like likes: List[Activity]) tell it to extract those related entities and the edges between them.
  • custom_prompt vs graph_model: they play different roles. graph_model defines the shape (which fields and relationships are allowed), while custom_prompt replaces the system prompt that tells the LLM what to look for. Use them together for predictable, domain-specific extraction.

Step 3: Remember Your Data with the Custom Model

This ingests the text and builds the graph in one call. The custom graph_model acts as the extraction schema, and CUSTOM_PROMPT tells the LLM what to look for — here, every person and all the activities they like, including activities shared between people.

Step 4: Visualize Your Data

This renders the generated graph to hobbies_graph.html so you can verify nodes, relationships, and overall schema behavior.

Use in Custom Tasks and Pipelines

This pattern is useful when you need predictable, domain-specific extraction inside custom workflows.
  • Reuse the same graph schema across tasks to keep outputs consistent
  • Run remember(graph_model=...) in workflows where downstream logic expects a specific graph shape
  • Combine with custom prompts or custom tasks to refine extraction
  • Validate pipeline results with visualize_graph before promoting changes to production

Additional examples

Additional examples about custom data models are available on our github.

Full Example

This example shows the complete workflow with metadata for indexing. In practice, you can create complex nested models with multiple relationships and sophisticated data structures.

Low-Level LLM

Learn about direct LLM interaction

Core Concepts

Understand knowledge graph fundamentals

API Reference

Explore API endpoints