Repository navigation
fix(parser): write an email's text output in a fixed field order - #21100
Conversation
The text output, which the default email pipeline uses, flattened the parsed fields by ranging over the content map. Go randomizes map order, so the same message came out differently on every parse: 15 distinct texts over 50 parses of one plain-text email, sometimes with the body before the headers. Chunking a different text each time changes the chunks of an unchanged message on every re-parse. The fields are now written headers first, in the order the JSON header item already uses (from, to, cc, bcc, date, subject), then text and text_html, and any other key after them in sorted order.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Summary
EmailParser.parseEmailbuilds the text output by ranging over the parsedcontentmap. The default email pipeline uses that output ("output_format": "text"in the email template). Go randomizes map iteration order, so the same message produced a different text on each parse. OnmainI parsed one plain-text.eml50 times and got 15 distinct texts. In some of them the body came before the headers:The chunker splits this text on
\ninto 512-token chunks. An unchanged message therefore gets different chunks every time it is re-parsed. In a long email the subject and sender can land after the body, in a different chunk from the opening of the message.What changed
The text output now writes the headers first, in the order
searchableEmailHeadersalready uses for the JSON header item (from, to, cc, bcc, date, subject). Then cometextandtext_html, and after them any other key in sorted order (emailTextKeys). Which fields appear, and how each one is written, does not change. Only the order is now fixed.Testing
TestEmailParser_EmlTextFieldOrderparses one message 20 times and requires the exact text every time. It fails onmain(one run printedfrom, cc, subject, date, to, …). The existing text-output tests, including the three-occurrence check inTestEmailParser_EmlTextChunkText, still pass.go veton the package reports only the existingcontext.WithDeadlinewarning atpaddleocr.go:453.