Skip to main content

mdream (Python)

Python bindings for mdream — a PyO3 wrapper around the Rust mdream core engine. Converts HTML to GitHub Flavored Markdown with frontmatter, lists, tables, and code blocks, through one-shot and streaming APIs. The GIL is released for the whole conversion, so Python code keeps running while large documents convert.

  • Requires CPython 3.10+ (a single abi3 wheel per platform covers every supported CPython)
  • No runtime Python dependencies
  • MIT licensed, matching the workspace

Python bindings for mdream by harlan-zw (MIT). This is an independent packaging of the upstream Rust core as mdream-py.

Install

From PyPI, once the first wheels are published (distribution name mdream-py, import name mdream):

pip install mdream-py

From source with maturin:

pip install maturin
maturin develop        # build and install into the active virtualenv
# or build a wheel:
maturin build --release

Quickstart

import mdream

md = mdream.html_to_markdown("<h1>Hello</h1><p>World</p>")
# "# Hello\n\nWorld"

# Rich result: markdown plus frontmatter, extracted elements, and the
# truncation flag. Pass options as a plain dict (snake_case keys).
result = mdream.convert(
    html,
    {
        "origin": "https://example.com",
        "plugins": {"frontmatter": {}},
    },
)
result.markdown      # str — the Markdown output
result.frontmatter   # dict[str, str] | None — extracted <head> metadata
result.extracted     # list[ExtractedElement] | None — extraction plugin matches
result.truncated     # bool — True if max_node_bytes dropped content

# Plain text output
text = mdream.html_to_text(html)

mdream.__version__ reports the package version (kept in lockstep with the workspace crate version).

Streaming

MarkdownStream converts HTML incrementally: feed chunks as they arrive and collect the Markdown as it is produced. Joining the chunk outputs always equals the one-shot conversion of the same input, even when a chunk boundary splits HTML mid-tag.

from mdream import MarkdownStream

stream = MarkdownStream()

for chunk in chunks:                # e.g. a network response body
    md_part = stream.process_chunk(chunk)
    write(md_part)                  # Markdown as it becomes ready

tail = stream.finish()              # flush remaining output
write(tail)

stream.truncated                    # bool — True if max_node_bytes fired
data = stream.take_data()           # {"extracted": [...], "frontmatter": {...}}

take_data() returns the elements collected by the extraction plugin and the frontmatter extracted so far; extracted drains on call and is complete after finish(). The GIL is released around each chunk's conversion work.

Options

Options are plain dicts with snake_case keys (a deliberate divergence from the JS engines' camelCase). Every key is validated: an unknown key at any level raises ValueError naming the key, a bad format or negative wrap_width raises ValueError, and wrong value types raise TypeError.

Top-level keys (all optional):

Key Type Default Description
origin str — Base URL used to resolve relative links and image sources.
clean dict — Post-processing cleanup rules (see below).
plugins dict — Plugin configuration (see below).
images bool True Pass False to omit images and drop links left with no text, such as image-only links. Markdown and text output.
wrap_width int 0 Hard-wrap prose at this many characters, breaking on word boundaries. 0 disables wrapping. Code, tables, and headings are never wrapped.
format str "markdown" Output format: "markdown", "text", or "html".
max_node_bytes int 0 Safety cap in bytes on what one construct may buffer — a text node, a tag, an open code block or inline code span, a table row's columns, or extraction script text. 0 (the default) is unlimited; content past the cap is dropped, bounding memory on adversarial input. Useful when streaming untrusted email. truncated in the result (or MarkdownStream.truncated) reports whether it fired.

clean

All flags are bool, false by default:

Key Description
urls Strip tracking query parameters from URLs (utm_*, fbclid, gclid, …).
fragments Strip fragment-only links that don't match any heading slug.
empty_links Strip links with meaningless or executable hrefs to plain text.
redundant_links Strip links where the text equals the URL: [https://x.com](https://x.com) → https://x.com.
self_link_headings Strip self-referencing heading anchors: ## [Title](#title) → ## Title.
empty_images Strip images with no alt text: ![](url) → nothing.
empty_link_text Drop links that produce no visible text: [](url) → nothing.

plugins

Key Type Description
filter dict Filter content by CSS selector: include (list of selectors to keep, dropping everything else), exclude (list of selectors to remove), process_children (bool — when true, children of an excluded element are still processed).
isolate_main bool Restrict output to the page's <main> element. Pass True to enable.
frontmatter dict Extract <head> metadata into YAML frontmatter: additional_fields (dict of {name: value} string pairs appended to the frontmatter; reserved keys such as title are ignored), meta_fields (list of extra <meta name="..."> keys to extract beyond the defaults).
tailwind bool Convert Tailwind utility classes to semantic Markdown. Pass True to enable.
extraction dict selectors (list of CSS selectors); matched elements are collected into the result's extracted / take_data()["extracted"].
tag_overrides dict Per-tag rendering overrides keyed by lowercase tag name (see below).

tag_overrides

Each value is a dict with any of:

Key Type Description
enter str String emitted when entering the tag, replacing its default opening Markdown.
exit str String emitted when exiting the tag, replacing its default closing Markdown.
spacing [int, int] Blank lines [before, after] around the tag's output. [0, 0] keeps it inline.
is_inline bool Treat the tag as inline (true) or block (false).
is_self_closing bool Treat the tag as void/self-closing (like <br>).
collapses_inner_white_space bool Collapse runs of whitespace inside the tag to a single space.
alias str Name of a built-in tag to render exactly like (e.g. "em"). Unknown tag names raise ValueError.
mdream.html_to_markdown(
    html,
    {"plugins": {"tag_overrides": {"sup": {"enter": "^", "exit": "^", "is_inline": True}}}},
)

Platform support

Prebuilt abi3 wheels (CPython 3.10+) are published for:

  • macOS (arm64)
  • Linux (x86_64)

Everything else builds from the sdist with a Rust toolchain and maturin.

Known limitations

  • Free-threaded CPython: abi3 wheels are incompatible with free-threaded CPython 3.13t/3.14t. If you need free-threaded Python, a separate cp3xxt wheel would be required; build from the sdist in the meantime.
  • Panic safety: Rust panics are caught and re-raised as Python RuntimeError (mdream internal error: ...) rather than crashing the interpreter. That path is implemented but not deterministically triggerable from pure Python, so it is an accepted, documented gap — no test asserts it end-to-end.
  • No html2text compatibility layer: the package ships a clean native API (one-shot + streaming). html2text remains a benchmark baseline only.

API surface

Symbol Kind Description
mdream.html_to_markdown(html, options=None) function One-shot conversion → Markdown str.
mdream.convert(html, options=None) function One-shot conversion → MdreamResult.
mdream.html_to_text(html, options=None) function One-shot conversion → plain-text str.
mdream.MarkdownStream(options=None) class Streaming converter: process_chunk(chunk), finish(), truncated, take_data().
mdream.MdreamResult type markdown, extracted, frontmatter, truncated.
mdream.ExtractedElement type selector, tag_name, text_content, attributes.
mdream.__version__ str Package version, in lockstep with the workspace crate.

The native extension lives in mdream._mdream; the mdream package re-exports the public API above, ships inline .pyi stubs, and is marked typed (py.typed).

Metadata

Release files for mdream-py 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mdream-py 0.1.1
File Size Uploaded Provenance Actions
mdream_py-0.1.1.tar.gz 275.1 kB With provenance Details for mdream_py-0.1.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mdream-py 0.1.1
File Interpreter ABI Platform Actions
mdream_py-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.28+ x86-64 Details for mdream_py-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl
mdream_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for mdream_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl

Total release size: 1.2 MB

Release files / mdream_py-0.1.1.tar.gz

Download URL mdream_py-0.1.1.tar.gz
Size 275.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f4a03fab27fa444c5098d9334a7c843c86c674235746f07373a367deb29bf110
BLAKE2b-256 checksum
How to use checksums
4e440905624d668b4f626d8e4861259e3093d878713c4e1fa253d7c77e2bb720
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / mdream_py-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL mdream_py-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl
Size 466.8 kB
Tags CPython 3.10 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
74d58320d07739e7bb4d0ff92e6f37e1f249d69c8626ac59c0269dde0c4cd623
BLAKE2b-256 checksum
How to use checksums
828363e795c5f65f95738bb459c5e00e5f4b45eebf3024548a185fead6d91dfb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / mdream_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl

Download URL mdream_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Size 435.2 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d31054b8b6fbbe0804f7f12b43a836289de20183c52844a698e0b26b171d4b52
BLAKE2b-256 checksum
How to use checksums
44b6d3eb15cd56f5bd517b0c3e148d51f62c9df9b68b2726221f178c4d0926bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for mdream-py 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for mdream_py-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page