SyntaxError in llm generated code when using pandas dataframe agent #25232
Replies: 1 comment
-
Hey there, @santhoshraje! I'm here to assist you with any issues you might have. Let's tackle this bug together! The To resolve this, you can try the following steps:
Here is an example of how you might modify your code to handle incomplete code generation: from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
import pandas as pd
from langchain_ollama import ChatOllama
df = pd.read_csv(
"https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv"
)
llm = ChatOllama(
model="llama3.1:latest",
temperature=1,
num_predict=-1,
cache=False,
num_ctx=30000,
)
def safe_invoke(agent, query):
try:
return agent.invoke(query)
except SyntaxError as e:
print(f"SyntaxError encountered: {e}")
# Handle the error or correct the code here
return None
agent = create_pandas_dataframe_agent(
llm,
df=df,
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
allow_dangerous_code=True,
)
result = safe_invoke(agent, "how many rows are there?")
print(result) This approach allows you to catch and handle If the issue persists, you might want to follow up on the LangChain repository for any updates or further assistance. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
For some reason, it looks like the llm generated code is giving a syntax error. It looks like the code is missing the closing parentheses. And then the agent goes in a loop and the chain just ends with no answer. Why is this happening?
System Info
langchain==0.2.12
langchain-community==0.2.11
langchain-core==0.2.28
langchain-experimental==0.0.64
langchain-ollama==0.1.1
langchain-text-splitters==0.2.2
Platform: mac
Python version: 3.9.13
Beta Was this translation helpful? Give feedback.
All reactions