{"record":{"id":"d6f8645052ea2093","repo":"ATH-MaaS/Pixelle-Video","slug":"batch-batch-idx-prompt-count-mismatch-attempt","errorCode":null,"errorMessage":"Batch {batch_idx} prompt count mismatch (attempt {attempt}/{max_retries}):\n  Expected: {len(batch_narrations)} prompts\n  Got: {len(batch_prompts)} prompts","messagePattern":"Batch (.+?) prompt count mismatch \\(attempt (.+?)/(.+?)\\):\n  Expected: (.+?) prompts\n  Got: (.+?) prompts","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pixelle_video/utils/content_generators.py","lineNumber":346,"sourceCode":"                if \"image_prompts\" not in result:\n                    raise KeyError(\"Invalid response format: missing 'image_prompts'\")\n                \n                batch_prompts = result[\"image_prompts\"]\n                \n                # Validate count\n                if len(batch_prompts) != len(batch_narrations):\n                    error_msg = (\n                        f\"Batch {batch_idx} prompt count mismatch (attempt {attempt}/{max_retries}):\\n\"\n                        f\"  Expected: {len(batch_narrations)} prompts\\n\"\n                        f\"  Got: {len(batch_prompts)} prompts\"\n                    )\n                    logger.warning(error_msg)\n                    \n                    if attempt < max_retries:\n                        logger.info(f\"Retrying batch {batch_idx}...\")\n                        continue\n                    else:\n                        raise ValueError(error_msg)\n                \n                # Success!\n                logger.info(f\"✅ Batch {batch_idx} completed successfully ({len(batch_prompts)} prompts)\")\n                all_prompts.extend(batch_prompts)\n                \n                # Report progress\n                if progress_callback:\n                    progress_callback(\n                        len(all_prompts),\n                        len(narrations),\n                        f\"Batch {batch_idx}/{len(batches)} completed\"\n                    )\n                \n                break\n                \n            except json.JSONDecodeError as e:\n                logger.error(f\"Batch {batch_idx} JSON parse error (attempt {attempt}/{max_retries}): {e}\")\n                if attempt >= max_retries:","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/utils/content_generators.py#L328-L364","documentation":"After parsing the LLM response, generate_image_prompts validates that the 'image_prompts' array has exactly as many entries as the batch's narrations. On mismatch it warns and retries; once max_retries is exhausted it raises ValueError with expected vs got counts. It means the model returned too few or too many image prompts for the batch.","triggerScenarios":"generate_image_prompts called (directly, via generate_image_prompt, __call__, or plan_visuals) where for every retry attempt len(result['image_prompts']) != len(batch_narrations), e.g. model merges or skips narrations, or the batch is too large for the model to enumerate faithfully.","commonSituations":"Long narration lists per batch; model summarizing multiple scenes into one prompt; off-by-one when the model numbers items but drops one; low-context models truncating long JSON arrays.","solutions":["Reduce the batch size (fewer narrations per LLM call) so counts stay aligned.","Raise max_retries to give the corrective retry loop more attempts.","Strengthen the prompt: instruct one prompt per narration, in order, matching the count exactly.","Post-process defensively: pad by splitting prompts or truncate to len(batch_narrations) instead of failing.","Use structured/JSON-mode output which enforces array length via a schema."],"exampleFix":"// before\nraise ValueError(error_msg)\n// after\nif len(batch_prompts) > len(batch_narrations):\n    batch_prompts = batch_prompts[:len(batch_narrations)]  # tolerate extra prompts\nelse:\n    raise ValueError(error_msg)","handlingStrategy":"retry","validationCode":"def batch_counts_match(result: dict, narrations: list) -> bool:\n    prompts = result.get(\"image_prompts\") if isinstance(result, dict) else None\n    return isinstance(prompts, list) and len(prompts) == len(narrations)","typeGuard":"def is_valid_prompt_batch(obj: object, expected: int) -> bool:\n    return (isinstance(obj, dict) and isinstance(obj.get(\"image_prompts\"), list)\n            and len(obj[\"image_prompts\"]) == expected)","tryCatchPattern":"try:\n    prompts = generator.generate_image_prompts(narrations)\nexcept ValueError as e:\n    if \"prompt count mismatch\" in str(e):\n        prompts = generator.generate_image_prompts(narrations, max_retries=max_retries + 2)\n    else:\n        raise","preventionTips":["State the exact required count in the prompt before the narrations","Keep per-batch narration counts low (e.g. <=5)","Use schema-constrained output with minItems/maxItems","Surface the retry warnings in logs to spot chronically failing batch sizes"],"tags":["llm","validation","count-mismatch","batching"],"backgroundTag":"llm-output-count-mismatch","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}