Working with DataheraldAPI in LangChain for Text-to-SQL
Posted: Feb 19, 2025.
The DataheraldAPIWrapper in LangChain provides an interface to interact with Dataherald's text-to-SQL service, allowing you to convert natural language questions into SQL queries programmatically.
What is DataheraldAPIWrapper?
DataheraldAPIWrapper is a utility class that wraps around Dataherald's API service, which specializes in converting natural language queries into SQL statements. This wrapper makes it easy to integrate Dataherald's text-to-SQL capabilities into your LangChain applications, allowing you to build natural language interfaces for database queries.
Reference
Method/Parameter | Description |
---|---|
dataherald_api_key | API key for authentication with Dataherald service (optional if set as environment variable) |
db_connection_id | Required identifier for the database connection in Dataherald |
run(prompt: str) | Generates a SQL query from a natural language prompt |
How to Use DataheraldAPIWrapper
Setup and Installation
Before using the wrapper, you need to:
- Install the required package:
- Sign up for a Dataherald account at dataherald.com
- Create an API key
- Set up your environment variable:
Basic Usage
Here's a simple example of using the wrapper to convert a natural language question to SQL:
Integration with LangChain Agents
The wrapper can be integrated into a LangChain agent system for more complex workflows:
Error Handling
When using the DataheraldAPIWrapper, it's important to handle potential errors:
The wrapper will raise validation errors if required parameters are missing or if there are issues with the API authentication.
By following these examples, you can effectively use the DataheraldAPIWrapper to convert natural language queries into SQL statements in your LangChain applications, whether as a standalone utility or as part of a larger agent-based system.
Remember to keep your API keys secure and never commit them directly in your code. Always use environment variables or secure secret management systems for handling sensitive credentials.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.