Tadinda sheetSheet Note N1 · Sheet 05 TR / EN

Note N1 · Tadinda

ALLERGEN RISK ENGINE

Don't give the LLM the final say in an allergy app: a rule engine and an LLM together

Sheet
TADINDA
Topic
FastAPI · Open Food Facts · LLM
Source
services/risk_engine.py
Date
2026-11-12
Reading
2 min

Short answer: for decisions where being wrong is costly, such as allergies, run a deterministic rule engine first and make the LLM only a second layer. In Tadinda, if the rule engine finds a risky match, the result returns without ever reaching the LLM. If it finds no risk, the product data, the user profile and the rule engine's result go to a local LLM; the LLM can turn the result into risky but cannot turn risky into safe. If the LLM fails, the rule engine's result is used.

01Data

  • The Open Food Facts record is fetched by barcode and cached in the database.
  • The ingredients text is read from the Turkish field first. If missing, it is translated into Turkish; the translation is written to both Redis and the database, so the same product is never translated twice.
  • Open Food Facts' allergen, additive and category tags, plus text read from a label photo via OCR, are added.
  • Everything is merged into one text and split into words. The analysis result is also cached in Redis.

02Rule engine

Each allergen has a list of synonyms; if one of them appears in the product text, there is a match. Allergens not in the list are searched by their own name, as are the sensitivities the user adds.

rules = {
    "gluten": ["wheat", "barley", "rye", "gluten", "bugday", "arpa", "cavdar", "triticale"],
    "milk":   ["milk", "lactose", "sut", "peynir", "whey", "casein", "kazein"],
    "peanut": ["peanut", "fistik", "yer fistigi", "yerfistigi"],
}

Because OCR can miss letters, matching is not limited to exact words: words with a similarity ratio above 0.82 also count (difflib.SequenceMatcher).

03Strictness modes

  • Normal: risk if the allergen or a synonym appears in the text.
  • Strict: additionally, risk if cross-contamination phrases such as "may contain", "traces of", "shared facility" (and their Turkish equivalents) appear together with the allergen.
  • Extreme: additionally, risk if the product category relates to the allergen: biscuits, bread, pasta, flour for gluten; cheese, yoghurt, butter for milk.

An automatic mode is also computed when the profile is saved. A marker such as anaphylaxis or coeliac disease, six or more sensitivities, or four sensitivities together with two diets makes the profile strict.

04LLM layer

result = evaluate_basic_risk(...)
if result.status != "RISKY":
    try:
        result = llm.analyze_structured({..., "rule_engine_result": result.model_dump()})
    except Exception:
        pass  # if the LLM fails, the rule engine's result stands

A risky result never reaches the LLM, so the LLM cannot lower the risk and no tokens are spent on that request. The LLM also gets the rule engine's result; it can turn the outcome into risky for content in a foreign language or an ingredient the rules missed. The LLM runs locally (an OpenAI-compatible endpoint).

05Uncertainty

The result is one of three states, RISKY, SAFE or UNCERTAIN, with a confidence value. If there is no barcode record and no readable text, the app does not say "safe"; it says "uncertain". For a risky result the user is shown, line by line, which word in the text triggered it.

→More notes from this project

—End of notewritten by MUHAMMEDPYZ