openai/openai-python · error · ValueError

Expected a non-empty value for `project_id` but received {pr

Error message

Expected a non-empty value for `project_id` but received {project_id!r}

What it means

The sync DataRetention `retrieve` method validates that `project_id` is non-empty before building GET `/organization/projects/{project_id}/data_retention`. A falsy value would produce a malformed path, so the SDK raises this ValueError without any network I/O.

Source

Thrown at src/openai/resources/admin/organization/projects/data_retention.py:66

        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx2.Timeout | None | NotGiven = not_given,
    ) -> ProjectDataRetention:
        """
        Retrieves project data retention controls.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not project_id:
            raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}")
        return self._get(
            path_template("/organization/projects/{project_id}/data_retention", project_id=project_id),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                security={"admin_api_key_auth": True},
            ),
            cast_to=ProjectDataRetention,
        )

    def update(
        self,
        project_id: str,
        *,
        retention_type: Literal[
            "organization_default",

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass the real project id from the dashboard or projects.list
  2. Validate config at startup
  3. Use os.environ[...] with KeyError rather than getenv defaulting to None when the value is required

Example fix

# before
retention = client.admin.organization.projects.data_retention.retrieve(project_id=os.getenv("PROJECT_ID"))

# after
retention = client.admin.organization.projects.data_retention.retrieve(project_id=os.environ["PROJECT_ID"])
Defensive patterns

Strategy: validation

Validate before calling

pid = os.environ["OPENAI_PROJECT_ID"]
retention = client.admin.organization.projects.data_retention.retrieve(project_id=pid)

Type guard

def is_valid_project_id(v: str | None) -> bool:
    return isinstance(v, str) and bool(v.strip())

Try / catch

try:
    retention = client.admin.organization.projects.data_retention.retrieve(project_id=pid)
except ValueError as e:
    logger.error("bad project_id: %s", e)
    return None

Prevention

When it happens

Trigger: Calling data_retention.retrieve with project_id as '', None, or whitespace; reading the id from an unset env var or empty config.

Common situations: Env var missing in deployed environments; blank project fields in config stores; scaffolding placeholders never replaced.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/752556dab6a0690d. Report an issue: GitHub.