fix: handle HowToSection missing itemListElement in clean_instructions (#7616)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
This commit is contained in:
Ishir Bhan
2026-07-30 16:17:48 -04:00
committed by GitHub
parent 1a595d4d0f
commit f1854201e9
2 changed files with 53 additions and 1 deletions
+4 -1
View File
@@ -220,11 +220,14 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N
# },
# }
#
# Some sites (e.g. NYT Cooking) emit empty HowToSection placeholders
# with no itemListElement key, or use "item" per the schema.org spec.
# Use .get() with both fallbacks so those sections are skipped gracefully.
steps_object = typing.cast(list[dict[str, str]], steps_object)
return clean_instructions(
functools.reduce(
operator.concat, # type: ignore
[x["itemListElement"] for x in steps_object],
[x.get("itemListElement", x.get("item", [])) for x in steps_object],
[],
)
)
@@ -224,6 +224,55 @@ instruction_test_cases = (
],
expected=None,
),
CleanerCase(
test_id="how to steps with empty section (e.g. NYT Cooking)",
input=[
{
"@type": "HowToSection",
},
{
"@type": "HowToSection",
"itemListElement": [
{
"@type": "HowToStep",
"text": "Instruction A",
},
{
"@type": "HowToStep",
"text": "Instruction B",
},
{
"@type": "HowToStep",
"text": "Instruction C",
},
],
},
],
expected=None,
),
CleanerCase(
test_id="how to steps using 'item' key (schema.org alternate)",
input=[
{
"@type": "HowToSection",
"item": [
{
"@type": "HowToStep",
"text": "Instruction A",
},
{
"@type": "HowToStep",
"text": "Instruction B",
},
{
"@type": "HowToStep",
"text": "Instruction C",
},
],
},
],
expected=None,
),
CleanerCase(
test_id="excessive whitespace str (1)",
input="Instruction A\n\nInstruction B\n\nInstruction C\n\n",