{"record":{"id":"8ad5486c7a6b2f50","repo":"run-llama/llama_index","slug":"invalid-answer-line-answer-line-answer-line-mu","errorCode":null,"errorMessage":"Invalid answer line: {answer_line}. Answer line must be of the form: answer_num: <int>, answer_relevance: <float>","messagePattern":"Invalid answer line: (.+?)\\. Answer line must be of the form: answer_num: <int>, answer_relevance: <float>","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"llama-index-core/llama_index/core/indices/utils.py","lineNumber":133,"sourceCode":"        )\n\n    return content_messages\n\n\ndef default_parse_choice_select_answer_fn(\n    answer: str, num_choices: int, raise_error: bool = False\n) -> Tuple[List[int], List[float]]:\n    \"\"\"Default parse choice select answer function.\"\"\"\n    answer_lines = answer.split(\"\\n\")\n    answer_nums = []\n    answer_relevances = []\n    for answer_line in answer_lines:\n        line_tokens = answer_line.split(\",\")\n        if len(line_tokens) != 2:\n            if not raise_error:\n                continue\n            else:\n                raise ValueError(\n                    f\"Invalid answer line: {answer_line}. \"\n                    \"Answer line must be of the form: \"\n                    \"answer_num: <int>, answer_relevance: <float>\"\n                )\n        try:\n            answer_num = int(line_tokens[0].split(\":\")[1].strip())\n        except (IndexError, ValueError) as e:\n            if not raise_error:\n                continue\n            else:\n                raise ValueError(\n                    f\"Invalid answer line: {answer_line}. \"\n                    \"Answer line must be of the form: \"\n                    \"answer_num: <int>, answer_relevance: <float>\"\n                )\n        if answer_num > num_choices:\n            continue\n        answer_nums.append(answer_num)","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/indices/utils.py#L115-L151","documentation":"default_parse_choice_select_answer (llama_index.core.indices.utils) raises ValueError('Invalid answer line: ...') when raise_error=True and a line of the LLM's answer does not contain exactly one comma — i.e. it is not of the form 'answer_num: <int>, answer_relevance: <float>'. With the default raise_error=False the line is silently skipped. The function parses the choice-select output used by rerankers/RouterQueryEngine, where each selected chunk must be listed with its relevance score.","triggerScenarios":"Calling parse_choice_select_answer(llm_output, n, raise_error=True) where the LLM emitted a preamble line ('Here are the relevant chunks:'), a single-field line ('answer_num: 3'), or extra commas; wrap around ChoiceSelectPrompt output from a weak model.","commonSituations":"LLMs that add chatty framing despite the output format instruction; few-shot prompt customization that changed the format; small local models ignoring the template; switching raise_error=True on to 'catch format bugs' and hitting real ones.","solutions":["Keep raise_error=False (the default) so malformed lines are skipped instead of raising.","Improve format adherence: keep the stock choice-select prompt, lower temperature, or use a stronger/structured-output LLM.","If you control the calling code, strip preamble/trailing lines and blank lines before parsing."],"exampleFix":"# before\nnums, rels = default_parse_choice_select_answer(raw_llm_output, n, raise_error=True)  # raises\n\n# after\nnums, rels = default_parse_choice_select_answer(raw_llm_output, n, raise_error=False)\nif not nums:\n    # LLM output unusable -> fall back to unranked order\n    nums, rels = list(range(n)), [1.0] * n","handlingStrategy":"fallback","validationCode":"import re\n\nCHOICE_LINE_RE = re.compile(r\"^\\s*answer_num:\\s*\\d+\\s*,\\s*answer_relevance:\\s*[\\d.]+\\s*$\")\n\ndef is_well_formed_choice_answer(answer: str) -> bool:\n    return all(CHOICE_LINE_RE.match(l) for l in answer.splitlines() if l.strip())","typeGuard":null,"tryCatchPattern":"try:\n    nums, rels = default_parse_choice_select_answer(answer, n, raise_error=True)\nexcept ValueError:\n    nums, rels = default_parse_choice_select_answer(answer, n, raise_error=False)\nif not nums:  # fully unparseable -> keep original ordering\n    nums, rels = list(range(n)), [1.0] * n","preventionTips":["Leave raise_error=False in production; it is designed to skip malformed lines.","Use the stock choice-select prompt and low temperature to keep LLM output on-format.","Strip blank/preamble lines from the raw completion before parsing."],"tags":["llm-output-parsing","choice-select","llama-index"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}