{"record":{"id":"0dddb46f9e3c29dc","repo":"Aider-AI/aider","slug":"unexpected-error-parsing-patch-e","errorCode":null,"errorMessage":"Unexpected error parsing patch: {e}","messagePattern":"Unexpected error parsing patch: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aider/coders/patch_coder.py","lineNumber":288,"sourceCode":"                raise DiffError(f\"File referenced in patch not found: {rel_path}\")\n            except IOError as e:\n                raise DiffError(f\"Error reading file {rel_path}: {e}\")\n\n        try:\n            # Parse the patch text using adapted logic\n            patch_obj = self._parse_patch_text(lines, start_index, current_files)\n            # Convert Patch object actions dict to a list of tuples (path, action)\n            # for compatibility with the base Coder's prepare_to_edit method.\n            results = []\n            for path, action in patch_obj.actions.items():\n                results.append((path, action))\n            return results\n        except DiffError as e:\n            # Raise as ValueError for consistency with other coders' error handling\n            raise ValueError(f\"Error parsing patch content: {e}\")\n        except Exception as e:\n            # Catch unexpected errors during parsing\n            raise ValueError(f\"Unexpected error parsing patch: {e}\")\n\n    def _parse_patch_text(\n        self, lines: List[str], start_index: int, current_files: Dict[str, str]\n    ) -> Patch:\n        \"\"\"\n        Parses patch content lines into a Patch object.\n        Adapted from the Parser class in apply_patch.py.\n        \"\"\"\n        patch = Patch()\n        index = start_index\n        fuzz_accumulator = 0\n\n        while index < len(lines):\n            line = lines[index]\n            norm_line = _norm(line)\n\n            if norm_line == \"*** End Patch\":\n                index += 1","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/Aider-AI/aider/blob/5dc9490bb35f9729ef2c95d00a19ccd30c26339c/aider/coders/patch_coder.py#L270-L306","documentation":"The defensive catch-all in PatchCoder.get_edits: any non-DiffError exception thrown while parsing (i.e. an unexpected bug in _parse_patch_text rather than a grammar violation) is wrapped as ValueError('Unexpected error parsing patch: ...'). It distinguishes 'model produced a bad patch' from 'the parser itself crashed'.","triggerScenarios":"An exception other than DiffError escapes _parse_patch_text — index errors from malformed nesting, None dereferences on unexpected shapes, type errors from odd content — i.e. parser bugs or unhandled edge cases in the patch grammar.","commonSituations":"New model output shapes that hit untested parser paths after a model upgrade; aider version regressions in patch parsing; exotic patches mixing markers in unusual orders.","solutions":["Capture the original exception text after 'Unexpected error parsing patch:' and the triggering patch text.","Search/upgrade the aider issue tracker — parser crashes on valid-ish input are bugs worth reporting with the minimal patch reproducing it.","As a workaround, switch to another edit format (diff/udiff) until the parser handles the case.","Pin/roll back the aider version if the crash appeared right after an upgrade."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    edits = coder.get_edits(reply)\nexcept ValueError as e:\n    if str(e).startswith(\"Unexpected error parsing patch:\"):\n        save_bug_report(reply, str(e))      # parser crash, not a model grammar error\n        edits = coder_with_diff_format.get_edits(reply)  # fallback edit format\n    else:\n        raise","preventionTips":["Distinguish 'Unexpected error parsing patch:' (parser bug) from 'Error parsing patch content:' (model grammar) before choosing a fix.","Minimize and report reproducing patch text to the aider tracker.","Keep a fallback edit format configured when running unattended."],"tags":["aider","patch-format","parsing","defect"],"backgroundTag":null,"analyzedSha":"5dc9490bb35f9729ef2c95d00a19ccd30c26339c","analyzedAt":"2026-08-15T05:40:10.498Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}