DataCareerHub
Explore learning
← Applied AIFree companion · 50 minute suggested activity

4. Extract structured fields without hiding ambiguity

Applied AI · Build a narrow deterministic baseline before considering a generative extractor.

Learn → Try → ExplainReviewed 2026-10-15
01

Objective & prerequisites

Build a narrow deterministic baseline before considering a generative extractor. Prerequisites: Python functions, JSON, lists and the existing API/testing foundations. Extract the full kit and open a terminal in applied-ai-kit. The core lab uses Python 3.10+ with no third-party dependencies or network. Actual new-text semantic inference is a separate optional local setup, tested on Python 3.12/macOS arm64. All CedarLab policies, invoices and review cases are fictional teaching material; they are not DataCareerHub service terms. No payment, registration, live chatbot or visitor-data collection is activated. These free companions do not change course progress or protected access.

python3 lab.py
python3 -m unittest discover -s tests -v
02

Understand the concept

Structured extraction turns a source document into named typed fields. Begin with a clear schema and a baseline whose assumptions can be inspected. The reference accepts exactly one Invoice, Currency and Total line in a small fictional format. It converts decimal strings directly into integer cents, preserving a valid zero and avoiding floating-point money rounding. Repeated totals, missing values, unsupported currencies and thousands separators are refused rather than guessed. This parser is rule-based, not an LLM or a general invoice product. A later model must be compared against it on agreed source formats and error categories. Preserve the original source and record whether a field was extracted, missing, ambiguous or outside the supported contract.

03

Worked example

I1 is EUR 125.40 and becomes 12540 cents; I2 is EUR 0.00 and becomes zero. I3 has two totals and is rejected, I4 has no total and is rejected, I5 uses USD and I6 uses a comma separator, both unsupported here. The report matches the six declared fixture outcomes. That means agreement with this limited contract, not general invoice accuracy or accounting readiness. No OCR, PDF layout processing or tax interpretation is implemented.

from lab import extract_invoice,load
cases=load("invoices.json")
assert extract_invoice(cases[0]["text"])["total_cents"]==12540
assert extract_invoice(cases[1]["text"])["total_cents"]==0
04

Try it yourself

Run all six cases and build a table of ID, accepted/rejected, fields and reason. Add a negative amount and a three-decimal amount in a copy. Explain why silently choosing the last total or converting an unknown amount to zero would destroy evidence. Write your prediction, inspect the source and record actual results. Keep a copy of the original fixtures before making changes.

Worked solution

Accepted IDs are I1 and I2; rejected IDs are I3–I6. Negative and three-decimal values fail the deliberately nonnegative two-decimal format. A real invoice may contain refunds or different locales, so new formats require a separately specified and tested contract rather than a claim that the source is inherently wrong. Keep missing or ambiguous values distinct from a known zero.

06

Knowledge check

Does the extractor accepting EUR 0.00 justify replacing a missing total with zero?

Check and explain

No. The first is explicit evidence; the second invents a value. Missing totals must be handled as missing under the contract.

08

Evidence, mistakes & handover

Record the executed mode: actual local encoder, saved-vector replay, deterministic extraction or human factual review. They establish different things. Keep model/version, source hashes, commands, expected outcomes, failures and limitations with your work. Never turn a schema pass, cosine score or quotation into a claim of proven truth. The download contains a worksheet, independent controls, worked solutions, a factual-review rubric, privacy/cost contract and mini-project checklist. Credit the supplied starting point and describe your own contribution.

09

Authoritative reference

Read the official reference ↗

— official source for the underlying mechanism. Teaching prose and CedarLab cases are original. Model/runtime compatibility, licensing and operating limits require review for any later deployment; this course does not activate a production AI service.

Make the reasoning yours.

Try the activity before reading its reference. Keep your sources, commands and assumptions with your work. Companion activities are unsaved self-practice and do not award a certificate or change lesson progress.

Resume guidance and examples are for learning and preparation only. They do not guarantee interviews, a job offer or employment.

Public registration, payments and new paid enrolment remain disabled. These free resources do not grant access to protected course modules.

Learner dashboard ↗ · Ask a question ↗