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 SpendLimit.retrieve method requires a non-empty project_id because it is interpolated into the URL path /organization/projects/{project_id}/spend_limit. An empty or otherwise falsy project_id raises this ValueError before any network call, protecting against malformed requests.

Source

Thrown at src/openai/resources/admin/organization/projects/spend_limit.py:67

        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx2.Timeout | None | NotGiven = not_given,
    ) -> ProjectSpendLimit:
        """
        Get a project's hard spend limit.

        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}/spend_limit", 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=ProjectSpendLimit,
        )

    def update(
        self,
        project_id: str,
        *,
        currency: Literal["USD"],
        interval: Literal["month"],

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass a valid project id such as 'proj_abc123'
  2. Check that the variable/env var supplying project_id is set and non-empty before the call

Example fix

// before
client.admin.organization.projects.spend_limit.retrieve(project_id="")
// after
project_id = os.environ["OPENAI_PROJECT_ID"]
assert project_id
client.admin.organization.projects.spend_limit.retrieve(project_id=project_id)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(project_id, str) and project_id.strip(), "project_id must be a non-empty string"
client.admin.organization.projects.spend_limit.retrieve(project_id=project_id)

Type guard

def is_valid_project_id(value: object) -> bool:
    return isinstance(value, str) and value.startswith("proj_")

Try / catch

try:
    client.admin.organization.projects.spend_limit.retrieve(project_id)
except ValueError as e:
    logger.error("Invalid argument: %s", e)
    raise SystemExit(2)

Prevention

When it happens

Trigger: Calling client.admin.organization.projects.spend_limit.retrieve('') or with a None/empty project_id, e.g. when the project id was never loaded from configuration.

Common situations: Using an env var like OPENAI_PROJECT_ID that is unset (so it reads as ''), or copy-pasting example code without filling in the project id.

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/04613e8ece1a3387. Report an issue: GitHub.