{"record":{"id":"0bc8dc4d8e16d66c","repo":"jd-opensource/joyagent-jdgenie","slug":"llm-json","errorCode":null,"errorMessage":"解析llm json结果失败","messagePattern":"解析llm json结果失败","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"genie-tool/genie_tool/tool/table_rag/table_column_filter.py","lineNumber":202,"sourceCode":"            \"user_info\": self.user_info,\n            \"time_info\": self.time_info,\n            \"query\": self.query,\n            \"memory_info\": memory_info_str,\n            \"error_msg\": error_msg\n        }\n        table_rag_prompts = get_prompt(\"table_rag\")\n        prompt = Template(table_rag_prompts[\"column_filter_prompt\"]).render(info_dict)\n        \n        return prompt\n    \n    def _parse_json_result(self, result_str):\n        pattern = r'```json\\s*([\\s\\S]*?)\\s*```'\n        try:\n            result = re.findall(pattern, result_str)\n            return result[0]\n        except Exception as e:\n            logger.error(f\"生成结果格式不合法:{result_str}，{e}\")\n            raise RuntimeError(\"解析llm json结果失败\")\n    \n    async def _filter_single_table(self, semaphore, table_schema_info: dict) -> dict | None:\n        \n        async with semaphore:\n            llm_response = \"\"\n            request_id = \"\"\n            error_msg = None\n            for retry in range(3):\n                try:\n                    columns_prompt = self._generate_filter_prompt(table_schema_info, error_msg)\n                    \n                    messages = [\n                        {\n                            \"role\": \"system\",\n                            \"content\": \"you are a helpful assistant.\",\n                        },\n                        {\n                            \"role\": \"user\",","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/jd-opensource/joyagent-jdgenie/blob/2417e0b8b636d941ad5fb14c59b20dddfef5375d/genie-tool/genie_tool/tool/table_rag/table_column_filter.py#L184-L220","documentation":"_parse_json_result in table_column_filter.py raises RuntimeError('解析llm json结果失败') when re.findall for the ```json ...``` fenced block on the LLM response returns nothing. It signals the LLM reply was not the expected JSON code block.","triggerScenarios":"The LLM response string contains no ```json fenced block (pattern ```json\\s*([\\s\\S]*?)\\s*```), e.g. plain text answer, bare JSON without fences, or a different fence language; findall returns [] and result[0] raises IndexError caught and re-raised.","commonSituations":"Model returns raw JSON without markdown fences; temperature/high randomness makes model drift from the requested format; prompt for column filtering changes; model truncation drops closing fence.","solutions":["Log the full result_str (already logged as 生成结果格式不合法) and strengthen the prompt to demand exactly one ```json fenced block","Broaden the parser to also accept bare JSON: try json.loads(result_str) directly before regex","Use response_format/JSON mode if the LLM API supports it, or a json_mode flag in your client","Retry the LLM call (the surrounding retry loop in _filter_single_table already retries) with an explicit format reminder"],"exampleFix":"// before\nresult = re.findall(r'```json\\s*([\\s\\S]*?)\\s*```', result_str)\nreturn result[0]\n// after\nmatch = re.findall(r'```(?:json)?\\s*([\\s\\S]*?)\\s*```', result_str)\nif match:\n    return match[0]\ntry:\n    json.loads(result_str)\n    return result_str\nexcept Exception:\n    raise RuntimeError('解析llm json结果失败')","handlingStrategy":"retry","validationCode":"import re, json\ndef llm_json_block_ok(result_str: str):\n    m = re.findall(r'```json\\s*([\\s\\S]*?)\\s*```', result_str)\n    return bool(m) and _is_json(m[0])","typeGuard":"def is_fenced_json(s):\n    m = re.match(r'```json\\s*([\\s\\S]*?)\\s*```$', s.strip())\n    return m is not None","tryCatchPattern":"try:\n    block = parse_json_result(result_str)\nexcept RuntimeError:\n    logger.warning('no ```json block, retrying with stricter prompt')\n    result_str = llm_call(prompt + '\\nRespond ONLY with a ```json block.')","preventionTips":["Use JSON mode / response_format when the LLM API supports it","Include an explicit output-format example in every prompt","Log full raw responses on failure","Retry with format reminder before failing the table"],"tags":["llm","json","parsing"],"backgroundTag":"json-parse-error","analyzedSha":"2417e0b8b636d941ad5fb14c59b20dddfef5375d","analyzedAt":"2026-09-08T11:28:19.414Z","contentChangedAt":"2026-09-08T11:28:19.414Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}