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

# Data Agent (CopilotKit + Pydantic)

> Build a "chat with your data" application using CopilotKit and Pydantic AI.

An application that lets users chat with their data through a web UI. Built with [CopilotKit](https://copilotkit.ai/) for the frontend and [Pydantic AI](https://ai.pydantic.dev/) for the backend agent.

> [card] [Try the live demo](https://try.snowleopard.ai/demos/data-agent)
>
> ![Chat with your data agent screenshot](https://github.com/SnowLeopard-AI/snowy-examples/raw/main/agent_examples/chat_with_your_data_copilotkit/data_agent.png)

## What it does

This agent provides a chat interface for querying your data in natural language. Users can:

* Ask questions about their data in plain English
* View query results in a formatted table
* Have multi-turn conversations to drill down into the data

The agent translates natural language questions into SQL queries via Snow Leopard, executes them, and presents results in a user-friendly format.

## Architecture

The application has two main components:

### Frontend (Next.js + CopilotKit)

* **Next.js** provides the web application framework
* **CopilotKit** provides the chat UI components and handles communication with the backend agent
* **Data tables** display query results returned from the agent

### Backend (Pydantic AI + Snow Leopard)

* **Pydantic AI** agent with two tools:
  * `get_data`: Calls Snow Leopard to convert natural language to SQL and retrieve results
  * `read_get_data_response`: Allows paginated reading of large result sets
* **AG-UI state management** keeps the frontend in sync with query results
* **Snow Leopard** handles natural language to SQL conversion

## Run the example

### Prerequisites

* Python 3.12+
* Node.js 20+
* [uv](https://docs.astral.sh/uv/) package manager
* pnpm (or npm)
* OpenAI API key
* A [Snow Leopard Cloud](https://cloud.snowleopard.ai) instance and [API key](https://docs.snowleopard.ai/cloud/getting-started.md#api-keys)
* A PostgreSQL database that Snow Leopard Cloud can reach, such as a free [Neon](https://neon.com) or [Supabase](https://supabase.com) database. See the [PostgreSQL setup guide](https://github.com/SnowLeopard-AI/snowy-examples/blob/main/docs/postgres-setup.md) for details.

> [!NOTE]
>
> Don't have data? This example uses Northwind, a public sales database. Our [northwind\_psql](https://github.com/SnowLeopard-AI/northwind_psql) repository has a `northwind.sql` script that loads it into PostgreSQL.

### Setup

Clone the [repository](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/agent_examples/chat_with_your_data_copilotkit):

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

Install dependencies:

```bash theme={null}
pnpm install
```

Load the Northwind dataset into your PostgreSQL database:

```bash theme={null}
psql "postgresql://USER:PASSWORD@HOST/DBNAME?sslmode=require" -f northwind.sql
```

In [Snow Leopard Cloud](https://cloud.snowleopard.ai), [add the database as a data source](https://docs.snowleopard.ai/cloud/getting-started.md#adding-a-data-source) to your instance. Then create an API key on the **Keys** tab and copy the instance ID from the **Connection Info** tab.

Create a `.env` file in the `agent` folder:

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

Start the development server:

```bash theme={null}
pnpm dev
```

This starts both the UI and agent servers. Open [http://localhost:3000](http://localhost:3000) to start chatting with your data.

## Next steps

* View the [full source code on GitHub](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/agent_examples/chat_with_your_data_copilotkit)
* Learn more about [CopilotKit](https://docs.copilotkit.ai/)
* Explore the [Pydantic AI quickstart](https://docs.snowleopard.ai/quickstarts/pydantic-ai.md)


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