{"record":{"id":"649f72ed5b223a29","repo":"ATH-MaaS/Pixelle-Video","slug":"invalid-response-format-missing-image-prompts","errorCode":null,"errorMessage":"Invalid response format: missing 'image_prompts'","messagePattern":"Invalid response format: missing 'image_prompts'","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"pixelle_video/utils/content_generators.py","lineNumber":329,"sourceCode":"                prompt = build_image_prompt_prompt(\n                    narrations=batch_narrations,\n                    min_words=min_words,\n                    max_words=max_words\n                )\n                \n                response = await llm_service(\n                    prompt=prompt,\n                    temperature=0.7,\n                    max_tokens=8192\n                )\n                \n                logger.debug(f\"Batch {batch_idx} attempt {attempt}: LLM response length: {len(response)} chars\")\n                \n                # Parse JSON\n                result = _parse_json(response)\n                \n                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                ","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/utils/content_generators.py#L311-L347","documentation":"generate_image_prompts batches narrations and asks an LLM to return JSON containing an 'image_prompts' key. When the parsed LLM response lacks that key, a KeyError('Invalid response format: missing 'image_prompts'') is raised so the retry loop can regenerate the batch. It indicates the model deviated from the required JSON schema or the JSON parse produced an object without the field.","triggerScenarios":"The LLM returns valid JSON but without an 'image_prompts' array — e.g. the model wrapped output in prose, returned prompts under a different key, returned a bare array, or a retry consumed a malformed response after _parse_json salvaged a partial object.","commonSituations":"Weaker/cheaper LLM models ignoring the JSON schema instruction; prompts asking for many narrations in one batch causing truncated or restructured output; model updates changing output shape; custom system prompts overriding the format instructions.","solutions":["Retry the call — generate_image_prompts already loops max_retries times; transient model drift usually resolves on retry.","Inspect the raw LLM response (logged at debug level) to see the actual JSON shape and adjust the prompt to explicitly demand {\"image_prompts\": [...]}.","Reduce batch size so the model reliably emits one prompt per narration.","Switch to a model/provider that follows structured output or use JSON-mode/forced-schema output.","Patch _parse_json/callers to accept alternative key names or a top-level array."],"exampleFix":"// before\nresult = _parse_json(response)\nif \"image_prompts\" not in result:\n    raise KeyError(\"Invalid response format: missing 'image_prompts'\")\n// after\nresult = _parse_json(response)\nif \"image_prompts\" not in result:\n    if isinstance(result, list):  # model returned a bare array\n        result = {\"image_prompts\": result}\n    else:\n        raise KeyError(\"Invalid response format: missing 'image_prompts'\")","handlingStrategy":"validation","validationCode":"def looks_like_image_prompt_response(payload) -> bool:\n    return isinstance(payload, dict) and isinstance(payload.get(\"image_prompts\"), list) and len(payload[\"image_prompts\"]) > 0\n# call after _parse_json, before consuming result","typeGuard":"def has_image_prompts(obj: object) -> bool:\n    return isinstance(obj, dict) and isinstance(obj.get(\"image_prompts\"), list) and all(isinstance(p, str) for p in obj[\"image_prompts\"])","tryCatchPattern":"try:\n    prompts = generator.generate_image_prompt(narrations)\nexcept KeyError as e:\n    if \"image_prompts\" in str(e):\n        logger.warning(\"LLM ignored schema, retrying with smaller batch\")\n        prompts = generator.generate_image_prompt(narrations[: len(narrations) // 2])\n    else:\n        raise","preventionTips":["Use a provider JSON mode / response schema when available","Keep batches small so the model reliably follows the schema","Log raw LLM responses at debug level for post-mortems","Prefer models known for structured-output compliance"],"tags":["llm","json-parsing","schema-validation","retry"],"backgroundTag":"llm-response-schema-mismatch","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}