Langchain SQL Database Agent failed to find the view name in the MS SQL database.
Labels: name: area: agent, name: auto:bug

I am trying to build a langchain SQL database agent where I want to query only one view for now. I have mentioned the view name in the System Prompt and I have passed view_support=True to the SQLDatabase constructor. When I run the query agent is trying to find out the tables instead views. I am sensing that agent has no method that can fetch the view names of the database, it is trying to call 'sql_db_list_tables' and then sql_db_schema.

look at the code below:

```
import os
from dotenv import load_dotenv

load_dotenv()

from sqlalchemy import create_engine

odbc_str = 'mssql+pyodbc:///?odbc_connect=' \
                'Driver={ODBC Driver 18 for SQL Server}' \
                ';Server=tcp:' + os.getenv("DB_SERVER")+';PORT=1453' + \
                ';Authentication=ActiveDirectoryPassword' + \
                ';DATABASE=' + os.getenv("DB_NAME") + \
                ';Uid='+os.getenv("UID")'+ \
                ';Pwd='+os.getenv("PWD")'+ \
                ';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'
print(odbc_str)
db_engine = create_engine(odbc_str)

```

```
from langchain.chat_models import AzureChatOpenAI

llm = AzureChatOpenAI(
    openai_api_type=os.getenv("OPENAI_API_TYPE"),
    api_version=os.getenv("OPENAI_API_VERSION"),
    azure_deployment=os.getenv("DEPLOYMENT_NAME"),
    model=os.getenv("OPENAI_CHAT_MODEL"),
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    temperature=0,
    verbose= True)
```


```
from langchain.prompts.chat import ChatPromptTemplate

final_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", 
         """
          You are a helpful AI assistant expert in querying SQL Database to 
          find answers to user's question about database view: XXXXXXXXXX.vw_YYYYYYYYY
          where XXXXXXXXXX is the schema and vw_YYYYYYYYY is the view. You need to directly query 
          this view: XXXXXXXXXX.vw_YYYYYYYYY for all user's questions.
          Note: DO NOT EXECUTE DROP, DELETE or UPDATE SQL QUERIES.
         """
         ),
        ("user", "{question}\n ai: "),
    ]
)
```


```
from langchain.agents import AgentType, create_sql_agent
from langchain.sql_database import SQLDatabase
from langchain.agents.agent_toolkits.sql.toolkit import SQLDatabaseToolkit

db = SQLDatabase(db_engine, view_support=True)

sql_toolkit = SQLDatabaseToolkit(db=db, llm=llm)
sql_toolkit.get_tools()

sqldb_agent = create_sql_agent(
    llm=llm,
    toolkit=sql_toolkit,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    
)

sqldb_agent.run(final_prompt.format(
    question="What is the Exposure for id: 574182 by type ?"
))
```

AGENT ANSWER:

> Entering new AgentExecutor chain...
Action: sql_db_list_tables
Action Input: ""
Observation: xyz, abc, pqr
Thought:The table I need to query, XXXXXXXXXX.vw_YYYYYYYYY, is not listed in the database tables. I need to check the schema of the view to understand its structure and the fields it contains.
Action: sql_db_schema
Action Input: "XXXXXXXXXX.vw_YYYYYYYYY"
Observation: Error: table_names {'XXXXXXXXXX.vw_YYYYYYYYY'} not found in database
Thought:It seems like there is an error in finding the view XXXXXXXXXX.vw_YYYYYYYYY in the database. I need to inform the user about this.
Final Answer: I'm sorry, but it seems like the view 'XXXXXXXXXX.vw_YYYYYYYYY' does not exist in the database. Please check the view name and try again.

> Finished chain.

One option is to override the sql agent action methods or add new methods, but I dont want to do do that as per prompt instructions it should successfully find the view in the schema and query.

Please help anyone has achieved this. Thanks in advance.