Skip to main content

Python bindings for the lance-graph Cypher engine

Project description

Lance Graph

A high-performance Cypher-capable graph query engine with Python bindings for building scalable, serverless knowledge graphs.

Lance Graph combines a Rust-powered Cypher query engine with Python APIs for:

  • Fast graph queries using Cypher query language
  • AI-powered knowledge extraction from text (via LLM)
  • Lance-backed storage for efficient graph data management
  • Natural language Q&A over your knowledge graphs
  • FastAPI web service for graph queries

Installation

pip install lance-graph

Quick Start

1. Simple Cypher Query

import pyarrow as pa
from lance_graph import CypherQuery, GraphConfig

# Create sample data
people = pa.table({
    "person_id": [1, 2, 3, 4],
    "name": ["Alice", "Bob", "Carol", "David"],
    "age": [28, 34, 29, 42],
})

# Configure graph schema
config = (
    GraphConfig.builder()
    .with_node_label("Person", "person_id")
    .build()
)

# Execute Cypher query
query = CypherQuery("MATCH (p:Person) WHERE p.age > 30 RETURN p.name, p.age")
result = query.with_config(config).execute({"Person": people})

print(result.to_pydict())
# Output: {'name': ['Bob', 'David'], 'age': [34, 42]}

2. Build a Knowledge Graph from Text

from pathlib import Path
from knowledge_graph import (
    KnowledgeGraphConfig,
    LanceKnowledgeGraph,
    LanceGraphStore,
    get_extractor,
)
from knowledge_graph.cli.ingest import extract_and_add

# Initialize knowledge graph
config = KnowledgeGraphConfig.from_root(Path("./my_graph"))
config.ensure_directories()

# Create schema
schema_path = config.resolved_schema_path()
if not schema_path.exists():
    schema_content = """
nodes:
  Entity:
    id_field: entity_id

relationships:
  RELATIONSHIP:
    source: source_entity_id
    target: target_entity_id
"""
    schema_path.write_text(schema_content, encoding="utf-8")

store = LanceGraphStore(config)
store.ensure_layout()

graph_config = config.load_graph_config()
kg = LanceKnowledgeGraph(graph_config, storage=store)
kg.ensure_initialized()

# Extract and add entities/relationships from text
# Using heuristic extractor for testing without API key
extractor = get_extractor("heuristic")
# or using LLM extractor (requires API key)
# extractor = get_extractor("llm", llm_model="gpt-4o-mini")
text = """
Albert Einstein developed the theory of relativity at Princeton.
Marie Curie discovered radioactivity in Paris.
"""

extract_and_add(text, kg, extractor, embedding_generator=None)

# Query the graph
result = kg.query("""
    MATCH (e:Entity)
    RETURN e.name, e.entity_type
    LIMIT 10
""")
print(result.to_pylist())

3. Natural Language Q&A

from knowledge_graph.llm.qa import ask_question

# Ask questions in natural language
answer = ask_question(
    "Who discovered radioactivity?",
    kg,
    llm_model="gpt-4o-mini"
)
print(answer)
# Output: Marie Curie discovered radioactivity.

Command-Line Interface

Lance Graph includes a CLI for building and querying knowledge graphs:

# Initialize and extract
knowledge_graph --root ./my_graph --init
knowledge_graph --root ./my_graph --extract-and-add notes.txt

# Query with Cypher
knowledge_graph --root ./my_graph "MATCH (e:Entity) RETURN e.name LIMIT 10"

# Natural language Q&A
knowledge_graph --root ./my_graph --ask "Who discovered DNA?"

For complete CLI documentation and examples, see the main README.

Requirements

  • Python 3.11+
  • Optional: OpenAI API key for LLM extraction

Contributing

Lance Graph is open source! Contributions are welcome.

Quick start

cd python
uv venv --python 3.11 .venv
source .venv/bin/activate
uv pip install maturin[patchelf]
uv pip install -e '.[tests]'
maturin develop
pytest python/tests/ -v

Development workflow

For linting and type checks:

# Install dev dependencies
uv pip install -e '.[dev]'

# Run linters and type checker
ruff format python/              # format code
ruff check python/               # lint code
pyright                          # type check

# Run specific tests
pytest python/tests/test_graph.py::test_basic_node_selection -v

# Rebuild extension after Rust changes
maturin develop

If another virtual environment is already active, run deactivate (or unset VIRTUAL_ENV) before invoking uv run so uv binds to .venv.

Repository layout

  • python/src/ – PyO3 bridge that exposes graph APIs to Python
  • python/python/lance_graph/ – pure-Python wrapper and __init__
  • python/python/knowledge_graph/ – CLI, FastAPI, and extractor utilities built on Lance
  • python/python/tests/ – graph-centric functional tests

For more information on development setup, building from source, running tests, and code quality guidelines, see DEVELOPMENT.md.

License

Apache 2.0

Links

Project details


Download files

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

Source Distribution

lance_graph-0.3.0.tar.gz (308.3 kB view details)

Uploaded Source

Built Distribution

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

lance_graph-0.3.0-cp39-abi3-manylinux_2_35_x86_64.whl (20.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.35+ x86-64

File details

Details for the file lance_graph-0.3.0.tar.gz.

File metadata

  • Download URL: lance_graph-0.3.0.tar.gz
  • Upload date:
  • Size: 308.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lance_graph-0.3.0.tar.gz
Algorithm Hash digest
SHA256 812c1907a90c10bff4b6cff841210d9a258396f40f5d853ddab6ce1b3cb45c4e
MD5 f4d62ab01ddf6f4fe6420cd7a213d76d
BLAKE2b-256 cb9fc2ef82114a735b2c613225b6c062c793c86413955ae25376563e435e343f

See more details on using hashes here.

File details

Details for the file lance_graph-0.3.0-cp39-abi3-manylinux_2_35_x86_64.whl.

File metadata

  • Download URL: lance_graph-0.3.0-cp39-abi3-manylinux_2_35_x86_64.whl
  • Upload date:
  • Size: 20.7 MB
  • Tags: CPython 3.9+, manylinux: glibc 2.35+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lance_graph-0.3.0-cp39-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 aa089c0e9edee57555cf6f7820445058b6df53d44d96624c6b827e4749f65f87
MD5 c2b09ac4a085749e490ae8492ff93d3c
BLAKE2b-256 77bc0363ed9bfbc87647ce8a2f43a639052d9a0558f7deabc0be495962f39fc7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page