omi
omi copied to clipboard
fix(plugins/openfoodfacts): barcode hygiene, graceful 404s, and multilingual allergen matching
Problem Description
The omi-openfoodfacts-app plugin provides nutrition, Nutri-Score, allergen detection, and barcode lookup tools for Omi users. However, in real-world usage and adversarial test scenarios, several edge cases lead to service errors, false negative allergen reports, or unexpected crashes:
-
Barcode & URL Sanitization Limitations:
- When users query with formatted barcodes (e.g.
barcode: 3017666510800,#012345678905), packaged product URLs (https://world.openfoodfacts.org/product/3017666510800-nutella), or strings with wrapping quotes/spaces, the plugin fails or sends corrupted queries. Conversely, unstructured natural language text containing numbers (e.g.coca cola 1000ml) was previously extracting raw digits into bogus barcode queries.
- When users query with formatted barcodes (e.g.
-
Unhandled 404 Status as Service Failure:
- In
_openfoodfacts_get, querying an uncataloged barcode returns HTTP 404 from upstream. Callingresponse.raise_for_status()treated expected misses as internal crashes ("Open Food Facts request failed"), obscuring the friendly "product not found" message and masking true upstream network errors.
- In
-
Dirty Data & Non-Finite Numbers in Community Catalog:
- Open Food Facts catalog items occasionally contain dirty types or booleans in
nutriscore_grade,ecoscore_grade, ornutriments. Calling.upper()or membership testing directly on these fields triggersAttributeErrororTypeError. Similarly, non-finite values like"inf"or extreme floats passed to_safe_inttriggered unhandledOverflowError.
- Open Food Facts catalog items occasionally contain dirty types or booleans in
-
Allergen Detection Edge Cases (CJK Negation, Accents, and Singular/Plural Variants):
- Non-spaced CJK ingredients (Chinese, Japanese, Korean) had a blunt whole-text negation check that caused false negatives if a disclaimer mentioned "no peanut oil" while ingredients contained peanuts.
- Latin-script ingredients previously missed word stems (e.g.,
apples/grapestruncated toappl/grap). - Matching against official allergen tags (
known_allergens,traces) previously required exact string equality, failing when a user specified singular terms (e.g.avoid: ["peanut"]) against standard plural tags (peanuts).
Proposed Solution
- Robust Barcode & URL Extraction:
- Implement
sanitize_barcodewith prefix stripping, URL slug isolation, GTIN length/digit validation, and alphanumeric noise rejection.
- Implement
- Graceful 404 Handling:
- Return structured
{"not_found": True, "status_code": 404}without raising exception noise or masking upstream errors.
- Return structured
- Resilient Data Sanitization:
- Guard
_safe_intagainst non-finite floats andOverflowError. Safely cast string grades and validate dict structures fornutriments.
- Guard
- Context-Aware Multilingual Allergen Matching:
- Traverse individual token matches in CJK with localized negation prefix checks (
无,不含,零,免). - Support Latin plural/singular stemming (
_term_variants) and English negation markers (free from,free of,no,without). - Match both singular and plural forms against official Open Food Facts tags and traces.
- Traverse individual token matches in CJK with localized negation prefix checks (
- Hermetic Test Suite & CI Check:
- Add 29 hermetic standard-library tests in
test_main.py(0.02s execution). - Register
omi-openfoodfacts-testsin.github/checks-manifest.yaml.
- Add 29 hermetic standard-library tests in
Bounty Claim
- Bounty: $50
- USDT (TRC20):
TGKmfSUmt2dggErAo8BicaCoXhkMwLetxV - Bitcoin (BTC):
1NkUnv23H65ZYJn64pseAcWRA6sJitxh2f - Contact Email:
[email protected]