feat: add schema validation for LLM extracted fields#212
Closed
utkarshqz wants to merge 2 commits intofireform-core:mainfrom
Closed
feat: add schema validation for LLM extracted fields#212utkarshqz wants to merge 2 commits intofireform-core:mainfrom
utkarshqz wants to merge 2 commits intofireform-core:mainfrom
Conversation
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.
feat: add schema validation for LLM extracted fields
Summary
This PR adds field-level schema validation to the LLM extraction pipeline, directly addressing GSoC Expected Outcome #1 which requires "improved AI extraction accuracy through schema validation".
After Mistral extracts values from the transcript, each value is now automatically validated against expected patterns for its field type before being written to the PDF. Validation issues are reported as structured warnings — never as hard failures — ensuring the pipeline remains robust while giving developers visibility into extraction quality.
Closes / Fixes
Closes #114
Addresses #173 — hallucination detection catches repeated values across fields
Addresses #186 — LLM test coverage now at 40 tests (was 0)
Type of change
What changed and why
1. 🔍
validate_extracted_fields()— new method insrc/llm.pyCalled automatically inside
main_loop()after every extraction. Runs 5 checks:"not-a-phone"→ warning@and a domain"johndoe"→ warningDD/MM/YYYY,YYYY-MM-DD, etc."yesterday"→ warning{"f1": "John", "f2": "John", "f3": "John"}→ warningDesign decisions:
Nonevalues are skipped — no false positives for empty fieldsget_validation_warnings()Real output example (from local testing):
Or when issues are found:
2. 🧪 5 new unit tests —
tests/test_llm.py::TestSchemaValidationtest_valid_fields_return_no_warningstest_invalid_email_flagged@→ warning producedtest_repeated_values_flagged_as_hallucinationtest_null_values_skippedNonevalues → no false positive warningstest_warnings_stored_on_instanceget_validation_warnings()returns correct data3. 📚
docs/TESTING.md— updatedTestSchemaValidationsection describing all 5 new test casesHow Has This Been Tested?
python -m pytest tests/ -v 57 passed, 14 warnings in 0.35smain_loop()call ✅Test Configuration:
Checklist