WWhat's On My Food
← All entries
·Caleb Barranco

Grounding Gemini in the Additive Database

How I moved the AI layer from free-form answers to source-grounded explanations that cite the additive database on every response.

Additive and ingredient database screen
The additive database is now the citation target for every AI explanation — tap a cited ingredient to jump straight to its entry.
Barcode scanner screen with AI toggle
The AI toggle on the scanner controls whether responses are generated with Gemini or rendered from the rules-only fallback.

The problem

Last month's pipeline worked, but the AI explanations occasionally drifted — confident wording without a clear tie back to the evidence the deterministic rules produced. For a project about helping shoppers understand ingredients, that is exactly the failure mode I cannot ship.

This month I focused on grounding: making sure every AI-generated sentence points back to a row in the additive database or to a rule that fired on the scanned product.

What I built

I restructured the prompt into a strict evidence-in / citations-out contract. The Node.js backend now assembles a compact evidence packet — matched additives, sugar and sodium thresholds that tripped, allergen flags — and Gemini is instructed to answer only from that packet and cite each claim by additive name.

On the client, the Additive & Ingredient Database screen became the source of truth users can tap into. Every citation in an explanation deep-links to the corresponding entry, so the UI itself reinforces the grounding rather than hiding it.

I also added a rules-only fallback path: if Gemini is unavailable or returns an ungrounded response, the app renders the deterministic summary instead of guessing.

Retrospective

What went right: response quality is noticeably steadier, and the citation contract makes regressions easy to spot in review.

What went wrong: prompt iteration ate more time than I planned, and I had to rebuild a few evidence-packet shapes before landing on one the model handled reliably.

How I will improve: I am adding a small evaluation harness next month so I can compare prompt changes against a fixed set of scans instead of eyeballing them.