From 9bb350ee32cc6bbfbfe147c45f3eac01fdc11c6a Mon Sep 17 00:00:00 2001 From: Tom Hunter Date: Thu, 15 Oct 2026 18:46:30 +0000 Subject: [PATCH] fix(scraper): unwrap schema.org Text nodes in clean_ingredients (#8683) Co-authored-by: Claude Opus 4.8 (1M context) --- mealie/services/scraper/cleaner.py | 16 ++++++++++++++++ .../scraper_tests/test_cleaner_parts.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/mealie/services/scraper/cleaner.py b/mealie/services/scraper/cleaner.py index c701573b6..8a850ca24 100644 --- a/mealie/services/scraper/cleaner.py +++ b/mealie/services/scraper/cleaner.py @@ -336,12 +336,27 @@ def _sanitize_instruction_text(line: str | dict) -> str: return clean_line +def unwrap_text_node(value: typing.Any) -> typing.Any: + """ + schema.org permits any Text subtype wherever a plain string is expected, so an + ingredient may arrive as `{"@type": "PronounceableText", "textValue": "1 cup flour"}` + rather than `"1 cup flour"`. Instacart emits these. Returns the inner string when the + value is such a wrapper, otherwise returns it untouched. + """ + if isinstance(value, dict) and "textValue" in value: + text = value.get("textValue") + if isinstance(text, str): + return text + return value + + def clean_ingredients(ingredients: list | str | None, default: list | None = None) -> list[str | dict]: """ ingredient attempts to parse the ingredients field from a recipe and return a list of Supported Structures: - `["1 cup flour"]` - A list of strings + - `[{"@type": "PronounceableText", "textValue": "1 cup flour"}]` - schema.org Text nodes - `"1 cup flour"` - A string - `None` - returns an empty list @@ -354,6 +369,7 @@ def clean_ingredients(ingredients: list | str | None, default: list | None = Non case list(ingredients): cleaned_ingredients: list[str | dict] = [] for ing in ingredients: + ing = unwrap_text_node(ing) if isinstance(ing, dict): cleaned_ingredients.append({clean_string(k): clean_string(v) for k, v in ing.items()}) else: diff --git a/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py b/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py index 23c1ab6b7..69626554e 100644 --- a/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py +++ b/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py @@ -546,6 +546,24 @@ ingredients_test_cases = ( test_id="multiple ingredient dictionary", exception=TypeError, ), + CleanerCase( + input=[ + {"@type": "PronounceableText", "textValue": "1 cup of flour"}, + {"@type": "PronounceableText", "textValue": "1 cup of sugar"}, + ], + expected=["1 cup of flour", "1 cup of sugar"], + test_id="schema.org PronounceableText nodes", + ), + CleanerCase( + input=["1 cup of flour", {"@type": "PronounceableText", "textValue": "1 cup of sugar"}], + expected=["1 cup of flour", "1 cup of sugar"], + test_id="mixed strings and PronounceableText nodes", + ), + CleanerCase( + input=[{"name": "flour", "amount": "1 cup"}], + expected=[{"name": "flour", "amount": "1 cup"}], + test_id="dict without textValue is left untouched", + ), )