Repository navigation
fix: handle multipart MIME attachments in partition_email - #4277
Closed
AlonNaor22 wants to merge 1 commit into
Closed
AlonNaor22 wants to merge 1 commit into
AlonNaor22 wants to merge 1 commit into
Conversation
When a PGP-signed email is wrapped in a multipart/mixed envelope, the multipart/signed part appears as an "attachment" via iter_attachments(). Calling get_content() on it raises KeyError because Python's email.contentmanager has no handler for multipart content types. Fix in three layers: - _file_bytes: check is_multipart() first and use as_bytes() to get raw MIME bytes instead of calling get_content() which cannot handle multipart types - _iter_elements: move file creation inside the try/except block so all attachment processing errors go through the same error handler - EXPECTED_ATTACHMENT_ERRORS: add KeyError as a safety net for any remaining unhandled content types from the content manager Closes #3922 Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
partition_emailcrashes withKeyError: 'multipart/mixed'when processing PGP-signed emails withprocess_attachments=Trueemail.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)_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 typesFiles 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) attachmentsruff checkandruff formatclean🤖 Generated with Claude Code