I am using this crewai’s Nl2SQL tool for a SQL query generation task, sometimes the tool’s behaves like the NL2SQLTool
receives the full SQL query as seen in the Tool Input, but the debug log prints only a truncated portion, often stopping at ...AND date =
, leading to execution failure. Behaviour Differs Between Standalone and FastAPI: The same agent, prompt, and code work perfectly when executed as a standalone script but fail when used within a FastAPI-based environment using .kickoff()
. Why is it happening?
- You can see in the screenshot clearly that the tool input has the full SQL query, but the output shows that only half of the query is used during execution
- I opened the tools code and used a print statement there, and that also clearly shows that only half query is going to the tool while execution.
Hi @Brinil_John, sorry I don’t have an answer but I do have a question - how did you get NL2SQLTool to populate sql_query? I’ve posted my question in detail and it’s clear that you’ve got it working, albeit with some truncation issues under certain conditions unfortunately…
Add this below given line in your task description in the top:
Use NL2SQLTool correctly by passing the input as ‘sql_query’: <your_query>.
Always return the SQL query as a JSON object using this format:
{{
"sql_query": "<FULL SQL QUERY>"
}}
Thanks @Brinil_John, I’m not sure I understood your reply. I tried several variations on your suggestion but none of them allowed me to get beyond that “validation error”.
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 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...6956',
'metadata': {}}}}, input_type=dict] │
│ For further information visit https://errors.pydantic.dev/2.11/v/missing │
│ Tool Args: │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
When you say <FULL SQL QUERY> are you saying that I should enter the SQL query myself or let NL2SQLTool generate it? I ask because the NL2SQLTool docs say “This tool is used to convert natural language to SQL queries. When passed to the agent it will generate queries and then use them to interact with the database.”
It sounded to me like you meant I should update my tasks.yaml file to look like -
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.
Always return the SQL query as a JSON object using this format:
{{
"sql_query": "SELECT last_name FROM entry_detail;"
}}
expected_output: >
The {topic} employee data will be output in markdown format.
agent: data_engineer
but that continues to generate that validation error.
Yes add that as the starting line of your task description also add this line in the top of your task:
”Use NL2SQLTool correctly by passing the input as ‘sql_query’: <your_query>.”