zylon-ai/private-gpt · error · ValueError

Failed to execute some SQL queries: {', '.join(str(e) for e

Error message

Failed to execute some SQL queries: {', '.join(str(e) for e in exceptions)}

What it means

Raised inside _exec_code() after pre-processing the generated code: the adapter extracts embedded SQL queries, executes them locally (via _process_sql_queries), and if any of those executions raised, the exceptions are aggregated into one ValueError. This means the LLM-generated Python contained SQL that failed against the actual database before remote execution was attempted.

Source

Thrown at private_gpt/components/tabular/pandasai_sandbox.py:281

                logger.warning("Environment setup warning: %s", result.error)
        except Exception as e:
            logger.warning("Error during environment setup: %s", e)

    # ------------------------------------------------------------------
    # Code execution
    # ------------------------------------------------------------------

    def _exec_code(self, code: str, environment: dict[str, Any]) -> dict[str, Any]:
        if not self._client:
            raise RuntimeError("Sandbox not started. Call start() first.")

        try:
            sql_queries = self._extract_sql_queries_from_code(code)
            datasets_code, exceptions = self._process_sql_queries(
                sql_queries, environment
            )
            if exceptions:
                raise ValueError(
                    f"Failed to execute some SQL queries: "
                    f"{', '.join(str(e) for e in exceptions)}"
                )

            processed_code = self._prepare_code_for_execution(code)
            full_code = "\n\n".join(
                part for part in (self._PREAMBLE, datasets_code, processed_code) if part
            )

            execution_result = self._run(
                self._client.run_code(
                    full_code,
                    SandboxCodeOptions(language="python", timeout=self._timeout),
                )
            )
            return self._process_execution_result(execution_result)

        except Exception as e:

View on GitHub (pinned to 4a030776a3)

Solutions

  1. Read the aggregated messages — each embedded exception names the exact SQL failure.
  2. For hallucinated schema: strengthen the prompt with the real schema/DDL, or verify the train dataframe metadata exposed to PandasAI.
  3. For permissions: grant SELECT on the referenced tables to the service DB user.
  4. For transient DB errors: retry the chat call; PandasAI regenerates code each time.
  5. If a specific query is known-bad, block it via a validation hook before execution.

Example fix

# before
result = service.chat(query, dfs, sandbox=sandbox)  # ValueError: Failed to execute some SQL queries: ...

# after
try:
    result = service.chat(query, dfs, sandbox=sandbox)
except ValueError as e:
    if "Failed to execute some SQL queries" in str(e):
        # surface the per-query failures to the user / regenerate with schema hint
        logger.warning("SQL pre-execution failed: %s", e)
        raise RetryableAnalysisError(str(e)) from e
    raise
Defensive patterns

Strategy: try-catch

Try / catch

try:
    result = service.chat(query, dfs, sandbox=sandbox)
except ValueError as e:
    if "Failed to execute some SQL queries" in str(e):
        # each embedded exception is in the message; regenerate with schema feedback
        raise RetryableAnalysisError(str(e)) from e
    raise

Prevention

When it happens

Trigger: PandasAI-generated code contains SQL queries (to be replaced with pre-fetched CSVs) that fail: syntax errors, unknown tables/columns, permission errors, or connection issues in _process_sql_queries. Any non-empty exceptions list triggers the raise.

Common situations: The LLM hallucinates table or column names; the connected DB user lacks SELECT rights; schema drift between prompt context and the live database; DB connection pool exhaustion while running multiple extracted queries.

Related errors


AI-assisted analysis of zylon-ai/private-gpt@4a030776a3 (2026-08-15). Data as JSON: /api/errors/0c827eb035661d82. Report an issue: GitHub.