{"record":{"id":"0c827eb035661d82","repo":"zylon-ai/private-gpt","slug":"failed-to-execute-some-sql-queries-join-str","errorCode":null,"errorMessage":"Failed to execute some SQL queries: {', '.join(str(e) for e in exceptions)}","messagePattern":"Failed to execute some SQL queries: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/tabular/pandasai_sandbox.py","lineNumber":281,"sourceCode":"                logger.warning(\"Environment setup warning: %s\", result.error)\n        except Exception as e:\n            logger.warning(\"Error during environment setup: %s\", e)\n\n    # ------------------------------------------------------------------\n    # Code execution\n    # ------------------------------------------------------------------\n\n    def _exec_code(self, code: str, environment: dict[str, Any]) -> dict[str, Any]:\n        if not self._client:\n            raise RuntimeError(\"Sandbox not started. Call start() first.\")\n\n        try:\n            sql_queries = self._extract_sql_queries_from_code(code)\n            datasets_code, exceptions = self._process_sql_queries(\n                sql_queries, environment\n            )\n            if exceptions:\n                raise ValueError(\n                    f\"Failed to execute some SQL queries: \"\n                    f\"{', '.join(str(e) for e in exceptions)}\"\n                )\n\n            processed_code = self._prepare_code_for_execution(code)\n            full_code = \"\\n\\n\".join(\n                part for part in (self._PREAMBLE, datasets_code, processed_code) if part\n            )\n\n            execution_result = self._run(\n                self._client.run_code(\n                    full_code,\n                    SandboxCodeOptions(language=\"python\", timeout=self._timeout),\n                )\n            )\n            return self._process_execution_result(execution_result)\n\n        except Exception as e:","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/tabular/pandasai_sandbox.py#L263-L299","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the aggregated messages — each embedded exception names the exact SQL failure.","For hallucinated schema: strengthen the prompt with the real schema/DDL, or verify the train dataframe metadata exposed to PandasAI.","For permissions: grant SELECT on the referenced tables to the service DB user.","For transient DB errors: retry the chat call; PandasAI regenerates code each time.","If a specific query is known-bad, block it via a validation hook before execution."],"exampleFix":"# before\nresult = service.chat(query, dfs, sandbox=sandbox)  # ValueError: Failed to execute some SQL queries: ...\n\n# after\ntry:\n    result = service.chat(query, dfs, sandbox=sandbox)\nexcept ValueError as e:\n    if \"Failed to execute some SQL queries\" in str(e):\n        # surface the per-query failures to the user / regenerate with schema hint\n        logger.warning(\"SQL pre-execution failed: %s\", e)\n        raise RetryableAnalysisError(str(e)) from e\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = service.chat(query, dfs, sandbox=sandbox)\nexcept ValueError as e:\n    if \"Failed to execute some SQL queries\" in str(e):\n        # each embedded exception is in the message; regenerate with schema feedback\n        raise RetryableAnalysisError(str(e)) from e\n    raise","preventionTips":["Give the LLM accurate table/column metadata","Verify DB grants for the service user before analysis","Dry-run extracted SQL against the schema when feasible"],"tags":["sql","sandbox","llm-output","aggregated-errors","database"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}