I’m having trouble with different API responses when generating SQL queries
When I use OpenAI’s API, everything works perfectly and I get clean SQL output like this:
print(generated_sql)
'SELECT COUNT(*) FROM EMPLOYEE_DATA;'
But when I switch to Gemini API, the output includes extra formatting that breaks my code:
print(generated_sql)
'```sql\nSELECT COUNT(*) AS "total_rows"\nFROM EMPLOYEE_DATA;\n```'
This formatted output causes problems when I try to execute the query:
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool
query_tool = QuerySQLDataBaseTool(db=database)
query_tool.invoke(generated_sql)
The extra backticks and formatting characters make the query fail. Is there a simple way to strip out the markdown formatting and get just the raw SQL statement? I need the output to be clean like the OpenAI version.
yeah, gemini puts sql in markdown, just use generated_sql.strip('```sql').strip('```').strip() to clean it up b4 your query tool. works for me every time, hope it helps!