[magika-lib] Support PredictionMode (HighConfidence, MediumConfidence, BestGuess)
Author: reyammerCreated Sep 18, 2026Updated Sep 18, 2026
Context
In the Python pyo3 bindings (rust/pyo3 + python/), we are replacing the legacy Python/ONNX Runtime inference and post-processing pipeline with magika-lib (rust/lib).
Currently, magika-lib already computes (ContentType, OverwriteReason) in FileType::convert, but only supports HIGH_CONFIDENCE prediction mode (rust/lib/src/lib.rs:147: // we only support high-confidence).
Current State in rust/lib
- Only
HighConfidenceis implemented: Inrust/lib/src/file.rs(FileType::convert), the score is always compared against the per-label high-confidenceconfig.threshold. medium_confidence_thresholdis not stored inrust/lib/src/model.rs:model_config.min.jsondefines"medium_confidence_threshold": 0.5alongside per-label"thresholds", andrust/gen/src/main.rsgeneratesrust/lib/src/model.rs. CurrentlyLabelConfigonly stores the per-label high-confidencethreshold: f32.
Expected Behavior (Parity with Python Magika)
Support PredictionMode (HighConfidence, MediumConfidence, BestGuess) when resolving (output_label, overwrite_reason) from (dl_label, score):
- Apply
overwrite_map:- Look up mapped
output_label(config.content_typeinCONFIG.labels). - If
output_label != dl_label, initialoverwrite_reasonisOverwriteReason::OverwriteMap.
- Look up mapped
- Apply confidence threshold based on
PredictionMode:PredictionMode::BestGuess: always keepoutput_labelandoverwrite_reasonfrom Step 1 (no minimum score check).PredictionMode::MediumConfidence: keepoutput_labelandoverwrite_reasonifscore >= medium_confidence_threshold(0.5).PredictionMode::HighConfidence: keepoutput_labelandoverwrite_reasonifscore >= config.threshold(the per-label threshold fordl_label).
- Fallback when score is below the required threshold:
- Replace
output_labelwithContentType::Txtifoutput_label.info().is_textistrue, elseContentType::Unknown. - Set
overwrite_reason = OverwriteReason::LowConfidence, except whendl_label == output_label(e.g. when rawdl_labelwas alreadytxtorunknown), in which caseoverwrite_reasonisNone.
- Replace
Proposed Rust API Changes
- Add
PredictionMode(HighConfidence,MediumConfidence,BestGuess) enum tomagika(rust/lib). - Include
medium_confidence_threshold: f32inrust/lib/src/model.rs(viarust/gen). - Allow configuring or passing
PredictionModeonSession/Builder(orFileType::convert) sorust/pyo3can delegatePredictionModethresholding tomagika-liband remove the Python-side thresholding workaround.
Source: google/magika