openai/openai-python · error · ValueError

Expected a non-empty value for `role_id` but received {role_

Error message

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

What it means

The sync Roles.retrieve method validates role_id last; an empty role_id raises this ValueError so that the URL path segment /roles/{role_id} is never rendered empty. No network request occurs.

Source

Thrown at src/openai/resources/admin/organization/projects/users/roles.py:120

    ) -> RoleRetrieveResponse:
        """
        Retrieves a project role assigned to a user.

        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 user_id:
            raise ValueError(f"Expected a non-empty value for `user_id` but received {user_id!r}")
        if not role_id:
            raise ValueError(f"Expected a non-empty value for `role_id` but received {role_id!r}")
        return self._get(
            path_template(
                "/projects/{project_id}/users/{user_id}/roles/{role_id}",
                project_id=project_id,
                user_id=user_id,
                role_id=role_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=RoleRetrieveResponse,
        )

    def list(

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass a valid role_id (e.g. 'role-...') obtained from roles.list or role creation
  2. Validate all three path parameters before the call

Example fix

// before
roles.retrieve(project_id=project_id, user_id=user_id, role_id="")
// after
roles.retrieve(project_id=project_id, user_id=user_id, role_id=role_id)
Defensive patterns

Strategy: validation

Validate before calling

if not role_id or not role_id.strip():
    raise ValueError("role_id required for role lookup")
roles.retrieve(project_id=project_id, user_id=user_id, role_id=role_id)

Type guard

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

Try / catch

try:
    roles.retrieve(project_id, user_id, role_id)
except ValueError as e:
    if "role_id" in str(e):
        logger.warning("no role id; skipping: %s", e)
    else:
        raise

Prevention

When it happens

Trigger: Calling roles.retrieve(project_id=..., user_id=..., role_id='') where the role id is blank or None.

Common situations: Hardcoding or copying a role_id that was truncated, or reading it from a response/object where the field was absent.

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