Open Food Facts plugin lacks typed request models, crashes on non-string nutrient grades, lacks lifespan client pooling, and has no hermetic test suite
Describe the bug
In plugins/omi-openfoodfacts-app, several reliability, validation, and architectural issues make the integration fragile and unmonitored:
-
Untyped Request Bodies & Missing Validation Models:
- The endpoints (
/tools/search_foods,/tools/lookup_barcode,/tools/compare_foods,/tools/check_allergens) parse untyped dictionaries manually usingawait request.json(). - Missing or malformed payloads can lead to unhandled
AttributeErroror inconsistent error formats. - For chat-tool integration, input validation errors should return a structured 200 JSON envelope (
ChatToolResponse) rather than unhandled 422/500 errors.
- The endpoints (
-
Crash Vectors in Product and Nutrient Parsing:
_summarize_product: calls.upper()directly onnutriscore_gradeandecoscore_gradewithout verifying they are strings:(product.get("nutriscore_grade") or "").upper(). If the upstream API returns non-string values (e.g. numeric status codes, booleans, or objects), this raises an unhandledAttributeError: 'int' object has no attribute 'upper'._nutrient: performsper_100g_key in nutrimentswherenutriments = product.get("nutriments") or {}. Ifproduct.get("nutriments")is a scalar or non-dict type, this raises aTypeErroror performs invalid substring checks._search_foods: traversespayload.get("products", [])assuming every element is a valid dictionary without defensive type checking.
-
Synchronous Threadpool I/O & Connection Churn:
- The plugin relies on synchronous
requests.getexecuted withinrun_in_threadpool, incurring thread context switches and establishing new TCP/TLS connections on every request instead of utilizing an async pooled HTTP client with a lifespan manager.
- The plugin relies on synchronous
-
Missing Hermetic Unit Tests & CI Checks Manifest Entry:
plugins/omi-openfoodfacts-appis completely absent from.github/checks-manifest.yaml.- Existing test files (
test_nutrient_basis.py,test_search_endpoint.py) do not run under standard Python stdlibunittestwithout external test runners. - No unified hermetic test suite exists covering endpoint contracts, barcode sanitization, allergen matching logic, error handling, and defensive parsing.
Proposed Fix
-
Create
plugins/omi-openfoodfacts-app/models.py:- Define typed Pydantic models:
SearchFoodsRequest,LookupBarcodeRequest,CompareFoodsRequest,CheckAllergensRequest, and hardenedChatToolResponse. - Add input trimming, integer bounds clamping for
page_size(1-10), and validation ensuring at least one ofbarcodeorqueryis provided in allergen checks.
- Define typed Pydantic models:
-
Harden
plugins/omi-openfoodfacts-app/main.py:- Add
@app.exception_handler(RequestValidationError)returning HTTP 200 with standardChatToolResponseerror envelope. - Guard
nutriscore_gradeandecoscore_gradewith safe string coercion before.upper(). - Guard
_nutrientwithisinstance(nutriments, dict)checks. - Add defensive filtering for non-dict items in search results.
- Implement persistent async HTTP client pooling via FastAPI lifespan with graceful fallback.
- Add
-
Comprehensive Hermetic Test Suite:
- Add
plugins/omi-openfoodfacts-app/test_main.pycontaining 20+ isolated, deterministic unit tests with zero external network calls. - Make existing tests runnable under stdlib test runners.
- Register
openfoodfacts-testsin.github/checks-manifest.yaml.
- Add
Resolved via PR #13988. Includes typed request models in models.py, defensive parsing for non-string nutrient grades and non-dict payloads, lifespan async HTTP client pooling, and hermetic unit test suite registered in .github/checks-manifest.yaml.
/attempt #13987
Submitting a PR adding typed request models (models.py), defensive nutrient/grade/payload parsing, lifespan httpx.AsyncClient pooling, and a 44-test hermetic suite for omi-openfoodfacts-app.