Add template detection from page 1 - #41
Merged
Merged
Conversation
- src/template_detect.py: classify page 1 of a PDF against the template
registry via the vision model. Rasterizes page 1, lists every known
template ID (implemented AND stubs) in the prompt, asks for
{template_id, confidence}, and validates the result.
- Recognising a stub is deliberate: if the model confidently says
"this is a Natation Québec form", detect_template returns that id
with is_implemented=False so the caller can surface the specific
"not implemented yet, see issue X" message (via templates.get_template)
rather than a vague "couldn't identify". TemplateDetection carries
template_id, confidence, and is_implemented.
- Error paths raise TemplateDetectionError with a --template override
hint: model said "unknown", model returned an unregistered id (a
hallucinated province), confidence below threshold (default 0.7), or
a non-dict response. The unknown/unregistered case also links the
new-issue page so users can contribute a sample PDF.
- Refactor: promoted vision_extract._try_parse_json to public
try_parse_json() (underscore dropped, name otherwise unchanged) so
both modules strip markdown ```json fences the same way. Updated the
two call sites in vision_extract.
- tests/test_template_detect.py: 19 tests over a mocked client —
prompt lists impl + stub ids with display names, happy path for an
implemented template, stub-detected-but-unimplemented, every error
path (unknown / unregistered / below-threshold / non-dict / missing
confidence), custom threshold, and confidence clamping.
- docs/architecture.md: marked implemented; noted CLI wiring is #10.
Closes #9.
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
src/template_detect.py— classify page 1 of a PDF against the template registry via the vision model. Rasterizes page 1, lists every known template ID (implemented and stubs) in the prompt, asks for{template_id, confidence}, validates.detect_templatereturns that id withis_implemented=Falseso the caller surfaces the specific "not implemented yet, see issue X" message (viatemplates.get_template) rather than a vague "couldn't identify".TemplateDetectionErrorwith a--templateoverride hint: model saidunknown, returned an unregistered (hallucinated) id, confidence below threshold (default 0.7), or a non-dict response.vision_extract._try_parse_jsonto publictry_parse_json()(underscore dropped, name otherwise unchanged) so both modules strip```jsonfences the same way.Test plan
pytest tests/ -q— 250 passing (231 prior + 19 new): prompt lists impl + stub ids; happy path; stub-detected-but-unimplemented; every error path; custom threshold; confidence clamping.Closes #9.
🤖 Generated with Claude Code