microsoft/autogen · error · ImportError
The 'unidiff' package is required for patch application. Ins
Error message
The 'unidiff' package is required for patch application. Install with 'pip install unidiff'.
What it means
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.
Source
Thrown at python/packages/autogen-ext/src/autogen_ext/memory/canvas/_text_canvas.py:151
fromfile=f"{filename}@r{from_revision}",
tofile=f"{filename}@r{to_revision}",
)
return "".join(diff)
def apply_patch(self, filename: str, patch_data: Union[str, bytes, Any]) -> None:
"""Apply *patch_text* (unified diff) to the latest revision and save a new revision.
Uses the *unidiff* library to accurately apply hunks and validate context lines.
"""
if isinstance(patch_data, bytes):
patch_data = patch_data.decode("utf-8")
if not isinstance(patch_data, str):
raise ValueError(f"Expected str or bytes, got {type(patch_data)}")
self._ensure_file(filename)
original_content = self.get_latest_content(filename)
if PatchSet is None:
raise ImportError(
"The 'unidiff' package is required for patch application. Install with 'pip install unidiff'."
)
patch = PatchSet(patch_data)
# Our canvas stores exactly one file per patch operation so we
# use the first (and only) patched_file object.
if not patch:
raise ValueError("Empty patch text provided.")
patched_file = patch[0]
working_lines = original_content.splitlines(keepends=True)
line_offset = 0
for hunk in patched_file:
# Calculate the slice boundaries in the *current* working copy.
start = hunk.source_start - 1 + line_offset
end = start + hunk.source_length
# Build the replacement block for this hunk.
replacement: List[str] = []
for line in hunk:View on GitHub (pinned to 027ecf0a37)
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.
Example fix
# shell # before: apply_patch raises ImportError pip install unidiff # after: apply_patch works
Defensive patterns
Strategy: validation
Validate before calling
try:
import unidiff # noqa: F401
unidiff_available = True
except ImportError:
unidiff_available = False
if not unidiff_available:
raise RuntimeError("unidiff required: pip install unidiff") before running apply_patch flows Try / catch
try:
canvas.apply_patch(filename, patch_text)
except ImportError as e:
if "unidiff" in str(e):
subprocess.check_call([sys.executable, "-m", "pip", "install", "unidiff"])
canvas.apply_patch(filename, patch_text)
else:
raise Prevention
- 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.
When it happens
Trigger: Calling apply_patch on TextCanvas in an environment where the unidiff package is not installed (it is an optional dependency of the canvas extra).
Common situations: 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.
Related errors
- To use the ChromaDBVectorMemory the chromadb extra must be i
- ChromaDB embedding functions not available. Ensure chromadb
- Dependencies for MagenticOneCoderAgent not found. Please ins
- Missing dependecies for DockerCommandLineCodeExecutor. Pleas
- File '{filename}' does not exist on the canvas; create it fi
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/3bc7df398cb00872.
Report an issue: GitHub.