binary-husky/gpt_academic · error · HTTPException

Request to the target service failed: {str(e)}

Error message

Request to the target service failed: {str(e)}

What it means

In the TTS forwarding path for TTS_TYPE == 'LOCAL_SOVITS_API', the request body is proxied with httpx to GPT_SOVITS_URL (timeout=60). Any httpx.RequestError — connection refused, DNS failure, timeout, TLS error — is caught and returned as HTTPException(400, 'Request to the target service failed: {e}'). It signals the local GPT-SoVITS inference service is unreachable, not a problem with gpt_academic itself.

Source

Thrown at shared_utils/fastapi_server.py:237

                        temp_file = os.path.join(temp_folder, f'{temp_file_name}.mp3')
                        await tts.save(temp_file)
                        try:
                            mp3_audio = AudioSegment.from_file(temp_file, format="mp3")
                            mp3_audio.export(temp_file, format="wav")
                            with open(temp_file, 'rb') as wav_file: t = wav_file.read()
                            os.remove(temp_file)
                            return Response(content=t)
                        except:
                            raise RuntimeError("ffmpeg未安装,无法处理EdgeTTS音频。安装方法见`https://github.com/jiaaro/pydub#getting-ffmpeg-set-up`")
                    if TTS_TYPE == "LOCAL_SOVITS_API":
                        # Forward the request to the target service
                        TARGET_URL = get_conf("GPT_SOVITS_URL")
                        body = await request.body()
                        resp = await client.post(TARGET_URL, content=body, timeout=60)
                        # Return the response from the target service
                        return Response(content=resp.content, status_code=resp.status_code, headers=dict(resp.headers))
                except httpx.RequestError as e:
                    raise HTTPException(status_code=400, detail=f"Request to the target service failed: {str(e)}")
        @gradio_app.post("/vits")
        async def forward_post_request(request: Request):
            return await forward_request(request, "POST")

    # --- --- app_lifespan --- ---
    from contextlib import asynccontextmanager
    @asynccontextmanager
    async def app_lifespan(app):
        async def startup_gradio_app():
            if gradio_app.get_blocks().enable_queue:
                gradio_app.get_blocks().startup_events()
        async def shutdown_gradio_app():
            pass
        await startup_gradio_app() # startup logic here
        yield  # The application will serve requests after this point
        await shutdown_gradio_app() # cleanup/shutdown logic here

    # --- --- FastAPI --- ---

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Start the GPT-SoVITS service and confirm it responds (curl its health/TTS endpoint directly).
  2. Fix GPT_SOVITS_URL in config.py to the correct host:port reachable from this process.
  3. If long audio routinely exceeds 60 s, raise the timeout in the client.post call or split the text.
  4. In docker-compose, use service names instead of 127.0.0.1.

Example fix

# before
resp = await client.post(TARGET_URL, content=body, timeout=60)

# after
resp = await client.post(TARGET_URL, content=body, timeout=120)  # long syntheses
Defensive patterns

Strategy: retry

Validate before calling

import httpx, socket
from toolbox import get_conf
TARGET_URL = get_conf('GPT_SOVITS_URL')
host = httpx.URL(TARGET_URL).host
port = httpx.URL(TARGET_URL).port or 80
with socket.socket() as s:
    s.settimeout(2)
    sovit_up = s.connect_ex((host, port)) == 0
if not sovit_up:
    return JSONResponse({'error': 'GPT-SoVITS service not reachable'}, 503)

Try / catch

for attempt in range(2):
    try:
        resp = await client.post(TARGET_URL, content=body, timeout=60)
        break
    except httpx.RequestError:
        if attempt == 1:
            raise HTTPException(503, 'TTS backend unavailable')
        await asyncio.sleep(1)

Prevention

When it happens

Trigger: TTS request while the GPT-SoVITS service is down, wrong GPT_SOVITS_URL in config, service listening on a different host/port than configured, or inference exceeding the 60 s httpx timeout.

Common situations: GPT-SoVITS not started or still loading models when the first TTS call arrives; docker networking misconfiguration (localhost vs container name); firewall blocking the port; long synthesis exceeding timeout=60.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/f8ec1bb3b8a84ee4. Report an issue: GitHub.