A model can return valid JSON and still invent a missing invoice number. A useful boundary is a separate Directus collection for extraction candidates: software checks the shape; a person checks the source before anything reaches the final invoices collection.
Here is a small Python example using the Items REST API. The input below is synthetic. This is an application-side workflow, independent of Directus’s built-in AI Assistant.
1. Create a staging collection
Create invoice_candidates in the Data Studio with an automatically generated id and these fields:
| Field | Type | Notes |
|---|---|---|
| source_ref | String | Required; unique stable ID for one source document |
| invoice_number | String | Nullable |
| total_minor | Big Integer | Nullable; integer minor units for a fixed-currency workflow |
| currency | String | Nullable; this example accepts USD only |
| review_state | String | Required; default needs_review |
Use a server-side service account that can create candidates only. Its policy should constrain review_state to needs_review; do not let this account create final invoices or approve candidates. Give a separate reviewer the permissions to inspect and correct candidates. Keep public access off.
The fixed currency matters: do not assume every currency has two decimal places. Here 1250 means USD 12.50, while null means missing and 0 is an explicit zero.
2. Validate, then send a narrow payload
Set DIRECTUS_URL and DIRECTUS_TOKEN as server-side environment variables. Run with Python 3. The token is a Directus credential, not a model-provider key.
import json
import os
from urllib.request import Request, urlopen
def candidate(raw, source_ref):
if not isinstance(raw, dict):
raise ValueError("Expected a JSON object")
if not isinstance(source_ref, str) or not source_ref.strip():
raise ValueError("source_ref must come from your ingestion job")
number = raw.get("invoice_number")
if number is not None:
if not isinstance(number, str) or not number.strip():
raise ValueError("invoice_number must be nonempty text or null")
number = number.strip()
amount = raw.get("total_minor")
if amount is not None and (
type(amount) is not int or not 0 <= amount <= 10**12
):
raise ValueError("total_minor must be a bounded integer or null")
currency = raw.get("currency")
if currency not in (None, "USD"):
raise ValueError("Route other currencies to a separate workflow")
if amount is not None and currency is None:
raise ValueError("An amount needs an explicit currency")
return {
"source_ref": source_ref.strip(),
"invoice_number": number,
"total_minor": amount,
"currency": currency,
"review_state": "needs_review",
}
# Synthetic candidate; source_ref is assigned by the ingestion job.
raw = {"invoice_number": None, "total_minor": 1250, "currency": "USD"}
payload = candidate(raw, "demo-invoice-001")
request = Request(
os.environ["DIRECTUS_URL"].rstrip("/") + "/items/invoice_candidates",
data=json.dumps(payload).encode("utf-8"),
headers={
"Authorization": "Bearer " + os.environ["DIRECTUS_TOKEN"],
"Content-Type": "application/json",
},
method="POST",
)
with urlopen(request, timeout=20) as response:
print(json.load(response)["data"]["id"])
Notice that model-supplied id, source_ref, review_state or extra fields never pass through. Missing values remain null; type checks do not establish that an amount actually appears in the invoice. The reviewer must compare it with the source.
3. Handle retries and review explicitly
If a POST times out, its outcome is unknown. Have an authorized reconciliation process look up the same unique source_ref before deciding whether to retry. A duplicate constraint prevents a second candidate for that source; it does not prove that the existing row contains the intended data. Do not blindly generate a new source ID on each retry.
Before promotion to final invoices, require source comparison, required business fields, and reviewer approval. A dropdown or this Python function alone is not an authorization boundary: enforce the restrictions in Directus permissions and the promotion service as well.
Useful acceptance cases: missing invoice number stays null; zero remains zero; a string amount and a Boolean amount are rejected; an injected review_state: approved cannot approve anything; replaying the same source cannot create a duplicate.
Disclosure: I’m with Tokatlas. The extraction provider sits upstream of this boundary; these checks belong between its output and the Directus write, regardless of provider.
References: Directus Items API.