{"record":{"id":"2cf21b631f238d43","repo":"microsoft/autogen","slug":"empty-patch-text-provided","errorCode":null,"errorMessage":"Empty patch text provided.","messagePattern":"Empty patch text provided\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/memory/canvas/_text_canvas.py","lineNumber":159,"sourceCode":"        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:\n                if line.is_added or line.is_context:\n                    replacement.append(line.value)\n                # removed lines (line.is_removed) are *not* added.\n            # Replace the slice with the hunk‑result.\n            working_lines[start:end] = replacement\n            line_offset += len(replacement) - (end - start)\n        new_content = \"\".join(working_lines)\n","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/memory/canvas/_text_canvas.py#L141-L177","documentation":"After parsing the patch text with unidiff, an empty PatchSet means the text contained no file diffs (blank string, whitespace, or a diff with no headers/hunks). apply_patch raises rather than creating a no-op revision.","triggerScenarios":"Calling apply_patch(filename, \"\"), with only whitespace, or with text that is not valid unified-diff format (so unidiff parses zero files).","commonSituations":"LLM emitting an empty patch in a tool call; extracting the wrong field from a model response; copy/paste dropping the '---/+++' headers so unidiff recognizes nothing.","solutions":["Skip the call when the patch text is blank: if not patch_data.strip(): return.","If a diff was expected, verify the text contains '--- a/...' and '+++ b/...' headers and at least one '@@' hunk.","Fix the prompt/tool schema so the model returns well-formed unified diffs."],"exampleFix":"# before\ncanvas.apply_patch(\"f.txt\", patch_text)\n# after\nif patch_text and patch_text.strip():\n    canvas.apply_patch(\"f.txt\", patch_text)","handlingStrategy":"validation","validationCode":"if not patch_text or not patch_text.strip():\n    # nothing to apply — skip instead of erroring\n    pass\nelse:\n    assert \"@@\" in patch_text, \"patch text has no hunks; not a valid unified diff\"\n    canvas.apply_patch(filename, patch_text)","typeGuard":"def is_nonempty_unified_diff(text: str) -> bool:\n    return isinstance(text, str) and bool(text.strip()) and \"@@\" in text","tryCatchPattern":"try:\n    canvas.apply_patch(filename, patch_text)\nexcept ValueError as e:\n    if \"Empty patch text\" in str(e):\n        pass  # no-op patch from the model; skip\n    else:\n        raise","preventionTips":["Skip blank patches before calling apply_patch.","Validate that model-generated diffs contain '---'/'+++' headers and '@@' hunks."],"tags":["autogen","canvas","patching","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}