-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Getting validation error for SQL Database, type=value_error.missing, when its trying to list tables in the db #20405
Comments
The patch I created mentioned, but it seems unrelated to this issue. |
Hey @os1ma , It can be possible, if there is some dependency issue, so when I upgrade the langchain_openai to latest version to use gpt-4-turbo, I am not able to run the sql_agent:
and when I downgrade it to langchain_openai==0.0.8, then I am not able to use gpt-4-turbo. Its a strange issue but mostly something related to the dependency issue or something else maybe. |
I was curious about this error, so I looked into it and found the cause. If anything, it is more of a gpt-4-turbo response issue than a LangChain issue. Based on the SQL Agent Document, I tried the following code and encountered the same error as you. from langchain_community.agent_toolkits.sql.base import create_sql_agent
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_openai import ChatOpenAI
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(model="gpt-4-turbo", temperature=0)
agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True)
result = agent_executor.invoke("List the total sales per country. Which country's customers spent the most?")
print(result["output"])
The follosing error occurs using gpt-4-turbo, but this error does not occur using gpt-3.5-turbo.
The reason for this is the difference in the OpenAI API response. gpt-3.5-turbo returns the following response
gpt-4-turbo returns the following response
LangSmith or the following code will allow you to see the difference of those responses. This code emulates LangChain SQL Agent behavior. from openai import OpenAI
MODEL = "gpt-3.5-turbo-0125"
message_dicts = [
{
"content": 'You are an agent designed to interact with a SQL database.\nGiven an input question, create a syntactically correct sqlite query to run, then look at the results of the query and return the answer.\nUnless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 10 results.\nYou can order the results by a relevant column to return the most interesting examples in the database.\nNever query for all the columns from a specific table, only ask for the relevant columns given the question.\nYou have access to tools for interacting with the database.\nOnly use the below tools. Only use the information returned by the below tools to construct your final answer.\nYou MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.\n\nDO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.\n\nIf the question does not seem related to the database, just return "I don\'t know" as the answer.\n',
"role": "system",
},
{
"content": "List the total sales per country. Which country's customers spent the most?",
"role": "user",
},
{
"content": "I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables.",
"role": "assistant",
},
]
params = {
"model": MODEL,
"stream": True,
"n": 1,
"temperature": 0.0,
"tools": [
{
"type": "function",
"function": {
"name": "sql_db_query",
"description": "Input to this tool is a detailed and correct SQL query, output is a result from the database. If the query is not correct, an error message will be returned. If an error is returned, rewrite the query, check the query, and try again. If you encounter an issue with Unknown column 'xxxx' in 'field list', use sql_db_schema to query the correct table fields.",
"parameters": {
"type": "object",
"properties": {
"query": {
"description": "A detailed and correct SQL query.",
"type": "string",
}
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "sql_db_schema",
"description": "Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables. Be sure that the tables actually exist by calling sql_db_list_tables first! Example Input: table1, table2, table3",
"parameters": {
"type": "object",
"properties": {
"table_names": {
"description": "A comma-separated list of the table names for which to return the schema. Example input: 'table1, table2, table3'",
"type": "string",
}
},
"required": ["table_names"],
},
},
},
{
"type": "function",
"function": {
"name": "sql_db_list_tables",
"description": "Input is an empty string, output is a comma-separated list of tables in the database.",
"parameters": {
"type": "object",
"properties": {
"tool_input": {
"description": "An empty string",
"type": "string",
}
},
"required": ["tool_input"],
},
},
},
{
"type": "function",
"function": {
"name": "sql_db_query_checker",
"description": "Use this tool to double check if your query is correct before executing it. Always use this tool before executing a query with sql_db_query!",
"parameters": {
"type": "object",
"properties": {
"query": {
"description": "A detailed and SQL query to be checked.",
"type": "string",
}
},
"required": ["query"],
},
},
},
],
}
client = OpenAI()
with client.chat.completions.create(messages=message_dicts, **params) as response:
for chunk in response:
print(chunk) As following code,
I have tried it and it works fine on gpt-4-turbo with the following patch in the tool. class _ListSQLDataBaseToolInput(BaseModel):
- tool_input: str = Field(..., description="An empty string")
+ tool_input: str = Field("", description="An empty string") I will try to create a pull request of this patch, but I am not sure if the LangChain team will accept it, since the root cause is a response issue of gpt-4-turbo. |
I think langchain team should accept the PR raised by you because, though, the function calling is being done, but for some reason Though maybe we need to take this issue to openai as well? Because |
…l_input (#20409) **Description:** `_ListSQLDatabaseToolInput` raise error if model returns `{}`. For example, gpt-4-turbo returns `{}` with SQL Agent initialized by `create_sql_agent`. So, I set default value `""` for `_ListSQLDatabaseToolInput` tool_input. This is actually a gpt-4-turbo issue, not a LangChain issue, but I thought it would be helpful to set a default value `""`. This problem is discussed in detail in the following Issue. **Issue:** #20405 **Dependencies:** none Sorry, I did not add or change the test code, as tests for this components was not exist . However, I have tested the following code based on the [SQL Agent Document](https://python.langchain.com/docs/use_cases/sql/agents/), to make sure it works. ``` from langchain_community.agent_toolkits.sql.base import create_sql_agent from langchain_community.utilities.sql_database import SQLDatabase from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri("sqlite:///Chinook.db") llm = ChatOpenAI(model="gpt-4-turbo", temperature=0) agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True) result = agent_executor.invoke("List the total sales per country. Which country's customers spent the most?") print(result["output"]) ```
This is merged! 😄 But I think it will take a little while to publish the package. |
…l_input (langchain-ai#20409) **Description:** `_ListSQLDatabaseToolInput` raise error if model returns `{}`. For example, gpt-4-turbo returns `{}` with SQL Agent initialized by `create_sql_agent`. So, I set default value `""` for `_ListSQLDatabaseToolInput` tool_input. This is actually a gpt-4-turbo issue, not a LangChain issue, but I thought it would be helpful to set a default value `""`. This problem is discussed in detail in the following Issue. **Issue:** langchain-ai#20405 **Dependencies:** none Sorry, I did not add or change the test code, as tests for this components was not exist . However, I have tested the following code based on the [SQL Agent Document](https://python.langchain.com/docs/use_cases/sql/agents/), to make sure it works. ``` from langchain_community.agent_toolkits.sql.base import create_sql_agent from langchain_community.utilities.sql_database import SQLDatabase from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri("sqlite:///Chinook.db") llm = ChatOpenAI(model="gpt-4-turbo", temperature=0) agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True) result = agent_executor.invoke("List the total sales per country. Which country's customers spent the most?") print(result["output"]) ```
…l_input (langchain-ai#20409) **Description:** `_ListSQLDatabaseToolInput` raise error if model returns `{}`. For example, gpt-4-turbo returns `{}` with SQL Agent initialized by `create_sql_agent`. So, I set default value `""` for `_ListSQLDatabaseToolInput` tool_input. This is actually a gpt-4-turbo issue, not a LangChain issue, but I thought it would be helpful to set a default value `""`. This problem is discussed in detail in the following Issue. **Issue:** langchain-ai#20405 **Dependencies:** none Sorry, I did not add or change the test code, as tests for this components was not exist . However, I have tested the following code based on the [SQL Agent Document](https://python.langchain.com/docs/use_cases/sql/agents/), to make sure it works. ``` from langchain_community.agent_toolkits.sql.base import create_sql_agent from langchain_community.utilities.sql_database import SQLDatabase from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri("sqlite:///Chinook.db") llm = ChatOpenAI(model="gpt-4-turbo", temperature=0) agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True) result = agent_executor.invoke("List the total sales per country. Which country's customers spent the most?") print(result["output"]) ```
…l_input (#20409) **Description:** `_ListSQLDatabaseToolInput` raise error if model returns `{}`. For example, gpt-4-turbo returns `{}` with SQL Agent initialized by `create_sql_agent`. So, I set default value `""` for `_ListSQLDatabaseToolInput` tool_input. This is actually a gpt-4-turbo issue, not a LangChain issue, but I thought it would be helpful to set a default value `""`. This problem is discussed in detail in the following Issue. **Issue:** #20405 **Dependencies:** none Sorry, I did not add or change the test code, as tests for this components was not exist . However, I have tested the following code based on the [SQL Agent Document](https://python.langchain.com/docs/use_cases/sql/agents/), to make sure it works. ``` from langchain_community.agent_toolkits.sql.base import create_sql_agent from langchain_community.utilities.sql_database import SQLDatabase from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri("sqlite:///Chinook.db") llm = ChatOpenAI(model="gpt-4-turbo", temperature=0) agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True) result = agent_executor.invoke("List the total sales per country. Which country's customers spent the most?") print(result["output"]) ```
Ran into this error while testing turbo today. Looking out for the new release. Thanks ! |
Checked other resources
Example Code
I have the following code:
Error Message and Stack Trace (if applicable)
Invoking:
sql_db_list_tables
with{}
ERROR:
1 validation error for _ListSQLDataBaseToolInput
tool_input
field required (type=value_error.missing)
Description
I recently updated langchain and pydantic to their latest version and my code broke and when using the sql agent I get input missing issue. I think the invoking of sql_db_list_tables is not putting any value into when trying to parse the description of table schema. And this is a bug from langchain library itself.
Kindly help into this issue, as this is becoming a blocker. Thanks.
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
The text was updated successfully, but these errors were encountered: