Skip to content

perf(converter): lazy-import markitdown to cut startup ~24% and enable slim packaging#186

Open
KylinMountain wants to merge 2 commits into
mainfrom
claude/project-desktop-compilation-vfd4q7
Open

perf(converter): lazy-import markitdown to cut startup ~24% and enable slim packaging#186
KylinMountain wants to merge 2 commits into
mainfrom
claude/project-desktop-compilation-vfd4q7

Conversation

@KylinMountain

Copy link
Copy Markdown
Collaborator

Summary

markitdown was imported at the top of openkb/converter.py, so every
import openkb eagerly pulled in markitdown → magika → onnxruntime (tens of
MB, slow to import) — even for commands that never touch an Office/HTML
document. But markitdown is only used in one branch of convert_document: the
non-PDF, non-Markdown path. This PR moves the import into that branch.

Changes

  • openkb/converter.py — import MarkItDown lazily inside the Office-format
    branch instead of at module top level.
  • tests/test_converter.py — update the mock patch target to
    markitdown.MarkItDown (the attribute the local import binds), and add a
    regression test that spawns a fresh interpreter and asserts
    import openkb.converter loads none of markitdown / magika / onnxruntime.

Why it helps (measured)

  • import openkb.cli cold start 4.64s → 3.53s (~24% faster); magika /
    onnxruntime / markitdown no longer load unless an Office-format document is
    actually converted.
  • A packaged (PyInstaller) build can drop magika/onnxruntime (~25 MB
    uncompressed) for a slimmer artifact when Office ingest isn't required.
    Previously the unconditional module-level import made magika load-bearing at
    startup, so excluding it crashed the app on launch.

Notes

  • markitdown appends warning filters when it imports, but leaves the CLI's
    front "ignore" filter (set in cli.py) at index 0, so no re-suppression is
    needed at the new import site — verified empirically and documented in a code
    comment.
  • The new guard test runs in a subprocess so it is deterministic regardless of
    what earlier tests already imported into sys.modules. Without it, a future
    change that reintroduces a top-level markitdown import would regress startup
    silently while every functional test stays green.
  • Behavior change: a missing/broken markitdown now surfaces when converting an
    Office/HTML file rather than at startup. In a normal pinned install markitdown
    is always present, so this only matters for intentionally-slimmed builds.

Verification

  • pytest tests/test_converter.py — 29 passed (28 existing + 1 new guard).
  • ruff check / ruff format --check / mypy openkb/converter.py clean.

https://claude.ai/code/session_01KZyUSGAzVL9yxpsWWPv6Y2


Generated by Claude Code

claude added 2 commits July 17, 2026 16:22
…m packaging

markitdown is only used for the non-PDF, non-Markdown ingest branch, but it
was imported at module top level — so every `import openkb` eagerly pulled in
markitdown → magika → onnxruntime (tens of MB, slow). Move the import into the
branch that actually uses it.

Effects (measured):
- `import openkb.cli` cold start 4.64s → 3.53s (~24% faster); magika /
  onnxruntime / markitdown no longer load unless an Office-format document is
  converted.
- A packaged (PyInstaller) build can now exclude magika/onnxruntime for a
  slimmer artifact when Office-format ingest isn't needed. Previously the
  unconditional module-level markitdown import made magika load-bearing at
  startup, so excluding it crashed the whole app on launch.

markitdown appends warning filters on import but leaves the CLI's front
"ignore" filter at index 0, so no re-suppression is needed at the new import
site. test_converter patches `markitdown.MarkItDown` (the real attribute the
local import binds) instead of the removed `openkb.converter.MarkItDown`.

Claude-Session: https://claude.ai/code/session_01KZyUSGAzVL9yxpsWWPv6Y2
Adds a regression test that spawns a fresh interpreter and asserts
`import openkb.converter` loads none of markitdown / magika / onnxruntime.
This locks in the lazy-import optimization: if a future change reintroduces a
module-level markitdown import, startup cost and the slim-packaging benefit
regress silently while every functional test still passes. The check runs in a
subprocess so it is deterministic regardless of what earlier tests imported
into this process's sys.modules.

Claude-Session: https://claude.ai/code/session_01KZyUSGAzVL9yxpsWWPv6Y2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants