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

# LangChain

> Integrate Snow Leopard with LangChain agents for natural language data queries.

This guide shows you how to add Snow Leopard data retrieval to a [LangChain](https://python.langchain.com/) agent.

## Prerequisites

* Python 3.10+
* OpenAI API key (or another [supported model provider](https://python.langchain.com/docs/integrations/chat/))
* [Snow Leopard API key](https://auth.snowleopard.ai/account/api_keys)
* A datafile uploaded to [Snow Leopard Playground](https://try.snowleopard.ai)

<Note>
  Don't have data? Use our [sample superheroes dataset](https://github.com/SnowLeopard-AI/playground_datasets/raw/refs/heads/main/superheroes.db) to get started, or choose from our other [sample datasets](https://github.com/SnowLeopard-AI/playground_datasets/).
</Note>

## Start from scratch

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

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

Install dependencies:

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

Set your environment variables:

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

Run the example:

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

## Bring your own agent

Already have a LangChain agent? Add Snow Leopard data retrieval with these steps.

### 1. Install dependencies

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

### 2. Create the Snow Leopard tool

Create a LangChain `Tool` that calls Snow Leopard to retrieve data:

```python theme={null}
from langchain_core.tools import Tool
from snowleopard import SnowLeopardClient

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

def query_data(natural_language_query: str) -> str:
    """Query your database using natural language."""
    response = client.retrieve(
        datafile_id="{your-datafile-id}",
        user_query=natural_language_query
    )
    return str(response)

snowleopard_tool = Tool(
    name="query_database",
    func=query_data,
    description="Query your database with natural language questions. "
                "Describe your data here - this becomes part of the agent's context."
)
```

### 3. Add the tool to your agent

Add the Snow Leopard tool to your agent's tool list:

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o")
tools = [snowleopard_tool]  # Add to your existing tools

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant that can query databases."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
```

## Next steps

* View the [full example on GitHub](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/quickstart/langchain)
* See our full [API documentation](https://docs.snowleopard.ai/) to learn about the [Retrieve](/playground/endpoints/retrieve) and [Response](/playground/endpoints/response) endpoints
* Explore [Recipes](/recipes) for production-ready agent examples
