mistral-ocr
Verbatim OCR of PDFs and images using Mistral's vision model - ImageMagick conversion at 300 DPI, per-page transcription, QA flags, OCR_Log row. Use when the user requests Mistral-based OCR for verbatim transcription (legal documents, contracts) or as secondary validation after the okf-ocr pipeline. Pairs with verbatim-ocr (QA) and okf-ocr (canonical pipeline).
Source: .opencode/skills/mistral-ocr/SKILL.md — site rebuilt 2026-09-05.
mistral-ocr¶
Mistral OCR — PDF/Image to Verbatim Text¶
When to Run¶
- User requests OCR of a PDF or image using Mistral's vision model.
- Verbatim transcription is required (legal documents, contracts, or precise text extraction).
- The
okf-ocrpipeline has already run, but user explicitly requests Mistral-based OCR for QA or secondary validation.
Dependencies¶
- ImageMagick: For PDF-to-image conversion (must be installed system-wide and in
PATH). - Download: ImageMagick Official Site
- Verify installation:
magick --version - Mistral Vision Model: Access via API (requires
MISTRAL_API_KEYin environment variables).
Core Principle¶
This skill performs verbatim transcription using Mistral's vision model. Every word, figure, punctuation mark, capitalisation, and line break is preserved exactly as it appears in the source. This is critical for legal documents where precision is non-negotiable.
Workflow¶
Phase 1: Convert PDF to Images (if input is PDF)¶
- Check input type: If the file is a PDF, convert it to images using ImageMagick.
- Command:
magick -density 300 "input.pdf" -quality 100 "output_page_%02d.png" - Resolution: 300 DPI (adjustable for higher quality if needed).
-
Output: One PNG image per PDF page.
-
Handle non-PDF images: If the input is already an image (PNG, JPG, etc.), skip conversion.
Phase 2: OCR with Mistral Vision Model¶
- Process each image: Send each image to Mistral's vision model for OCR.
- API endpoint:
https://api.mistral.ai/v1/vision(or relevant endpoint). -
Prompt: "Extract all text from this image verbatim. Preserve every word, figure, punctuation mark, capitalisation, and line break exactly as it appears. Do not interpret, correct, or summarize."
-
Combine results: Concatenate OCR results from all images into a single text file.
- Format: Include page separators (e.g.,
--- PAGE 1 ---). - Output filename:
<source_name>_V2_MISTRAL_OCR.txt(sidecar next to source).
Phase 3: Quality Assurance (QA)¶
- Verify completeness: Ensure all pages are processed and no text is truncated.
- Check metadata: Extract and log document type, parties, dates, and key figures.
- Flag issues: Mark unreadable sections as
[ILLEGIBLE]or[LOW CONFIDENCE].
Phase 4: Output and Logging¶
- Save transcription: Write verbatim text to
<source_name>_V2_MISTRAL_OCR.txt. - Save metadata: Log extracted metadata (document type, parties, dates, key figures) in a structured format (e.g., JSON sidecar
<source_name>_mistral_metadata.json). - Update OCR Log: Append a row to
97_OCR_Log/OCR_Log.csvwith: - Date
- Filename
- Document type
- Source path
- Destination path
- Method:
Mistral Vision Model - Quality Check:
PASS/PARTIAL/FAIL - Notes (e.g., "6 pages, verbatim")
Rules¶
- Never edit or redact content: Preserve the source exactly.
- Stop on unreadable text: If a section is genuinely unreadable, flag it as
[ILLEGIBLE]and continue. - Confidentiality: All processing happens locally or via secure API calls; no document content is stored externally.
Example Usage¶
User Request:¶
"OCR this contract PDF using Mistral."
Steps:¶
- Convert PDF to images:
magick -density 300 "Another Contract.pdf" "Another Contract_page_%02d.png" - OCR each image with Mistral's vision model.
- Combine results into
Another Contract_V2_MISTRAL_OCR.txt. - Log metadata and update
97_OCR_Log/OCR_Log.csv.
Self-Learning¶
- Record lessons in
learnings.md(same folder as this SKILL.md). - Example lessons:
- "Mistral vision model struggles with handwritten text in scanned PDFs; flag as
[HANDWRITTEN: ILLEGIBLE]." - "ImageMagick at 300 DPI produces optimal balance of quality and file size for Mistral OCR."
File Structure¶
mistral-ocr/
├── SKILL.md # This file
├── learnings.md # Self-learning log
├── tools/
│ ├── pdf_to_images.py # PDF-to-image conversion script
│ ├── mistral_ocr.py # Mistral vision model OCR script
│ └── combine_results.py # Combine OCR results into final output
└── templates/
└── metadata_template.json # Template for metadata sidecar
Tools¶
1. pdf_to_images.py¶
- Input: PDF file path, output directory, DPI (default: 300).
- Output: PNG images (one per page).
- Uses:
subprocessto call ImageMagick.
2. mistral_ocr.py¶
- Input: Image file path, Mistral API key.
- Output: Extracted text (verbatim).
- Uses:
requeststo call Mistral's vision API.
3. combine_results.py¶
- Input: Directory of OCR'd text files (one per image).
- Output: Combined text file with page separators.
Environment Setup¶
- Install ImageMagick and add to
PATH. - Set
MISTRAL_API_KEYin environment variables. - Install Python dependencies:
pip install requests pillow python-dotenv
Error Handling¶
- ImageMagick not found: Return error with installation instructions.
- Mistral API failure: Retry 3 times; if persistent, log error and skip the image.
- Unreadable text: Flag as
[ILLEGIBLE]and continue.
Performance Notes¶
- PDF-to-image conversion: ~3–10 seconds for a 6-page PDF at 300 DPI.
- Mistral OCR: ~1–2 seconds per image (depends on API latency).
- Total time for 6-page PDF: ~10–20 seconds.