{"record":{"id":"5892063ad2ba671b","repo":"mem0ai/mem0","slug":"either-api-key-must-be-provided-or-turbopuffer-a","errorCode":null,"errorMessage":"Either 'api_key' must be provided or TURBOPUFFER_API_KEY environment variable must be set.","messagePattern":"Either 'api_key' must be provided or TURBOPUFFER_API_KEY environment variable must be set\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/configs/vector_stores/turbopuffer.py","lineNumber":27,"sourceCode":"    embedding_model_dims: int = Field(1536, description=\"Dimensions of the embedding model\")\n    api_key: Optional[str] = Field(None, description=\"API key for Turbopuffer\")\n    region: str = Field(\"gcp-us-central1\", description=\"Turbopuffer region (e.g., 'gcp-us-central1', 'aws-us-west-2')\")\n    distance_metric: str = Field(\n        \"cosine_distance\",\n        description=\"Distance metric for vector similarity ('cosine_distance' or 'euclidean_squared')\",\n    )\n    batch_size: int = Field(100, description=\"Batch size for bulk operations\")\n    extra_params: Optional[Dict[str, Any]] = Field(\n        None,\n        description=\"Additional parameters for Turbopuffer client\",\n    )\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def check_api_key(cls, values: Dict[str, Any]) -> Dict[str, Any]:\n        api_key = values.get(\"api_key\")\n        if not api_key and \"TURBOPUFFER_API_KEY\" not in os.environ:\n            raise ValueError(\n                \"Either 'api_key' must be provided or TURBOPUFFER_API_KEY environment variable must be set.\"\n            )\n        return values\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_extra_fields(cls, values: Dict[str, Any]) -> Dict[str, Any]:\n        allowed_fields = set(cls.model_fields.keys())\n        input_fields = set(values.keys())\n        extra_fields = input_fields - allowed_fields\n        if extra_fields:\n            raise ValueError(\n                f\"Extra fields not allowed: {', '.join(extra_fields)}. \"\n                f\"Please input only the following fields: {', '.join(allowed_fields)}\"\n            )\n        return values\n\n    model_config = ConfigDict(arbitrary_types_allowed=True)","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/configs/vector_stores/turbopuffer.py#L9-L45","documentation":"TurbopufferConfig's `before` validator requires either a non-empty 'api_key' in the config or the environment variable TURBOPUFFER_API_KEY to be set. Because it runs before field defaults, an omitted/None/empty api_key with no env var raises this ValueError at Memory construction time.","triggerScenarios":"Memory(vector_store={'provider': 'turbopuffer'}) with no api_key while TURBOPUFFER_API_KEY is unset in the process; passing api_key=None or api_key=''; running under a service (systemd, Docker, cron) where the env var was set in the shell but not exported into the service environment.","commonSituations":"Setting the env var in ~/.bashrc but launching the app from an IDE or Docker where it is absent; rotating/renaming the variable name; assuming mem0 reads TURBOPUFFER_KEY or another spelling.","solutions":["Set TURBOPUFFER_API_KEY in the environment the process actually runs in (export it, add to .env loaded before import, or Docker ENV)","Or pass the key explicitly: vector_store={'provider': 'turbopuffer', 'config': {'api_key': '...'}}","Verify with a quick check (python -c \"import os; print(os.environ.get('TURBOPUFFER_API_KEY'))\") in the same environment that runs the app"],"exampleFix":"# before\nos.environ.pop(\"TURBOPUFFER_API_KEY\", None)\nconfig = {}\n\n# after\nconfig = {\"api_key\": \"tpuf_...\"}  # or: os.environ[\"TURBOPUFFER_API_KEY\"] = \"tpuf_...\"","handlingStrategy":"validation","validationCode":"import os\nif not cfg.get(\"api_key\") and not os.environ.get(\"TURBOPUFFER_API_KEY\"):\n    raise ConfigError(\"Provide api_key or set TURBOPUFFER_API_KEY\")","typeGuard":null,"tryCatchPattern":"try:\n    memory = Memory(config=full_config)\nexcept ValueError as e:\n    if \"TURBOPUFFER_API_KEY\" in str(e):\n        os.environ[\"TURBOPUFFER_API_KEY\"] = load_secret(\"turbopuffer\")\n        memory = Memory(config=full_config)\n    else:\n        raise","preventionTips":["Set provider credentials in the process environment at startup, not in shell rc files","In containers, pass secrets via --env-file or the orchestrator's secret mechanism"],"tags":["turbopuffer","vector-store","api-key","environment","config","validation"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}