[X] I added a very descriptive title to this issue.
[X] I searched the LangChain documentation with the integrated search.
[X] I used the GitHub search to find a similar question and didn't find it.
[X] I am sure that this is a bug in LangChain rather than my code.
[X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
import os
from langchain.chains.sql_database.query import create_sql_query_chain
from langchain_community.utilities import SQLDatabase
from langchain_openai import ChatOpenAI
db = SQLDatabase.from_uri(f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}/{db_name}")
os.environ["OPENAI_API_KEY"] = ""
llm = ChatOpenAI(model="gpt-4o")
chain = create_sql_query_chain(llm, db)
response = chain.invoke({"question": "How many users are there"})
print(response)
Error Message and Stack Trace (if applicable)
"```sql
SELECT COUNT(*) AS "user_count"
FROM "users";
### Description
I'm trying to create an NL2SQL model with Lang chain with Postgres SQL database.
So I've expected a SQL query as plain text as the output but it returns a Query with markdowns that will cause issues when executing it
### System Info
platform: windows
python: 3.12
Checked other resources
Example Code
import os
from langchain.chains.sql_database.query import create_sql_query_chain from langchain_community.utilities import SQLDatabase from langchain_openai import ChatOpenAI
db_user = "" db_password = "" db_host = "" db_name = ""
db = SQLDatabase.from_uri(f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}/{db_name}")
os.environ["OPENAI_API_KEY"] = ""
llm = ChatOpenAI(model="gpt-4o")
chain = create_sql_query_chain(llm, db) response = chain.invoke({"question": "How many users are there"}) print(response)
Error Message and Stack Trace (if applicable)
"```sql SELECT COUNT(*) AS "user_count" FROM "users";