Unstructuredunstructured 0.27.6 · Python 3.11+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
21 small wins to finish your pathNext lesson

How partition picks a partitioner

partition is a dispatcher. It works out what the file is, then hands it to the function for that format. Both halves are worth seeing, because when a file comes back wrong it is almost always the first half.

The decision is its own function and you can call it.

Example
from unstructured.file_utils.filetype import detect_filetype

for name in ["shipping.md", "refunds.html", "orders.csv", "ravi.eml"]:
    print(name, "->", detect_filetype(name))

A FileType member for each, and each member knows the function that handles it. That is all the dispatch is.

Four questions, in order

The detector asks four things and takes the first answer it gets.

How a file's type is decided
Is it a known binary?. Word, PowerPoint and Excel files are zip archives with known contents, and old Office files are another known format. These can be identified exactly, so they are checked first.Step 1 of 4
The warning you have been seeing, libmagic is unavailable but assists in filetype detection, is the third question being skipped. python-magic is installed by pip; the C library it wraps is not, and pip cannot install it. On a text file it makes no difference here, because the fallback cannot name text formats either, and both fall through to the extension.

When the extension lies

Here is a web page that somebody saved with the wrong extension.

html
<h1>Refunds</h1><p>Refunds take five working days.</p>
Example
from unstructured.partition.auto import partition

for element in partition("policy.txt"):
    print(element.category, "|", element.text)

One element, and its text is the file with the tags still in it. Nothing raised, nothing warned. The fourth question answered txt and the text partitioner did exactly what it was asked to do with a line of HTML.

This is the failure worth recognising, because it looks like a bug in the HTML parser and it is not. The fix is the second question.

Example
from unstructured.partition.auto import partition

for element in partition("policy.txt", content_type="text/html"):
    print(element.category, "|", element.text)

Two elements, correctly labelled. content_type jumps the queue, and it is the one argument worth reaching for whenever a file comes back as a single suspicious blob.

Try it yourself
  • Rename orders.csv to orders.dat and see what detect_filetype returns.
  • Pass content_type="text/csv" for that file and check the elements.
  • Run detect_filetype on returns.docx after renaming it to returns.bin. Why does that one still work?

Every expert started right here.