pandas-ai icon indicating copy to clipboard operation
pandas-ai copied to clipboard

sandbox: AttributeError: 'bool' object has no attribute 'upper'

Open tomconte opened this issue 1 week ago • 0 comments

System Info

❯ python --version Python 3.11.9 ❯ pip freeze | grep pandasai pandasai==3.0.0b11 pandasai-docker==0.1.2 pandasai-openai==0.1.4

🐛 Describe the bug

Sometimes, when using the sandbox, the following failure happens.

Here is what I did:

class PandasAIAnalyzer:
    def __init__(self):
        # Initialize PandasAI with Azure LLM
        llm = PandasOpenAI(
            azure_endpoint=azure_endpoint,
            api_token=api_key,
            api_version=api_version,
            deployment_name=azure_deployment,
        )
        pandasai.config.set({"llm": llm})
        # Start the Docker sandbox
        self.sandbox = DockerSandbox()
        self.sandbox.start()
    
    def analyze(self, df: pd.DataFrame, question: str) -> pandasai.dataframe.base.BaseResponse:
        try:
            pai_df = pandasai.DataFrame(df)
            agent = pandasai.Agent(pai_df, sandbox=self.sandbox)
            result = agent.chat(question)
            return result
        except Exception as e:
            # Display full trace
            traceback.print_exc()
            return f"Error in PandasAI analysis: {str(e)}"

df is a simple dataframe with Timestamp/Value. The question was "Does the time series show any unusual trends?".

And the error:

Traceback (most recent call last):
  File "/var/folders/fy/2zls_q7526d7rk8xmp_kkcjh0000gn/T/ipykernel_29184/1296646968.py", line 29, in analyze
    result = agent.chat(question)
             ^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/agent/base.py", line 92, in chat
    return self._process_query(query, output_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/agent/base.py", line 262, in _process_query
    result = self.execute_with_retries(code)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/agent/base.py", line 177, in execute_with_retries
    result = self.execute_code(code)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/agent/base.py", line 127, in execute_code
    return self._sandbox.execute(code, code_executor.environment)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/sandbox/sandbox.py", line 19, in execute
    return self._exec_code(code, environment)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai_docker/docker_sandbox.py", line 119, in _exec_code
    sql_queries = self._extract_sql_queries_from_code(code)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/sandbox/sandbox.py", line 63, in _extract_sql_queries_from_code
    SQLQueryExtractor().visit(tree)
  File "/Users/tconte/.pyenv/versions/3.11.9/lib/python3.11/ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/Users/tconte/.pyenv/versions/3.11.9/lib/python3.11/ast.py", line 426, in generic_visit
    self.visit(item)
  File "/Users/tconte/.pyenv/versions/3.11.9/lib/python3.11/ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/Users/tconte/.pyenv/versions/3.11.9/lib/python3.11/ast.py", line 428, in generic_visit
    self.visit(value)
  File "/Users/tconte/.pyenv/versions/3.11.9/lib/python3.11/ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/Users/tconte/src/Cognite/cog-ai/.venv-notebooks/lib/python3.11/site-packages/pandasai/sandbox/sandbox.py", line 57, in visit_Call
    if "SELECT" in arg.s.upper():
                   ^^^^^^^^^^^
AttributeError: 'bool' object has no attribute 'upper'

tomconte avatar Feb 18 '25 16:02 tomconte