Skip to content

Add template detection from page 1 - #41

Merged
gavinbee merged 1 commit into
mainfrom
9-template-detection
May 29, 2026
Merged

Add template detection from page 1#41
gavinbee merged 1 commit into
mainfrom
9-template-detection

Conversation

@gavinbee

@gavinbee gavinbee commented May 29, 2026

Copy link
Copy Markdown
Collaborator

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.
  • 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 surfaces the specific "not implemented yet, see issue X" message (via templates.get_template) rather than a vague "couldn't identify".
  • Error paths raise TemplateDetectionError with a --template override hint: model said unknown, returned an unregistered (hallucinated) id, confidence below threshold (default 0.7), or a non-dict response.
  • Refactor: promoted vision_extract._try_parse_json to public try_parse_json() (underscore dropped, name otherwise unchanged) so both modules strip ```json fences 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.
  • CI goes green.

Closes #9.

🤖 Generated with Claude Code

- 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>

@gavinbee gavinbee left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gavinbee
gavinbee merged commit ff2059e into main May 29, 2026
1 check passed
@gavinbee
gavinbee deleted the 9-template-detection branch May 29, 2026 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template detection

1 participant