fix: handle multipart MIME attachments in partition_email
Closed
AL
9mo ago+136+136+012345678901234567890123456789-3-3-0123456789
Summary
- Fixes #3922 —
partition_emailcrashes withKeyError: 'multipart/mixed'when processing PGP-signed emails withprocess_attachments=True - Root cause: Python's
email.contentmanagerhas no handler formultipart/*content types, soget_content()raisesKeyErrorwhen called on a multipart MIME part that appears as an "attachment" (e.g.multipart/signedinside amultipart/mixedenvelope) - Fix applied in three layers:
_file_bytes: checkis_multipart()first and useas_bytes()for raw MIME bytes_iter_elements: move file creation inside the try/except block for uniform error handlingEXPECTED_ATTACHMENT_ERRORS: addKeyErroras safety net for unhandled content types
Files changed
unstructured/partition/email.py—_file_bytesmultipart handling +_iter_elementserror boundary fixunstructured/partition/common/__init__.py— addKeyErrortoEXPECTED_ATTACHMENT_ERRORStest_unstructured/partition/test_email.py— 2 new unit testsexample-docs/eml/mime-signed-multipart.eml— test fixture (simple PGP-signed email)example-docs/eml/mime-signed-with-attachment.eml— test fixture (reproduces exact bug scenario)
Test plan
-
_file_bytesreturns raw bytes for multipart attachments viaas_bytes() -
_file_bytesstill works normally for regular (non-multipart) attachments - All 37 existing unit tests pass unchanged
-
ruff checkandruff formatclean
🤖 Generated with Claude Code