python config_error ai_generated true

pydantic_settings.exceptions.SettingsError: error parsing value for field "DATABASE_URL" from source "EnvSettingsSource"

ID: python/starlette-uvicorn-invalid-env

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-09-04First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

A Pydantic BaseSettings field expects a URL/int/bool but the environment variable is malformed or empty.

generic

中文

Pydantic BaseSettings 字段期望 URL/int/bool,但环境变量格式错误或为空。

Workarounds

  1. 92% success
    from pydantic import PostgresDsn
    class Settings(BaseSettings):
        database_url: PostgresDsn
  2. 95% success
    export DATABASE_URL='postgresql://user:pass@localhost:5432/db'

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Downstream code receives None and fails at connect time.

  2. 85% fail

    Silently connects to the wrong database.