Doclingdocling 2.127.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
26 small wins to finish your pathNext lesson

Converting bytes that were never a file

Everything so far has been a path. A file arriving over HTTP is not a path, and writing it to disk just to convert it is a step nobody wants.

DocumentStream wraps bytes with a name. The name is not decoration: it is what Docling reads to decide which backend to use.

Example
from io import BytesIO
from docling.datamodel.base_models import DocumentStream
from docling.document_converter import DocumentConverter

raw = b"# Notice\n\nDelivery is paused on public holidays.\n"
upload = DocumentStream(name="notice.md", stream=BytesIO(raw))
print(DocumentConverter().convert(upload).document.export_to_markdown())

No file was written. The same bytes with the name notice.html would go to the HTML backend and come out as one paragraph starting with a hash, because nothing about the bytes says Markdown.

Proving that

Example
upload = DocumentStream(name="notice.html", stream=BytesIO(raw))
document = DocumentConverter().convert(upload).document
print(document.texts[0].label, "|", document.texts[0].text)

One text item holding the hash as a literal character, where the Markdown reading gave a title. Same bytes, different name, different document. In a web service that name comes from whatever the browser uploaded, so it is worth checking rather than trusting.

The result still knows the name

Example
result = DocumentConverter().convert(
    DocumentStream(name="notice.md", stream=BytesIO(raw)))
print(result.input.file.name)
print(result.document.origin.filename)

So the citation machinery from lesson 14 works on an upload exactly as it does on a file. The handbook in lesson 29 never needs this, but a service built on the same code would use nothing else.

A URL is the third source. Passing convert a string starting with http downloads the file and converts it, which is a path, a stream and a URL through one method.
Try it yourself
  • Convert the same bytes named notice.txt and compare the labels.
  • Read shipping.md into memory and convert it as a stream named anything.md.
  • Pass a stream with no extension in its name and read the error.

Every expert started right here.