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/ParameterDescription
dataherald_api_keyAPI key for authentication with Dataherald service (optional if set as environment variable)
db_connection_idRequired 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:

  1. Install the required package:
pip install dataherald
  1. Sign up for a Dataherald account at dataherald.com
  2. Create an API key
  3. Set up your environment variable:
export DATAHERALD_API_KEY="your-api-key"

Basic Usage

Here's a simple example of using the wrapper to convert a natural language question to SQL:

from langchain_community.utilities.dataherald import DataheraldAPIWrapper

# Initialize the wrapper
dataherald = DataheraldAPIWrapper(
    db_connection_id="your-db-connection-id"
)

# Convert natural language to SQL
sql_query = dataherald.run("How many employees are in the company?")
print(sql_query)

Integration with LangChain Agents

The wrapper can be integrated into a LangChain agent system for more complex workflows:

from langchain_community.utilities.dataherald import DataheraldAPIWrapper
from langchain_community.tools.dataherald.tool import DataheraldTextToSQL
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent

# Set up the components
api_wrapper = DataheraldAPIWrapper(db_connection_id="your-db-connection-id")
tool = DataheraldTextToSQL(api_wrapper=api_wrapper)
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

# Create and configure the agent
tools = [tool]
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
    agent=agent, 
    tools=tools, 
    verbose=True, 
    handle_parsing_errors=True
)

# Use the agent
result = agent_executor.invoke({
    "input": "Return the sql for this question: How many employees are in the company?"
})

Error Handling

When using the DataheraldAPIWrapper, it's important to handle potential errors:

from langchain_community.utilities.dataherald import DataheraldAPIWrapper

try:
    dataherald = DataheraldAPIWrapper(db_connection_id="your-db-connection-id")
    sql_query = dataherald.run("How many employees are in the company?")
except Exception as e:
    print(f"An error occurred: {str(e)}")

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 Docs

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

LangChain Docs