> ## Documentation Index
> Fetch the complete documentation index at: https://docs.snowleopard.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LangGraph

> Integrate Snow Leopard with LangGraph workflows for stateful data retrieval.

This guide shows you how to add Snow Leopard data retrieval to a [LangGraph](https://langchain-ai.github.io/langgraph/) workflow.

## Prerequisites

* Python 3.10+
* OpenAI API key (or another [supported model provider](https://python.langchain.com/docs/integrations/chat/))
* [Snow Leopard Cloud API key](https://docs.snowleopard.ai/cloud/getting-started.md#api-keys)
* A [Snow Leopard Cloud](https://docs.snowleopard.ai/cloud/getting-started.md) instance with at least one [data source](https://docs.snowleopard.ai/cloud/getting-started.md#adding-a-data-source) connected
* Your instance ID, from the instance's [Connection Info](https://docs.snowleopard.ai/cloud/getting-started.md#connection-info) tab

> [!NOTE]
>
> Don't have data? Load the [sample Northwind dataset](https://github.com/SnowLeopard-AI/northwind_psql) into a PostgreSQL database that Snow Leopard Cloud can reach, such as a hosted [Neon](https://neon.com) or [Supabase](https://supabase.com) database. Run the dataset's `northwind.sql` script against your database, then [add the database as a data source](https://docs.snowleopard.ai/cloud/getting-started.md#adding-a-data-source) in your instance.

## Start from scratch

Clone and run the [complete working example](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/quickstart/langgraph):

```bash theme={null}
git clone https://github.com/SnowLeopard-AI/snowy-examples.git
cd snowy-examples/quickstart/langgraph
```

Install dependencies:

```bash theme={null}
pip install langgraph langchain langchain-openai snowleopard python-dotenv
```

Set your environment variables:

```bash theme={null}
export OPENAI_API_KEY=your-openai-key
export SNOWLEOPARD_API_KEY=your-snowleopard-key
export SNOWLEOPARD_INSTANCE_ID=your-instance-id
```

Run the example:

```bash theme={null}
python langgraph_quickstart.py
```

## Bring your own agent

Already have a LangGraph workflow? Add Snow Leopard data retrieval with these steps.

### 1. Install dependencies

```bash theme={null}
pip install snowleopard
```

### 2. Add Snow Leopard fields to your state

Add fields to your graph state to store query results:

```python theme={null}
from typing_extensions import TypedDict

class GraphState(TypedDict):
    user_question: str
    query_result: str  # Add this field for Snow Leopard results
    # ... your other state fields
```

### 3. Create a Snow Leopard query node

Create a node that calls Snow Leopard to retrieve data:

```python theme={null}
from snowleopard import SnowLeopardClient

client = SnowLeopardClient(api_key="{your-snowleopard-api-key}")

def query_database(state: GraphState) -> GraphState:
    """Query the database using Snow Leopard."""
    response = client.retrieve(
        instance_id="{your-instance-id}",
        user_query=state["user_question"]
    )
    state["query_result"] = str(response)
    return state
```

### 4. Add the node to your workflow

Add the query node and connect it to your existing workflow:

```python theme={null}
from langgraph.graph import StateGraph

workflow = StateGraph(GraphState)

# Add the Snow Leopard query node
workflow.add_node("query", query_database)

# Connect it to your workflow
workflow.set_entry_point("query")
workflow.add_edge("query", "your_next_node")
```

## Next steps

* View the [full example on GitHub](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/quickstart/langgraph)
* See our full [API documentation](https://docs.snowleopard.ai/index.md) to learn about the [Retrieve](https://docs.snowleopard.ai/cloud/endpoints/retrieve.md) and [Response](https://docs.snowleopard.ai/cloud/endpoints/response.md) endpoints
* Improve accuracy by giving Snow Leopard [feedback](https://docs.snowleopard.ai/cloud/endpoints/feedback.md) about your business logic and terminology
* See this quickstart in action with the [Financial Coach recipe](https://docs.snowleopard.ai/recipes/financial-coach.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.