{
  "id": "python/pydantic-settings-env-parse-error",
  "signature": "pydantic_settings.exceptions.SettingsError: error parsing value for field \"allowed_hosts\" from source \"EnvSettingsSource\"",
  "signature_zh": "pydantic_settings.exceptions.SettingsError: 从 \"EnvSettingsSource\" 解析字段 \"allowed_hosts\" 的值时出错",
  "regex": "pydantic_settings\\.exceptions\\.SettingsError:\\ error\\ parsing\\ value\\ for\\ field\\ \"allowed_hosts\"\\ from\\ source\\ \"EnvSettingsSource\"",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "An environment variable holding a complex type (list, dict) could not be parsed because Pydantic Settings expects JSON for non-scalar types by default.",
  "root_cause_type": "generic",
  "root_cause_zh": "环境变量中存放的是复杂类型（列表、字典），但 Pydantic Settings 默认期望非标量类型为 JSON 格式，因此解析失败。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Shell quoting affects the string delivered to the process but does not change the JSON parsing requirement.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Case sensitivity does not affect value parsing, only key matching.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Naming convention is irrelevant; the content format is the problem.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "export ALLOWED_HOSTS='[\"a.com\", \"b.com\"]'",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "from pydantic_settings import BaseSettings, SettingsConfigDict\nclass S(BaseSettings):\n    model_config = SettingsConfigDict(env_parse_none_str=\"null\")\n    allowed_hosts: list[str] = []\n# Set env var: ALLOWED_HOSTS=a.com,b.com and override the source",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from pydantic import field_validator\nclass S(BaseSettings):\n    allowed_hosts: list[str] = []\n    @field_validator(\"allowed_hosts\", mode=\"before\")\n    @classmethod\n    def split(cls, v):\n        if isinstance(v, str):\n            return [x.strip() for x in v.split(\",\")]\n        return v",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}