openai/openai-python · error · ValueError

Expected a non-empty value for `alert_id` but received {aler

Error message

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

What it means

The SDK requires alert_id to be non-empty when retrieving a spend alert; an empty value fails fast with ValueError rather than requesting a malformed URL.

Source

Thrown at src/openai/resources/admin/organization/projects/spend_alerts.py:133

        extra_body: Body | None = None,
        timeout: float | httpx2.Timeout | None | NotGiven = not_given,
    ) -> ProjectSpendAlert:
        """
        Retrieves a project spend alert.

        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}")
        if not alert_id:
            raise ValueError(f"Expected a non-empty value for `alert_id` but received {alert_id!r}")
        return self._get(
            path_template(
                "/organization/projects/{project_id}/spend_alerts/{alert_id}", project_id=project_id, alert_id=alert_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=ProjectSpendAlert,
        )

    def update(
        self,
        alert_id: str,
        *,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass the real alert ID, e.g. 'alert_abc123' (from create or list responses)
  2. Validate parsed event payloads contain alert_id before dispatching to retrieve
  3. Log the raw payload when the ID is missing to find the mapping bug

Example fix

// before
alert = client.admin.organization.projects.spend_alerts.retrieve(project_id=pid, alert_id="")
// after
alert = client.admin.organization.projects.spend_alerts.retrieve(project_id=pid, alert_id="alert_abc123")
Defensive patterns

Strategy: validation

Validate before calling

if not alert_id:
    raise ValueError("alert_id is required to retrieve a spend alert")

Type guard

def is_valid_alert_id(value: object) -> bool:
    return isinstance(value, str) and bool(value.strip())

Prevention

When it happens

Trigger: Calling spend_alerts.retrieve(...) with alert_id=None or '' (project_id may be valid).

Common situations: Parsing alert IDs from event payloads where the field is absent, or using a variable shadowed/overwritten earlier in the function.

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