Skip to main content

Cognee Community Graph Adapter - Google Cloud Spanner (Spanner Graph)

This package provides a Google Cloud Spanner graph database adapter for Cognee using Spanner Graph (GQL). It allows using Cloud Spanner as the graph backend for knowledge graphs and GraphRAG pipelines when running in GCP.

Requirements

  • Python >= 3.10, <= 3.13
  • Google Cloud project with Spanner API enabled
  • A Spanner instance (Enterprise or Enterprise Plus edition; Spanner Graph is not available on PostgreSQL dialect)
  • A database created with the Cognee graph schema (see below)

Schema setup

Create a Spanner database with Google Standard SQL and run the following DDL to create the Cognee graph tables and property graph:

CREATE TABLE CogneeNode (
  id         STRING(MAX) NOT NULL,
  properties JSON,
) PRIMARY KEY (id);

CREATE TABLE CogneeEdge (
  source_id         STRING(MAX) NOT NULL,
  target_id         STRING(MAX) NOT NULL,
  edge_id           STRING(MAX) NOT NULL,
  relationship_type STRING(MAX) NOT NULL,
  properties        JSON,
) PRIMARY KEY (source_id, target_id, edge_id);

CREATE OR REPLACE PROPERTY GRAPH CogneeGraph
  NODE TABLES (CogneeNode)
  EDGE TABLES (
    CogneeEdge
      SOURCE KEY (source_id) REFERENCES CogneeNode (id)
      DESTINATION KEY (target_id) REFERENCES CogneeNode (id)
      LABEL Related
  );

You can create the database and apply this DDL via the Google Cloud Console, gcloud CLI, or the Spanner Admin API. See Set up and query Spanner Graph for details.

Installation

Create a virtual environment, then install the package (recommended):

cd packages/graph/spanner
python -m venv .venv
source .venv/bin/activate   # On Windows: .venv\Scripts\activate
pip install -e ".[dev]"   # Editable install with dev deps (pytest, pytest-asyncio)

Or install from PyPI (when published):

pip install cognee-community-graph-adapter-spanner

Or with uv:

uv venv && source .venv/bin/activate
uv pip install -e ".[dev]"

Usage

import asyncio
import cognee
from cognee.infrastructure.databases.graph import get_graph_engine
from cognee_community_graph_adapter_spanner import register


async def main():
    register()
    cognee.config.set_graph_database_provider("spanner")

    # Option A: single URL (project_id/instance_id/database_id)
    cognee.config.set_graph_db_config(
        {
            "graph_database_url": "my-project/my-instance/my-database",
        }
    )

    # Option B: separate parameters
    cognee.config.set_graph_db_config(
        {
            "project_id": "my-project",
            "instance_id": "my-instance",
            "database_id": "my-database",
        }
    )

    # Optional: custom credentials (defaults to Application Default Credentials)
    # cognee.config.set_graph_db_config({
    #     "project_id": "my-project",
    #     "instance_id": "my-instance",
    #     "database_id": "my-database",
    #     "credentials": your_credentials,
    # })

    await cognee.add(["Your content here."], "my_dataset")
    await cognee.cognify(["my_dataset"])
    results = await cognee.search(
        query_type=cognee.SearchType.GRAPH_COMPLETION,
        query_text="your query",
    )
    graph_engine = await get_graph_engine()
    nodes, edges = await graph_engine.get_graph_data()


if __name__ == "__main__":
    asyncio.run(main())

Configuration

Configure via set_graph_db_config() with one of:

Key Description
graph_database_url Single string: project_id/instance_id/database_id
project_id GCP project ID (required if not using graph_database_url)
instance_id Spanner instance ID
database_id Spanner database ID
credentials Optional Google Auth credentials (default: Application Default Credentials)

Authentication uses Application Default Credentials unless credentials is provided.

Features

  • Implements Cognee's GraphDBInterface using Spanner Graph (GQL) and SQL DML
  • Async API; blocking Spanner calls run in a thread pool
  • Node and edge properties stored as JSON
  • Compatible with Cognee's add/cognify/search and graph visualization

Example

See the examples/example.py script for a full workflow (add data, cognify, search, graph data access). Ensure your Spanner database exists with the schema above and that ADC or credentials are set.

License

This project is licensed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognee_community_graph_adapter_spanner-0.2.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file cognee_community_graph_adapter_spanner-0.2.0.tar.gz.

File metadata

File hashes

Hashes for cognee_community_graph_adapter_spanner-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8315e3068233ce5f7a6a5d210fda2df8acc711d1e9aa0af31ba9b4ea3acd318c
MD5 8a739e0ce95b64ae2cf24c5202a3eb76
BLAKE2b-256 2e6ce6066ba0892e912cea348915f83599992b8c3b5a0af51290c5c30dc7e9b7

See more details on using hashes here.

File details

Details for the file cognee_community_graph_adapter_spanner-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cognee_community_graph_adapter_spanner-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b92d02431237b0115a5140255ff690594c0d8a28b4921b777bbc83dd17f8e151
MD5 5bc2c03072d78ce80f78117c2e7496f1
BLAKE2b-256 8b931aa5aba55f17676cf2ebd993f9b70033b745cefb5f6f0226ac31b91df231

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page