{"record":{"id":"b04c9cf272fafec3","repo":"tiangolo/fastapi","slug":"number-of-code-blocks-does-not-match-the-number-in","errorCode":null,"errorMessage":"Number of code blocks does not match the number in the original document ({len(code_blocks)} vs {len(original_code_blocks)})","messagePattern":"Number of code blocks does not match the number in the original document \\((.+?) vs (.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"scripts/doc_parsing_utils.py","lineNumber":656,"sourceCode":"            code_block.append(line_b)\n\n    return code_block\n\n\ndef replace_multiline_code_blocks_in_text(\n    text: list[str],\n    code_blocks: list[MultilineCodeBlockInfo],\n    original_code_blocks: list[MultilineCodeBlockInfo],\n) -> list[str]:\n    \"\"\"\n    Update each code block in `text` with the corresponding code block from\n    `original_code_blocks` with comments taken from `code_blocks`.\n\n    Raises ValueError if the number, language, or shape of code blocks do not match.\n    \"\"\"\n\n    if len(code_blocks) != len(original_code_blocks):\n        raise ValueError(\n            \"Number of code blocks does not match the number in the original document \"\n            f\"({len(code_blocks)} vs {len(original_code_blocks)})\"\n        )\n\n    modified_text = text.copy()\n    for block, original_block in zip(code_blocks, original_code_blocks, strict=True):\n        updated_content = replace_multiline_code_block(block, original_block)\n\n        start_line_index = block[\"start_line_no\"] - 1\n        for i, updated_line in enumerate(updated_content):\n            modified_text[start_line_index + i] = updated_line\n\n    return modified_text\n\n\n# All checks\n# --------------------------------------------------------------------------------------\n","sourceCodeStart":638,"sourceCodeEnd":674,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/scripts/doc_parsing_utils.py#L638-L674","documentation":"Raised by replace_multiline_code_blocks_in_text() in scripts/doc_parsing_utils.py:656. It is the top-level guard before pairing translated fenced code blocks with English ones: the two documents must contain the same number of fenced code blocks. extract_multiline_code_blocks() (scripts/doc_parsing_utils.py:482) walks lines tracking triple and quad backtick fences, so this fires when a translation has gained or lost a whole fenced block relative to English.","triggerScenarios":"check_translation() calls replace_multiline_code_blocks_in_text(doc_lines, doc_code_blocks, en_code_blocks). Fires when len(doc_code_blocks) != len(en_code_blocks) — e.g. the translation has 5 fenced blocks but the English original has 6.","commonSituations":"A translator deleted a fenced example, duplicated one, or accidentally merged two blocks by removing a fence. The English doc added a new code example and the translation has not been updated. A stray ``` inside prose (e.g. inline `code` mistakenly fenced) opened/closed a phantom block.","solutions":["Compare the fenced code blocks in the translated file against the English source at the same path under docs/en/.","Add the missing fenced block or remove the extra one so the counts match.","Watch for accidental triple-backtick sequences inside inline code or prose that the extractor counts as a real fence.","Re-run the docs translation check to confirm both block count and per-block line counts (error 100) now pass."],"exampleFix":"// before — translation is missing the second example block\n```Python\napp = FastAPI()\n```\n// after — restore the missing English block as a stub for translation\n```Python\napp = FastAPI()\n```\n\n```Python\n@app.get(\"/\")\ndef read_root():\n    return {\"Hello\": \"World\"}\n```","handlingStrategy":"validation","validationCode":"from scripts.doc_parsing_utils import extract_multiline_code_blocks\n\ndef block_counts_match(en_lines: list[str], translated_lines: list[str]) -> bool:\n    return len(extract_multiline_code_blocks(en_lines)) == len(\n        extract_multiline_code_blocks(translated_lines)\n    )","typeGuard":null,"tryCatchPattern":"try:\n    check_translation(doc_lines, en_doc_lines, lang_code, auto_fix=False, path=path)\nexcept ValueError as e:\n    if \"Number of code blocks\" in str(e):\n        logging.error(f\"Translation {path} has a different code-block count than English\")\n        raise","preventionTips":["Do not add or remove fenced code blocks in translations; only translate in-place.","If English adds an example, mirror the block in every language.","Beware accidental ``` inside prose that the extractor counts as a fence."],"tags":["docs","translation","markdown","validation"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}