multimodal-preprocess-kit
A framework-agnostic policy kit for chatbot multimodal preprocessing: caption retry, pending-message deferral, caption-mode selection, caption quality checks, and safe fallback.
At a glance (defaults):
| Situation | Decision |
|---|---|
Caption failed |
drop_media |
Caption too_short, or ready and under 40 chars |
retry_caption |
Caption pending, waited ≤ 90s |
defer with retry_after |
Caption pending, waited > 90s |
push_without_media |
Caption ready and non-empty |
push_now; screenshots carry "auto" |
| No local file, thumbnails enabled | thumbnail fallback |
| Media from another message | never borrowed unless explicitly enabled |
Problem
Vision-language captions are often slower than the chat message buffer. A bot may first answer that it cannot see an image, then describe it a second later. Screenshots lose their text when they are summarized instead of transcribed. And when the original media is unavailable, a pipeline may borrow a nearby image, silently attaching the wrong picture to a message.
What it does NOT do
- Calls no model (VL, LLM, or otherwise).
- Connects to no chat platform and performs no network I/O.
- Stores no chat history.
- Reads, decodes, or uploads no media files.
Install
pip install -e .
Quick Start
from mm_preprocess_kit.models import MediaItem, PolicyConfig
from mm_preprocess_kit.policy import decide
media = MediaItem(id="m1", kind="image", caption_status="pending", received_at=0.0)
decision = decide(media, now=30.0, cfg=PolicyConfig())
print(decision.action, decision.reason, decision.retry_after) # defer caption_pending 60.0
Screenshots get caption_mode="auto". Resolve it with choose_caption_mode(width, height, cfg) once your adapter knows the real pixel size.
Caption Quality
Length is a weak signal: a caption made entirely of placeholders can pass 40
characters, and a model can copy the instruction back instead of describing the
image. caption_quality adds three cheap heuristics:
from mm_preprocess_kit import assess_caption, is_usable
assess_caption("REM\n" + "\u25a1\n" * 32).issue # "placeholder"
assess_caption("transcribe every line... do not summarize").issue # "prompt_echo"
is_usable("21:30") # True
| Rule | Default | Rejects when |
|---|---|---|
min_meaningful_chars |
3 | fewer alphanumeric characters |
placeholder_min_count / placeholder_ratio |
4 / 2.0 | placeholders outnumber meaningful characters by more than 2x |
echo_min_hits |
2 | two or more instruction phrases are copied back |
assess_caption returns the verdict plus the counts, so a rejected caption can
be logged instead of silently dropped. Override CaptionQualityConfig to change
the thresholds or supply the echo phrases of your own prompt.
Policy Config Table
| Field | Default | Meaning |
|---|---|---|
min_caption_chars |
40 | Captions shorter than this (after trimming) are treated as missing. |
max_defer_seconds |
90 | Upper bound on how long a pending message may be deferred. |
long_image_min_edge |
700 | Long-edge threshold that selects transcribe. |
long_image_max_edge |
1400 | Reserved for caller-side resizing. Not enforced in v0.1. |
long_image_max_tokens |
900 | Reserved for caller-side budgeting. Not enforced in v0.1. |
allow_thumbnail_fallback |
True | Permit a thumbnail when no local file exists. |
allow_borrow_other_media |
False | Permit borrowing another message's media. Off by default. |
long_image_min_edge is a long-edge rule, not an area rule: an 800x600 image has a long edge of 800 and therefore selects transcribe at the default threshold.
Design Decisions
No borrowing by default. A missing image is recoverable; a wrong image is not. Attaching another message's media can silently send someone else's picture, so it requires an explicit opt-in (allow_borrow_other_media=True).
Prefer text over blocking. When the deferral window expires, the message is pushed without media. Waiting longer delays every later message in the queue and produces inconsistent "no image" answers. The caption can still arrive later as a follow-up.
Screenshots stay auto until the caller knows the size. decide never learns an item's pixel size and never opens the file, so it returns caption_mode="auto". The adapter resolves it with choose_caption_mode(width, height, cfg).
Where this kit deliberately stops. A production adapter usually does more than these four functions. The gaps below are intentional, and each one changes how you should wire the kit:
- Failure handling. A real pipeline often pushes a placeholder ("media could not be described") instead of discarding the item. If yours does, map
caption_status="failed"topush_without_media; reservedrop_mediafor pipelines that intentionally drop the item. - Garbage captions.
caption_qualitycovers the common shapes: placeholder density, prompt echo, and captions too short to be meaningful. A caption can still be wrong in ways these heuristics do not see; treatis_usableas a cheap filter, not a guarantee. - Text-heavy gating.
should_retry_captionapplies to every caption. Production may retry only screenshots or long images; call it only for those kinds instead of unconditionally. - Thumbnail semantics.
can_use_thumbnailtreatslocal_path is Noneas "no original is available". If the file exists but cannot be decoded, passNonerather than a path that will fail later.
This kit gives you the decision vocabulary; the adapter still owns these policy details.
Test
pytest -q
Compliance
This library contains only generic preprocessing policies. It does not include any platform automation implementation.
License
MIT
Metadata
Release files for multimodal-preprocess-kit 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| multimodal_preprocess_kit-0.2.0.tar.gz | 14.3 kB | Without provenance | Details for multimodal_preprocess_kit-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| multimodal_preprocess_kit-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for multimodal_preprocess_kit-0.2.0-py3-none-any.whl |
Total release size: 25.2 kB
Release files / multimodal_preprocess_kit-0.2.0.tar.gz
| Download URL | multimodal_preprocess_kit-0.2.0.tar.gz |
|---|---|
| Size | 14.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17aaf5f4cc7e99d9d140eb0fe9dd23f617df580f3177b2cd933d4648658c677c
|
|
BLAKE2b-256 checksum How to use checksums |
bddcbf21b3205bdcf1837471401132f3b2bc60a7d81878d9f2ed99b7ed03518e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / multimodal_preprocess_kit-0.2.0-py3-none-any.whl
| Download URL | multimodal_preprocess_kit-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c5b6fdf09a8d6951de9d6ca74afbe24283fffc248dcc4f3a8a1cbd3fce4bf39
|
|
BLAKE2b-256 checksum How to use checksums |
17085607a3a9bf07532d304a0ef927f42604e1b46f1fe2101b308d00090650b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for multimodal-preprocess-kit 0.2.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.