Add vision extraction for scanned PDFs - #40
Merged
Merged
Conversation
- src/vision_extract.py: per-page extraction via a local vision model
(Qwen2.5-VL through Ollama). Rasterizes each page, prompts the model
for a JSON object, parses it into the same PageExtraction shape the
form-field path emits — so merge / output stay path-agnostic.
- Prompt construction: a canonical-schema JSON skeleton built from
src.schema constants (can't drift), plus the template's
vision_prompt_addendum, plus the source filename so the model can
resolve session_number from the form text, the filename, or both
(and report which via the "source" field).
- Model call: client.generate(format="json", options temperature=0)
with the page PNG. One retry on a structural parse failure with a
clarifying nudge appended; raises VisionExtractionError if the second
attempt also fails. Tolerates markdown code fences some models wrap
JSON in despite instructions.
- Parsing: each field becomes a typed FieldValue with clamped [0,1]
confidence; null values are dropped; bare scalars (model ignored the
{value, confidence} shape) get a default 0.5 confidence. successful
is coerced to true/false/null defensively — a model emitting the
string "false" must NOT become a truthy non-empty string downstream
(regression-tested). session_number preserves its source provenance.
- Caching: raw model responses are written to a .raw.json sidecar keyed
by page number and tagged with the model. Re-runs reuse the cache
(fast iteration on parsing logic without re-invoking the slow model);
use_cache=False (the CLI's --no-cache) forces a fresh call; a cache
built with a different model is ignored.
- VisionClient is a Protocol so the module doesn't hard-depend on the
ollama package surface; tests inject a fake client returning canned
JSON.
- tests/test_vision_extract.py: 41 tests over a mocked client. Prompt
content, good-response parsing, successful/confidence coercion
(incl. the stringy-false regression), retry-then-succeed and
retry-then-fail, markdown-fence stripping, missing-rows retry, and
the cache round-trip (write → reuse → --no-cache → model-mismatch).
- docs/architecture.md: marked implemented; noted it's not yet wired
into the CLI (that's #10).
Closes #8.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Test plan
Closes #8.
🤖 Generated with Claude Code