tiangolo/fastapi · error · RuntimeError

Couldn't find pre section (<style>) in index.md

Error message

Couldn't find pre section (<style>) in index.md

What it means

Raised by generate_readme_content() in scripts/docs.py:359. After slicing the sponsor section it also needs the end of the front-matter <style> block to know where the README body begins: it searches index.md for '</style>\n\n' (scripts/docs.py:351). If that exact sequence is absent the README preamble cannot be reconstructed and the function aborts.

Source

Thrown at scripts/docs.py:359

            hashes, title, *_ = match.groups()
            line = f"{hashes} {title}"
        lines.append(line)
    return "\n".join(lines)


def generate_readme_content() -> str:
    en_index = en_docs_path / "docs" / "index.md"
    content = en_index.read_text("utf-8")
    content = remove_header_permalinks(content)  # remove permalinks from headers
    match_pre = re.search(r"</style>\n\n", content)
    match_start = re.search(r"<!-- sponsors -->", content)
    match_end = re.search(r"<!-- /sponsors -->", content)
    sponsors_data_path = en_docs_path / "data" / "sponsors.yml"
    sponsors = yaml.safe_load(sponsors_data_path.read_text(encoding="utf-8"))
    if not (match_start and match_end):
        raise RuntimeError("Couldn't auto-generate sponsors section")
    if not match_pre:
        raise RuntimeError("Couldn't find pre section (<style>) in index.md")
    frontmatter_end = match_pre.end()
    pre_end = match_start.end()
    post_start = match_end.start()
    template = Template(index_sponsors_template)
    message = template.render(sponsors=sponsors, sponsor_img_url=sponsor_img_url)
    pre_content = content[frontmatter_end:pre_end]
    post_content = content[post_start:]
    new_content = pre_content + message + post_content
    # Remove content between <!-- only-mkdocs --> and <!-- /only-mkdocs -->
    new_content = re.sub(
        r"<!-- only-mkdocs -->.*?<!-- /only-mkdocs -->",
        "",
        new_content,
        flags=re.DOTALL,
    )
    return new_content

View on GitHub (pinned to 3e8d1526d8)

Solutions

  1. Open docs/en/docs/index.md and verify a '<style>...</style>' block exists ending with '</style>' followed by a blank line.
  2. Restore the exact '</style>\n\n' sequence from git history.
  3. Re-run generate-readme.

Example fix

<!-- before: blank line after </style> removed -->
</style>
## FastAPI
<!-- after: restore the blank line -->
</style>

## FastAPI
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
import re

def has_style_block(index_md: Path) -> bool:
    return re.search(r"</style>\n\n", index_md.read_text(encoding="utf-8")) is not None

Try / catch

try:
    content = generate_readme_content()
except RuntimeError as e:
    if "pre section" in str(e):
        raise RuntimeError("index.md missing '</style>\n\n'; restore the style block") from e
    raise

Prevention

When it happens

Trigger: Running generate-readme (or generate_readme_content) after the '<style>' block in docs/en/docs/index.md was removed, moved, or reformatted so the literal '</style>\n\n' no longer appears.

Common situations: The <style> tag was reformatted (e.g. minified onto one line, or trailing blank line removed). The style block was deleted entirely. A formatter collapsed the blank line after </style>.

Related errors


AI-assisted analysis of tiangolo/fastapi@3e8d1526d8 (2026-08-11). Data as JSON: /api/errors/4ec4d474646e2d64. Report an issue: GitHub.