DuckDB Agent
DuckDB database tools for AI agents.
Prerequisites
- Python 3.10 or higher
- PraisonAI Agents package installed
duckdb
package installed- Basic understanding of SQL and databases
DuckDB Tools
Use DuckDB Tools to manage and query databases with AI agents.
Install Dependencies
First, install the required packages:
Import Components
Import the necessary components:
Create Agent
Create a DuckDB database agent:
Define Task
Define the database task:
Run Agent
Initialize and run the agent:
Available Functions
Function Details
execute_query(query: str, params: Optional[Union[tuple, dict]] = None, return_df: bool = True)
Executes SQL queries with advanced features:
- Supports parameterized queries
- Returns results as DataFrame records or raw tuples
- Full SQL query support (SELECT, INSERT, UPDATE, DELETE, etc.)
- Automatic connection management
load_csv(table_name: str, filepath: str, schema: Optional[Dict[str, str]] = None, if_exists: str = ‘replace’)
Loads CSV files into DuckDB tables:
- Optional schema definition
- Flexible table existence handling
- Automatic type inference
- Support for large files
export_csv(query: str, filepath: str, params: Optional[Union[tuple, dict]] = None)
Exports query results to CSV files:
- Supports parameterized queries
- Automatic header generation
- Configurable output formatting
- Large result set handling
Understanding DuckDB Tools
What are DuckDB Tools?
DuckDB Tools provide database capabilities for AI agents:
- SQL query execution
- Data import/export
- Schema management
- Data analysis
- Performance optimization
Key Components
DB Agent
Create specialized database agents:
DB Task
Define database tasks:
Process Types
Sequential or parallel processing:
DB Options
Customize database parameters:
Examples
Basic Database Agent
Advanced Database Operations with Multiple Agents
Dependencies
The DuckDB tools require the following Python packages:
- duckdb: For database operations
- pandas: For data manipulation and CSV handling
These will be automatically installed when needed.
Example Agent Configuration
Error Handling
All functions include comprehensive error handling:
- Database connection errors
- Query syntax errors
- File I/O errors
- Schema validation errors
- Missing dependency errors
Errors are logged and returned in a consistent format:
- Success cases return the expected data type
- Error cases return a dict with an “error” key containing the error message
Common Use Cases
- Data Analysis:
- Data Export:
- Data Transformation:
Best Practices
Common Patterns
Database Operation Pipeline
Was this page helpful?