{"record":{"id":"16d23dec395a3ec3","repo":"harry0703/MoneyPrinterTurbo","slug":"failed-to-request-elevenlabs-music-exc","errorCode":null,"errorMessage":"failed to request ElevenLabs music: {exc}","messagePattern":"failed to request ElevenLabs music: (.+?)","errorType":"exception","errorClass":"ElevenLabsMusicError","httpStatus":null,"severity":"error","filePath":"app/services/elevenlabs_music.py","lineNumber":342,"sourceCode":"                            (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\n        os.replace(temp_audio_path, output_path)\n        temp_audio_path = \"\"\n        logger.info(\n            \"ElevenLabs background music generated: \"\n            f\"output={output_path}, size={total_bytes} bytes\"\n        )\n        return output_path\n    finally:\n        _remove_file(temp_audio_path)","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/elevenlabs_music.py#L324-L360","documentation":"Wraps any requests.RequestException (DNS failure, connection reset, TLS error, read timeout from the (15, <=1800s) timeout tuple, or a dropped stream mid-download) raised while POSTing to ElevenLabs or streaming the audio body back. Wrapping it in ElevenLabsMusicError forces the task into its degradation logic instead of failing an already-rendered video because of third-party network churn.","triggerScenarios":"Any requests exception during the streaming POST /v1/music/video-to-music or during _stream_audio: ConnectionError (no route to api.elevenlabs.io), ChunkedEncodingError (server closed the stream mid-audio), ReadTimeout when generation exceeds the configured music_timeout read timeout.","commonSituations":"Corporate proxies/firewalls blocking api.elevenlabs.io, flaky home uplinks dropping long streams, music_timeout set too low for long videos (600s content can take minutes), DNS failures in containers.","solutions":["Retry the generation once or twice with backoff — the error is explicitly treated as transient degradation, not a hard failure","Increase the elevenlabs.music_timeout config value if the read timeout fires on long videos (it is capped at 1800s)","Verify network egress to api.elevenlabs.io (curl -I https://api.elevenlabs.io) and fix proxy/DNS if the connection never opens","If it fails repeatedly, let the task fall back to no-BGM output rather than aborting the rendered video"],"exampleFix":"# before\npath = _request_bgm(proxy_path, output_path, prompt)\n\n# after\nfor attempt in range(2):\n    try:\n        path = _request_bgm(proxy_path, output_path, prompt)\n        break\n    except ElevenLabsMusicError as exc:\n        if attempt == 1:\n            raise\n        time.sleep(10)","handlingStrategy":"retry","validationCode":"import socket, urllib.parse\nhost = urllib.parse.urlparse(\"https://api.elevenlabs.io\").hostname\nsocket.gethostbyname(host)  # fail fast on DNS problems","typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        return elm.generate_bgm(video, out, duration, prompt)\n    except elm.ElevenLabsMusicError as e:\n        if attempt == 2 or not is_transient(e):\n            return fallback_no_bgm()\n        time.sleep(2 ** attempt * 5)","preventionTips":["Size elevenlabs.music_timeout generously for long videos (cap 1800s)","Run generation with retry + exponential backoff wrapped around generate_bgm","Degrade to no-BGM output on repeated network failure rather than failing the video"],"tags":["network","retry","elevenlabs","streaming"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}