{"record":{"id":"67fb14c5450d05d2","repo":"zylon-ai/private-gpt","slug":"failed-to-generate-sql-query","errorCode":null,"errorMessage":"Failed to generate SQL query","messagePattern":"Failed to generate SQL query","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/tabular/database_query_generator.py","lineNumber":909,"sourceCode":"                system=ResolvedSystemConfig(\n                    prompt=system_prompt, use_default_prompt=False\n                ),\n                condensation=CondensationConfig(enabled=False),\n                sampling_params=sampling_params,\n            )\n        )\n\n        # find the first text block in the response\n        for block in response.content:\n            if isinstance(block, TextBlock):\n                raw_text = block.text\n                # despite the instructions, the LLM might\n                # generate markdown like ```sql ... ```\n                # so we try to extract the SQL code from it\n                # remove the prefix and suffix if present\n                return self._extract_sql_code(raw_text, transpile_sql=False)\n\n        raise ValueError(\"Failed to generate SQL query\")\n\n    def _transpile_sql(self, sql: str) -> str:\n        if not self._dialect:\n            return sql\n\n        for read_dialect in Dialects:\n            if read_dialect.value == self._dialect:\n                continue\n\n            with contextlib.suppress(ParseError):\n                result = \"\\n\".join(\n                    sqlglot.transpile(sql, read=read_dialect, write=self._dialect)\n                )\n\n                if result:\n                    return result\n\n        try:","sourceCodeStart":891,"sourceCodeEnd":927,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/tabular/database_query_generator.py#L891-L927","documentation":"Raised after a successful LLM chat call when none of the response content blocks is a TextBlock — i.e. the model returned only non-text content (empty response, tool calls, or an unexpected block type). The SQL extraction path never gets text to parse, so the generator gives up with this generic error instead of returning empty SQL.","triggerScenarios":"The chat service response contains zero TextBlock entries: model emitted an empty completion, only tool-use blocks, or the response was truncated to nothing by sampling limits (e.g. max_tokens consumed by reasoning).","commonSituations":"max_tokens budget from schema packing (see the 20% buffer logic) leaving almost nothing for output; a misrouted model that answers with tool calls; provider returning an empty delta list.","solutions":["Retry the generation request — transient empty completions often succeed on a second attempt","Free up output tokens: reduce schema size / history so available_tokens for generation is comfortably large","Inspect response.content types in a debug hook to confirm which block type the model actually returned and adjust prompt/model accordingly"],"exampleFix":"# before\nresult = await generator.generate(...)  # raises ValueError\n# after\nfor attempt in range(2):\n    try:\n        result = await generator.generate(...)\n        break\n    except ValueError as e:\n        if \"Failed to generate SQL\" not in str(e) or attempt == 1:\n            raise","handlingStrategy":"retry","validationCode":"if not response.content or not any(\n    hasattr(b, \"text\") for b in response.content\n):\n    raise ValueError(\"model returned no text block; retrying\")","typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        return await generator.generate(question)\n    except ValueError as e:\n        if \"Failed to generate SQL\" not in str(e) or attempt == 2:\n            raise\n        await asyncio.sleep(2**attempt)","preventionTips":["Keep the generation max_tokens budget comfortably large (shrink schema, not output)","Log response.content block types when debugging empty generations"],"tags":["database","text-to-sql","llm","empty-response","retry"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}