Situation
I’ve been trying to get NL2SQLTool to work and have not had any luck, so I tried a very simple setup and am still stuck with a validation error. Hoping someone can help as I have not been able to find a working NL2SQLTool example.
This is my simplified agents.yaml -
data_engineer:
role: >
{topic} SQL Data Engineer
goal: >
Fetch employee data from MySQL.
Using data in a mysql database in table {table_name},
put together a list of employees' last names.
backstory: >
You are experienced with querying mysql tables.
And my simplified tasks.yaml -
query_task:
description: >
Query employees from a mysql database.
To accomplish this retrieve the {topic} data from the {table_name} table
and show a list of last names.
expected_output: >
The {topic} employee data will be output in markdown format.
agent: data_engineer
And finally the relevant portions of my crew.py -
from crewai_tools import NL2SQLTool
local_ollama_llm = LLM(model="ollama/crewai-llama3.2-3b:latest", base_url="http://localhost:11434")
nl2sql = NL2SQLTool(db_uri="mysql://timebill:******@127.0.0.1:3306/timebills")
@CrewBase
class Timebillcrew():
"""Timebillcrew crew"""
agents: List[BaseAgent]
tasks: List[Task]
@agent
def data_engineer(self) -> Agent:
return Agent(
config=self.agents_config["data_engineer"],
llm=local_ollama_llm,
tools=[nl2sql],
allow_delegation=False,
verbose=True,
)
@task
def query_task(self) -> Task:
return Task(
config=self.tasks_config["query_task"],
output_file="q.out",
)
@crew
def crew(self) -> Crew:
"""Creates the Timebillcrew crew"""
return Crew(
llm=local_ollama_llm,
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=True,
memory=True,
share_crew=False,
output_log_file="crew.log",
embedder={
"provider": "ollama",
"config": {
"model": "nomic-embed-text",
"task_type": "retrieval_document",
"url": "http://localhost:11434/api/embeddings",
}
}
)
And here’s the results of the crew run -
╭──────────────────────────────────────────────────────── 🤖 Agent Started ────────────────────────────────────────────────────────╮
│ │
│ Agent: Time Billing SQL Data Engineer │
│ │
│ Task: Query employees from a mysql database. To accomplish this retrieve the Time Billing data from the entry_detail table and │
│ show a list of last names. │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
🚀 Crew: crew
├── 📋 Task: query_task (ID: 260571c5-5971-4304-aabd-f01913edef4c)
│ Status: Executing Task...
│ └── 🔧 Failed NL2SQLTool (1)
└── ✅ Memory Retrieval Completed
└── Sources Used
├── ✅ Long Term Memory (0.53ms)
├── ✅ Short Term Memory (49.67ms)
└── ✅ Entity Memory (14.80ms)
╭─────────────────────────────────────────────────────────── Tool Error ───────────────────────────────────────────────────────────╮
│ │
│ Tool Usage Failed │
│ Name: NL2SQLTool │
│ Error: Arguments validation failed: 1 validation error for NL2SQLToolInput │
│ sql_query │
│ Field required [type=missing, input_value={'query': 'SELECT last_na...9482', 'metadata': {}}}}, input_type=dict] │
│ For further information visit https://errors.pydantic.dev/2.11/v/missing │
│ Tool Args: │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Summary
My understanding was that NL2SQLTool expects “natural language” input and it will generate the corresponding SQL. So I avoided providing the SQL directly and instead provided a natural language description of the data desired -
To accomplish this retrieve the {topic} data from the {table_name} table
and show a list of last names.
And I expected that it would produce -
SELECT last_name FROM entry_detail;
If I let the crew run for a while (looping on that error), then it eventually starts spitting out
╭──────────────────────────────────────────────────── 🔧 Agent Tool Execution ─────────────────────────────────────────────────────╮
│ │
│ Agent: Time Billing SQL Data Engineer │
│ │
│ Thought: Thought: I need to query the entry_detail table in the MySQL database to fetch employee data. │
│ │
│ Using Tool: NL2SQLTool │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────── Tool Input ───────────────────────────────────────────────────────────╮
│ │
│ "{\"query\": \"SELECT last_name FROM entry_detail\"}" │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────────────────────── Tool Output ───────────────────────────────────────────────────────────╮
│ │
│ │
│ I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for │
│ NL2SQLToolInput │
│ sql_query │
│ Field required [type=missing, input_value={'query': 'SELECT last_na...9482', 'metadata': {}}}}, input_type=dict] │
│ For further information visit https://errors.pydantic.dev/2.11/v/missing. │
│ Tool NL2SQLTool accepts these inputs: Converts natural language to SQL queries and executes them.. │
where it does seem to come up with the correct SQL query but continues erroring out with that validation error.
I must be doing something wrong and could really use some help.