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.5 reports failed model requests and retries empty answers. 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

Choose a chat model in PowerShell

For Groq, set GROQ_API_KEY first. The following command uses the key from your environment or project's .env file to list model IDs. Pick a text chat model your account can use.

py -c "from dotenv import find_dotenv, load_dotenv; from groq import Groq; load_dotenv(find_dotenv(usecwd=True), override=True, encoding='utf-8-sig'); print('\n'.join(sorted(m.id for m in Groq().models.list().data)))"
$model = Read-Host "Enter a chat model ID from the list"
graph-n4j --model "$model" ask "Which functions does cJSON_Parse call directly?" --repo-id "DaveGamble/cJSON" --provider groq

Use the repository ID shown by graph-n4j repos. Place --model before ask. You can switch models and reuse the same index.

If you get HTTP 404, check the exact model ID and any custom provider endpoint. A database hit counts a query sent to Neo4j, even if it returns zero records. If final answer generation returns empty text, graph-n4j retries with a shorter prompt. If that also returns empty, try a narrower question or another chat model. For diagnostic details, put --verbose before ask.

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.5

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.5
File Size Uploaded Provenance Actions
graph_n4j-1.1.5.tar.gz 55.3 kB Without provenance Details for graph_n4j-1.1.5.tar.gz

Built distribution (wheel)

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

Total release size: 121.4 kB

Release files / graph_n4j-1.1.5.tar.gz

Download URL graph_n4j-1.1.5.tar.gz
Size 55.3 kB
Tags Source
SHA-256 checksum
How to use checksums
be0c05b197cbf6d80268baa8a9615942003f47c866ecba3022c4e83bb32f2880
BLAKE2b-256 checksum
How to use checksums
3694f53f6f0b0a2f47477cb9f38b625b9826304f1d30801472a7894779ca96c5
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.5-py3-none-any.whl

Download URL graph_n4j-1.1.5-py3-none-any.whl
Size 66.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efc10f321d2e235d39d31db3e38fad1ea880d64ba22d3913f7a45f0836818470
BLAKE2b-256 checksum
How to use checksums
50b671537de88eb2c09917e254677c5d0c9c840f30d86a1726e2f98ef9c1e212
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.5

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