{"record":{"id":"3bc7df398cb00872","repo":"microsoft/autogen","slug":"the-unidiff-package-is-required-for-patch-applic","errorCode":null,"errorMessage":"The 'unidiff' package is required for patch application. Install with 'pip install unidiff'.","messagePattern":"The 'unidiff' package is required for patch application\\. Install with 'pip install unidiff'\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/memory/canvas/_text_canvas.py","lineNumber":151,"sourceCode":"            fromfile=f\"{filename}@r{from_revision}\",\n            tofile=f\"{filename}@r{to_revision}\",\n        )\n        return \"\".join(diff)\n\n    def apply_patch(self, filename: str, patch_data: Union[str, bytes, Any]) -> None:\n        \"\"\"Apply *patch_text* (unified diff) to the latest revision and save a new revision.\n\n        Uses the *unidiff* library to accurately apply hunks and validate context lines.\n        \"\"\"\n        if isinstance(patch_data, bytes):\n            patch_data = patch_data.decode(\"utf-8\")\n        if not isinstance(patch_data, str):\n            raise ValueError(f\"Expected str or bytes, got {type(patch_data)}\")\n        self._ensure_file(filename)\n        original_content = self.get_latest_content(filename)\n\n        if PatchSet is None:\n            raise ImportError(\n                \"The 'unidiff' package is required for patch application. Install with 'pip install unidiff'.\"\n            )\n\n        patch = PatchSet(patch_data)\n        # Our canvas stores exactly one file per patch operation so we\n        # use the first (and only) patched_file object.\n        if not patch:\n            raise ValueError(\"Empty patch text provided.\")\n        patched_file = patch[0]\n        working_lines = original_content.splitlines(keepends=True)\n        line_offset = 0\n        for hunk in patched_file:\n            # Calculate the slice boundaries in the *current* working copy.\n            start = hunk.source_start - 1 + line_offset\n            end = start + hunk.source_length\n            # Build the replacement block for this hunk.\n            replacement: List[str] = []\n            for line in hunk:","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/memory/canvas/_text_canvas.py#L133-L169","documentation":"apply_patch needs the third-party 'unidiff' library to parse and apply unified diffs. The import is optional (PatchSet is None if unidiff is absent) and this ImportError tells you to install it; the chained guidance is 'pip install unidiff' or the chromadb-style extra.","triggerScenarios":"Calling apply_patch on TextCanvas in an environment where the unidiff package is not installed (it is an optional dependency of the canvas extra).","commonSituations":"Fresh environments where only the base autogen-ext package was installed; CI images trimmed of optional deps; dependency lock files that dropped the transitive extra.","solutions":["Install unidiff: pip install unidiff (or install the autogen-ext extra that includes it).","Verify with python -c \"import unidiff\" after installing.","Add unidiff to your project's dependency list so CI environments include it."],"exampleFix":"# shell\n# before: apply_patch raises ImportError\npip install unidiff\n# after: apply_patch works","handlingStrategy":"validation","validationCode":"try:\n    import unidiff  # noqa: F401\n    unidiff_available = True\nexcept ImportError:\n    unidiff_available = False\nif not unidiff_available:\n    raise RuntimeError(\"unidiff required: pip install unidiff\") before running apply_patch flows","typeGuard":null,"tryCatchPattern":"try:\n    canvas.apply_patch(filename, patch_text)\nexcept ImportError as e:\n    if \"unidiff\" in str(e):\n        subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"unidiff\"])\n        canvas.apply_patch(filename, patch_text)\n    else:\n        raise","preventionTips":["Declare unidiff in your project dependencies since apply_patch needs it.","Smoke-test `import unidiff` in environment setup scripts for apps that patch canvas files."],"tags":["autogen","canvas","dependencies","import-error"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}