How to fix Gemini API SQL query formatting issues in Langchain

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.

I’ve hit this exact problem switching between LLM providers. Gemini wraps code in markdown by default - great for reading, terrible for execution.

Quick fix: add a cleaning function before your tool runs. Strip the markdown with regex:

import re

def clean_sql_output(sql_string):
cleaned = re.sub(r’sql\n|\n|```', ‘’, sql_string)
return cleaned.strip()

generated_sql = clean_sql_output(generated_sql)
query_tool.invoke(generated_sql)

Or just change your prompt. Add “Return only the SQL query without markdown formatting or code blocks” to your template. Usually works with Gemini.

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!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.