# How to Find the Path Between Two Nodes in Graphify: API and CLI Methods

> Learn how to find the path between two nodes in Graphify using the API and CLI. Discover efficient shortest path computation with Graphify Labs Graphify.

- Repository: [Graphify Labs/graphify](https://github.com/Graphify-Labs/graphify)
- Tags: how-to-guide
- Published: 2026-07-16

---

**Graphify computes the shortest undirected path between two nodes using NetworkX's `shortest_path` algorithm, exposed through both the HTTP API at `/tools/shortest_path` and the `graphify shortest_path` CLI command.**

Graphify is an open-source knowledge graph framework developed by Graphify-Labs that stores concepts as nodes in a NetworkX graph. Finding the path between two nodes in Graphify enables you to trace semantic relationships through the knowledge base, with the system treating edges as undirected to discover the minimal hop sequence regardless of original edge direction.

## How Graphify Computes the Path Between Two Nodes

The path finding implementation in Graphify follows a three-stage pipeline that transforms natural language concept names into navigable graph traversals.

### Node Lookup and Scoring

When you provide `source` and `target` strings, Graphify first identifies the corresponding node IDs using the `_score_nodes` helper function defined in [`graphify/paths.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/paths.py). This function tokenizes the input labels and scores candidate nodes based on label token matches, ensuring fuzzy matching against the knowledge base entries.

### Undirected Path Computation

After resolving the node IDs (`src_nid` and `tgt_nid`), the system calls `nx.shortest_path(G.to_undirected(as_view=True), src_nid, tgt_nid)` according to the implementation in [`graphify/serve.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/serve.py) (lines 1444-1449). The graph is converted to an undirected view using `as_view=True`, meaning edge directionality does not constrain the route discovery. This operation resides within the `_tool_shortest_path` function, which also handles the optional `max_hops` parameter (defaulting to 8) to limit traversal depth.

### Result Formatting

The function returns the path as a newline-separated list of node labels. Each label is sanitized to ensure safe output for LLM consumption, stripping any potentially problematic characters while preserving the semantic content of the knowledge graph nodes.

## Finding Paths via the HTTP API

To programmatically find the path between two nodes in Graphify, send a POST request to the `/tools/shortest_path` endpoint with the source and target concepts:

```python
import requests

payload = {
    "source": "GraphQL query execution",
    "target": "TypeScript type inference",
    "max_hops": 8  # optional, default=8

}

resp = requests.post(
    "http://localhost:8000/tools/shortest_path",
    json=payload
)

print(resp.text)

# Output: list of node labels forming the shortest path

```

The API returns the path as a plain text response containing the sequence of node labels, computed by the `_tool_shortest_path` function in [`graphify/serve.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/serve.py).

## Finding Paths via the CLI

Graphify also exposes path finding through the command-line interface in [`graphify/cli.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/cli.py). The CLI provides a convenient wrapper around the same NetworkX shortest path logic:

```bash
graphify shortest_path \
    --source "GraphQL query execution" \
    --target "TypeScript type inference"

```

Both the CLI and HTTP API ultimately invoke the same underlying function, ensuring consistent behavior across interfaces. The CLI handler processes the arguments and delegates to the NetworkX graph traversal logic, supporting the same `max_hops` constraint available in the API.

## Key Source Files and Architecture

Understanding the Graphify source code structure helps when extending or debugging path finding functionality:

- **[`graphify/serve.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/serve.py)** – Registers the `shortest_path` tool and implements `_tool_shortest_path` (lines 1444-1449), containing the core logic for node scoring and path computation.
- **[`graphify/cli.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/cli.py)** – Provides the command-line wrapper that instantiates the graph and calls the shortest path utilities.
- **[`graphify/paths.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/paths.py)** – Generated at runtime, holds the NetworkX graph object (`G`) and helper functions such as `_score_nodes` for fuzzy node matching.
- **[`graphify/multigraph_compat.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/multigraph_compat.py)** – Supplies compatibility layers for handling NetworkX multigraph structures during path traversal.

These components work together to provide the path finding capability, with the NetworkX graph serving as the central data structure for all traversal operations.

## Summary

- **Graphify** stores knowledge as a NetworkX graph where nodes represent concepts and edges represent relationships.
- The **shortest path** algorithm treats the graph as undirected, allowing discovery of minimal routes regardless of original edge direction.
- Both HTTP and CLI interfaces accept **`source`** and **`target`** parameters, with an optional **`max_hops`** limit (default 8).
- The implementation lives in **[`graphify/serve.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/serve.py)** (lines 1444-1449) and relies on **`_score_nodes`** from [`graphify/paths.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/paths.py) for fuzzy node matching.
- Results are returned as sanitized node labels suitable for downstream LLM processing.

## Frequently Asked Questions

### Does Graphify use directed or undirected paths when finding routes between nodes?

Graphify treats the knowledge graph as undirected for path discovery. The implementation explicitly calls `G.to_undirected(as_view=True)` before computing the shortest path, meaning the direction of edges does not limit the route. This design choice ensures that conceptual relationships can be traversed bi-directionally to find the minimal hop sequence.

### How does Graphify match input strings to actual graph nodes?

The system uses the `_score_nodes` function defined in [`graphify/paths.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/paths.py) to perform fuzzy matching on label tokens. When you provide source and target strings, this function tokenizes the input and scores candidate nodes based on matching tokens, allowing you to reference concepts using natural language descriptions rather than exact node IDs.

### What is the maximum hop limit for path finding in Graphify?

The default maximum hop limit is **8**, controlled by the optional `max_hops` parameter in both the HTTP API and CLI. You can override this default by specifying a different integer value when calling the endpoint or command, which constrains the NetworkX shortest path algorithm to paths within that depth limit.

### Which source file contains the main path finding implementation?

The core logic resides in **[`graphify/serve.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/serve.py)** at lines 1444-1449 within the `_tool_shortest_path` function. This function orchestrates the node scoring, calls `nx.shortest_path` on the undirected graph view, and formats the results. The CLI implementation in [`graphify/cli.py`](https://github.com/Graphify-Labs/graphify/blob/main/graphify/cli.py) serves as a thin wrapper that calls this same underlying function.