crewAI
crewAI copied to clipboard
[BUG] - nl2sql tool is failing.
Description
I am trying to connect to a Singlestore database with CrewAI's NL2SQL tool and I see this error
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 DISTINC...store_name FROM stores'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing.
Tool NL2SQLTool accepts these inputs: Converts natural language to SQL queries and executes them..
My code is pretty simple and straight forward, I used basic SQLAlchemy executor to verify the DB connection as well.
Steps to Reproduce
Use my code above.
Expected behavior
The Nl2sql tool must work properly when used as per the example.
Screenshots/Code snippets
from crewai import Agent, Crew, Task
from crewai_tools import NL2SQLTool
DATABASE_URI = f"mysql+pymysql://{db_cfgs["db_username"]}:{encoded_password}@{db_cfgs["db_host"]}:3306/{db_cfgs["db_name"]}"
# Define file paths for YAML configurations
files = {
'agents': 'config/agents.yaml',
'tasks': 'config/tasks.yaml'
}
# Load configurations from YAML files
configs = {}
for config_type, file_path in files.items():
with open(file_path, 'r') as file:
configs[config_type] = yaml.safe_load(file)
# Assign loaded configurations to specific variables
agents_config = configs['agents']
tasks_config = configs['tasks']
# create Agent
agent_text_to_sql = Agent(
config=agents_config['agent_text_to_sql'],
allow_delegation=False,
tools=[nl2sql]
)
# create Task
task_extract_data = Task(
config=tasks_config['task_extract_data'],
agent=agent_text_to_sql
)
# Creating Crew
crew = Crew(
agents=[
agent_text_to_sql
],
tasks=[
task_extract_data
],
verbose=True
)
# The given Python dictionary
inputs = {
"sql_query": "find all distinct stores"
}
# Run the crew
result = crew.kickoff(
inputs=inputs
)```
### Operating System
MacOS
### Python Version
3.12
### crewAI Version
0.95.0
### crewAI Tools Version
0.25.8
### Virtual Environment
Venv
any help on this?
i am also facing the same issue when i try to implement NL2Sql_tool:
@Neerajkumar12 please add a +1 to the bug above to come up on the issue board for them.
same issue
Bump! Any help?
The error statement seems to be saying that the required “sql_query” key is missing.
I'm having the same problem, the query is being generated as “query” instead of “sql_query”.
I solved this problem by adding the following to the function def _parse_args(self, raw_args: Union[str, dict]) -> dict in structured_tool.py
if('query' in raw_args):
raw_args['sql_query'] = raw_args.pop('query')
I put in the code to make it temporarily bypass it.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
How can we connect the tool with the SQL database?
How can we connect the tool with the SQL database?
Here's how to connect. Let me know if it's enough
Hey everyone,
This is happening because the LLM is generating a response using the query key instead of sql_query, which is what's required by the Tool.
This can likely be fixed with prompt engineering. You can update your agent or task description to instruct the model to pass the input as:
{'sql_query': <your_query>}
That should help ensure compatibility with the tool.
Here is a example:
from crewai import Agent, Crew, Process, Task
from crewai_tools import NL2SQLTool
# Define database connection
nl2sql = NL2SQLTool(db_uri="postgresql://postgres@localhost:5432/postgres")
# Define the Researcher agent
researcher = Agent(
role="Database Researcher",
goal="Convert natural language questions into SQL queries and retrieve data from the database.",
backstory=(
"An expert in database analysis, capable of translating human requests into precise SQL queries."
),
tools=[nl2sql],
allow_delegation=False,
verbose=True,
)
# Define a Task for the agent
query_task = Task(
description=(
"Retrieve data from the database based on a given question: {question}"
"Use NL2SQLTool correctly by passing the input as {'sql_query': <your_query>}."
),
expected_output="A structured response with the requested data from the database.",
agent=researcher,
)
# Create the Crew
crew = Crew(
agents=[researcher],
tasks=[query_task],
process=Process.sequential, # Tasks are executed one after the other
verbose=True,
)
# Run the Crew with an example input
result = crew.kickoff(
inputs={"question": "how many patients are there in the database?"}
)
print(result)