Skip to main content

graph-n4j

Index Python, C and C++ code in Neo4j, then query relationships between files, classes and functions. Version 1.1.3 keeps indexing independent of AI-provider keys and avoids cache warnings on older Neo4j graphs. The indexer uses Tree-sitter grammars, SHA-256 file checks, a cross-file linker and batched graph writes.

graph-n4j 1.1.0 indexing pipeline

How indexing works

The indexer reads selected files and calculates a SHA-256 digest for each one. If the saved digest, extractor version and extracted records still match, it restores the saved records. Otherwise, Tree-sitter parses the whole changed file with the grammar for Python, C or C++. Indexing needs Neo4j but no AI-provider API key; a key is needed only for natural-language ask requests that use a hosted model.

The parsers emit symbols, such as functions and classes, plus references, such as a call whose target still needs to be found. The linker matches those names across files. The writer sends the resulting graph records to Neo4j in batches.

A cache hit skips parsing. It does not skip linking or dependency checks. If a changed file affects an importer, graph links for both files may need refreshing. This project does not use Tree-sitter's edit-by-edit parsing API.

Graph item Labels or relationships
Code symbols MODULE, CLASS, FUNCTION, METHOD, FIELD, GLOBAL_VARIABLE
Relationships CONTAINS, HAS_METHOD, HAS_FIELD, INHERITS, USES, CALLS, IMPORTS

Worked example

The call is in checkout.py; the function is in rates.py:

# checkout.py
from rates import discount

def quote(total):
    return discount(total)

# rates.py
def discount(total):
    return total - 5

The parser records the call name discount. The import points to rates.py, and the linker resolves the name to the function there. The graph can then store a CALLS edge that a Cypher query can follow:

MATCH (q:FUNCTION {repo_id: $repo, name: 'quote'})-[:CALLS]->(f)
RETURN f.name AS callee, f.file_path AS file

What changed since 0.1.8

The published 0.1.8 release used Python's built-in AST, handled Python files only and completed relationships in a second pass. Version 1.1.0 uses Tree-sitter for Python, C and C++, reuses unchanged file extraction, resolves references across files, and sends graph records to Neo4j in batches.

Only Click and Black can be compared across both versions because 0.1.8 does not support C or C++. The C and C++ results below were measured with 1.1.0 only.

Benchmark results

We used pinned source revisions and measured a first index from an empty repository graph, followed by an unchanged repeat. Times include reading and hashing files, parsing or restoring records, linking, and Neo4j writes. They exclude checkout, database setup and graph inspection. The release run has one observation per case; a separate Click follow-up includes three trials. All timings come from one machine.

Selection Language Files 0.1.8 first / repeat (s) 1.1.0 first / repeat (s) Write calls, 0.1.8 first / repeat Write calls, 1.1.0 first / repeat
Click Python 16 29.20 / 23.44 6.39 / 0.18 3,335 / 3,335 26 / 1
Black Python 25 22.79 / 12.18 1.99 / 0.14 2,264 / 2,264 22 / 1
cJSON C 4 Unsupported 0.84 / 0.08 Unsupported 17 / 1
curl TLS C 31 Unsupported 2.59 / 0.16 Unsupported 25 / 1
tinyxml2 C++ 2 Unsupported 0.83 / 0.04 Unsupported 20 / 1
fmt C++ 17 Unsupported 3.19 / 0.13 Unsupported 26 / 1

“Write calls” counts calls to the project's Neo4j write helpers. One helper call can submit many graph records. It is not a count of nodes, edges or physical disk writes. On an unchanged 1.1.0 run, files are still read and hashed, records are linked again, and the revision stamp is updated.

First-index time

First-index time comparison for Click and Black

Database write calls

Neo4j write-helper calls for Click and Black

Unchanged reindex time

Unchanged reindex time for Click and Black

Native-language graph coverage

Stored CALLS and IMPORTS relationships in the C and C++ benchmark graphs

These results compare complete indexing paths. Parser support, linking, graph records, cache reuse and database writes changed together, so the timings do not isolate Tree-sitter's effect. Edge counts describe stored relationships, not correctness scores. We did not measure precision or recall against a labelled set of expected edges.

The benchmark table and figures are shown here. Machine-readable measurements are available in benchmark-results.json.

Install and run

Install graph-n4j:

python -m pip install --upgrade graph-n4j

Start a local Neo4j instance for development:

docker run -d --name graph-n4j-db \
  -p 7687:7687 -p 7474:7474 \
  -e NEO4J_AUTH=neo4j/change-me \
  neo4j:5-community

Set the database connection for indexing:

export NEO4J_URI=bolt://localhost:7687
export NEO4J_USER=neo4j
export NEO4J_PASSWORD=change-me

Index a local project or a public GitHub repository, then ask a question:

graph-n4j index ./project --repo-id example/project --stats
graph-n4j index-github pallets/click --stats
graph-n4j ask "Where is command routing handled?" --repo-id pallets/click --provider groq

Ask questions with your preferred model

Run graph-n4j repos to find the repository ID. For a hosted provider, set its API key in the terminal where you run ask.

Provider CLI option Environment variable
Groq --provider groq GROQ_API_KEY
OpenAI --provider openai OPENAI_API_KEY
Anthropic --provider anthropic ANTHROPIC_API_KEY
Google Gemini --provider gemini GEMINI_API_KEY
Ollama --provider ollama Local server, no API key

For example, in PowerShell:

$env:OPENAI_API_KEY = "<your key>"
graph-n4j ask "Which modules define commands?" --repo-id pallets/click --provider openai

For a running local Ollama server:

ollama pull qwen2.5-coder
graph-n4j --model qwen2.5-coder ask "Which functions call the parser?" --repo-id pallets/click --provider ollama

The provider setup guide lists model settings and server options. Indexing has no default file-count or file-size cap. Language and directory filters still apply.

For direct graph access, use Cypher:

graph-n4j cypher "MATCH (c:CLASS) RETURN c.name LIMIT 10"

Python API

from graph_n4j import GraphN4J

with GraphN4J.from_env(provider="groq") as graph:
    graph.index_github("pallets/click")
    answer = graph.ask(
        "Where is command routing handled?",
        repo_id="pallets/click",
    )
    print(answer.answer)

Neo4j is required for indexing and graph queries. A language-model provider key is needed for natural-language ask requests. Direct Cypher queries do not need a model provider.

License

MIT. See LICENSE.

Metadata

Release files for graph-n4j 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graph-n4j 1.1.3
File Size Uploaded Provenance Actions
graph_n4j-1.1.3.tar.gz 53.5 kB Without provenance Details for graph_n4j-1.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graph-n4j 1.1.3
File Interpreter ABI Platform Actions
graph_n4j-1.1.3-py3-none-any.whl Python 3 none any Details for graph_n4j-1.1.3-py3-none-any.whl

Total release size: 118.1 kB

Release files / graph_n4j-1.1.3.tar.gz

Download URL graph_n4j-1.1.3.tar.gz
Size 53.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3bb29d3c38b752203c5670965552e9c4de1fd7128381f8197cc995eb5a2c4459
BLAKE2b-256 checksum
How to use checksums
012f3a0d6ba30dd41e95955d126e63830a844d449f8a290b47c3d48174cd1dfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / graph_n4j-1.1.3-py3-none-any.whl

Download URL graph_n4j-1.1.3-py3-none-any.whl
Size 64.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b52481d172d43d85d654322fc2ec74e219f322df8a7f9cff1343cc95859d6b9b
BLAKE2b-256 checksum
How to use checksums
2a6e7195dabb1229217d0aae035393e4f2c69db92e7e31a1a7dc19a7cd4ec897
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for graph-n4j 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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