omi icon indicating copy to clipboard operation
omi copied to clipboard

Open Food Facts plugin lacks typed request models, crashes on non-string nutrient grades, lacks lifespan client pooling, and has no hermetic test suite

Open Ivansky1 opened this issue 2 months ago • 2 comments

Describe the bug

In plugins/omi-openfoodfacts-app, several reliability, validation, and architectural issues make the integration fragile and unmonitored:

  1. Untyped Request Bodies & Missing Validation Models:

    • The endpoints (/tools/search_foods, /tools/lookup_barcode, /tools/compare_foods, /tools/check_allergens) parse untyped dictionaries manually using await request.json().
    • Missing or malformed payloads can lead to unhandled AttributeError or inconsistent error formats.
    • For chat-tool integration, input validation errors should return a structured 200 JSON envelope (ChatToolResponse) rather than unhandled 422/500 errors.
  2. Crash Vectors in Product and Nutrient Parsing:

    • _summarize_product: calls .upper() directly on nutriscore_grade and ecoscore_grade without 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 unhandled AttributeError: 'int' object has no attribute 'upper'.
    • _nutrient: performs per_100g_key in nutriments where nutriments = product.get("nutriments") or {}. If product.get("nutriments") is a scalar or non-dict type, this raises a TypeError or performs invalid substring checks.
    • _search_foods: traverses payload.get("products", []) assuming every element is a valid dictionary without defensive type checking.
  3. Synchronous Threadpool I/O & Connection Churn:

    • The plugin relies on synchronous requests.get executed within run_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.
  4. Missing Hermetic Unit Tests & CI Checks Manifest Entry:

    • plugins/omi-openfoodfacts-app is completely absent from .github/checks-manifest.yaml.
    • Existing test files (test_nutrient_basis.py, test_search_endpoint.py) do not run under standard Python stdlib unittest without external test runners.
    • No unified hermetic test suite exists covering endpoint contracts, barcode sanitization, allergen matching logic, error handling, and defensive parsing.

Proposed Fix

  1. Create plugins/omi-openfoodfacts-app/models.py:

    • Define typed Pydantic models: SearchFoodsRequest, LookupBarcodeRequest, CompareFoodsRequest, CheckAllergensRequest, and hardened ChatToolResponse.
    • Add input trimming, integer bounds clamping for page_size (1-10), and validation ensuring at least one of barcode or query is provided in allergen checks.
  2. Harden plugins/omi-openfoodfacts-app/main.py:

    • Add @app.exception_handler(RequestValidationError) returning HTTP 200 with standard ChatToolResponse error envelope.
    • Guard nutriscore_grade and ecoscore_grade with safe string coercion before .upper().
    • Guard _nutrient with isinstance(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.
  3. Comprehensive Hermetic Test Suite:

    • Add plugins/omi-openfoodfacts-app/test_main.py containing 20+ isolated, deterministic unit tests with zero external network calls.
    • Make existing tests runnable under stdlib test runners.
    • Register openfoodfacts-tests in .github/checks-manifest.yaml.

Ivansky1 avatar Sep 15 '26 09:09 Ivansky1

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.

Ivansky1 avatar Sep 15 '26 09:09 Ivansky1

/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.

ducanh3005 avatar Oct 24 '26 16:10 ducanh3005