{"record":{"id":"9dbb9a990df738eb","repo":"harry0703/MoneyPrinterTurbo","slug":"elevenlabs-generation-failed-response-status-cod","errorCode":null,"errorMessage":"ElevenLabs generation failed ({response.status_code}): {_safe_response_error(response)}","messagePattern":"ElevenLabs generation failed \\((.+?)\\): (.+?)","errorType":"exception","errorClass":"ElevenLabsMusicError","httpStatus":null,"severity":"error","filePath":"app/services/elevenlabs_music.py","lineNumber":333,"sourceCode":"                    f\"{_base_url()}{VIDEO_TO_MUSIC_PATH}\",\n                    headers={\"xi-api-key\": get_api_key()},\n                    params={\"output_format\": \"mp3_44100_128\"},\n                    files=[\n                        (\n                            # 官方文档把表单数组展示为 ``videos[]``，但 2026-07-18\n                            # 生产接口会对该字段返回 422，实际 Starlette 参数名为\n                            # ``videos``。重复上传时 requests 可继续添加同名字段。\n                            \"videos\",\n                            (Path(video_path).name, video_file, \"video/mp4\"),\n                        )\n                    ],\n                    data=request_data,\n                    stream=True,\n                    timeout=_request_timeout(),\n                )\n                with response:\n                    if not response.ok:\n                        raise ElevenLabsMusicError(\n                            \"ElevenLabs generation failed \"\n                            f\"({response.status_code}): \"\n                            f\"{_safe_response_error(response)}\"\n                        )\n                    total_bytes = _stream_audio(response, temp_audio_path)\n        except requests.RequestException as exc:\n            # 下载阶段断线也属于请求失败，必须进入任务降级逻辑，不能留下半条\n            # 音频或让已经生成的视频因为第三方网络波动整体失败。\n            raise ElevenLabsMusicError(\n                f\"failed to request ElevenLabs music: {exc}\"\n            ) from exc\n\n        try:\n            bgm_service.validate_audio_file(temp_audio_path, timeout_seconds=120)\n        except (bgm_service.BgmUploadError, bgm_service.BgmServiceError) as exc:\n            raise ElevenLabsMusicError(\n                \"ElevenLabs returned audio that FFmpeg cannot decode\"\n            ) from exc","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/elevenlabs_music.py#L315-L351","documentation":"Raised by _request_bgm when the ElevenLabs video-to-music HTTP call (POST /v1/music/video-to-music) returns a non-2xx status. The message embeds the HTTP status code and a truncated (<=500 bytes) response body read via _safe_response_error, so the upstream ElevenLabs error text is preserved without dumping the whole body into logs.","triggerScenarios":"ElevenLabs rejects the multipart upload: 401/403 for a bad or endpoint-restricted API key, 422 for wrong multipart field names or invalid prompt/duration, 429 for quota exhaustion, 5xx for ElevenLabs outages. Only raised when response.ok is false after the streaming POST completes the status phase.","commonSituations":"Free-plan key without Music API access, key restricted per-endpoint in the ElevenLabs dashboard, prompt longer than 1000 chars slipping past client checks, wrong music_base_url (self-hosted proxy returning HTML errors), or rate limiting during batch video generation.","solutions":["Read the embedded status code and body: 401/403 means rotate the key or grant the Music endpoint permission; 422 usually means the proxy expects the 'videos' multipart field or the prompt/duration is malformed","Run test_connection() first to verify the key and that the account is not on a free plan","Check music_base_url in config; it must point at https://api.elevenlabs.io or a compatible proxy that mirrors the video-to-music API","Retry once after a delay if the status is 429 or 5xx — both are transient on ElevenLabs"],"exampleFix":"# before\noutput = _request_bgm(proxy_path, out_path, prompt)  # raises opaque ElevenLabsMusicError mid-pipeline\n\n# after\nfrom app.services import elevenlabs_music as elm\nelm.test_connection()  # cheap preflight: verifies key + paid plan before any upload\noutput = elm.generate_bgm(video_path, out_path, duration, prompt)","handlingStrategy":"validation","validationCode":"from app.services import elevenlabs_music as elm\n\nelm.test_connection()  # raises ElevenLabsAuthenticationError / PaidPlanRequired early\nassert len(prompt.strip()) <= 1000 and 0 < duration <= 600","typeGuard":null,"tryCatchPattern":"try:\n    out = elm.generate_bgm(video, out_path, duration, prompt)\nexcept elm.ElevenLabsAuthenticationError:\n    notify_user(\"invalid ElevenLabs key\")\nexcept elm.ElevenLabsMusicError as e:\n    log_and_degrade(e)  # keep video without BGM instead of failing the task","preventionTips":["Call test_connection() before enabling the BGM pipeline step","Keep prompts under 1000 chars and durations under 600s at the task layer","Treat 429/5xx as retryable; 401/403 as configuration failures"],"tags":["elevenlabs","http","api","music-generation"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}