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 second guard in role retrieve (roles.py:114): after group_id passes, a falsy role_id raises ValueError before the GET /organization/groups/{group_id}/roles/{role_id} request is built. It exists because an empty final path segment would hit the collection URL instead of a specific role.

Source

Thrown at src/openai/resources/admin/organization/groups/roles.py:114

        extra_body: Body | None = None,
        timeout: float | httpx2.Timeout | None | NotGiven = not_given,
    ) -> RoleRetrieveResponse:
        """
        Retrieves an organization role assigned to a group.

        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 group_id:
            raise ValueError(f"Expected a non-empty value for `group_id` but received {group_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("/organization/groups/{group_id}/roles/{role_id}", group_id=group_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(
        self,
        group_id: str,
        *,
        after: str | Omit = omit,
        limit: int | Omit = omit,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Use the id field from a prior roles.list() call
  2. Validate role_id is a non-empty string before retrieving
  3. Check for typos in the dict key/attribute supplying the role ID

Example fix

# before
role = client.admin.organization.groups.roles.retrieve(group_id=gid, role_id=role.get('id'))

# after
rid = role.get('id') or ''
assert rid, 'role has no id'
role = client.admin.organization.groups.roles.retrieve(group_id=gid, role_id=rid)
Defensive patterns

Strategy: type-guard

Validate before calling

rid = role.get('id')
if not isinstance(rid, str) or not rid:
    raise ValueError('role id missing or empty')

Type guard

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

Prevention

When it happens

Trigger: client.admin.organization.groups.roles.retrieve(group_id='grp_1', role_id=None) or role_id=''; e.g. when the role ID comes from an optional field that was not provided.

Common situations: Iterating role IDs from a partially filled config; roles list returning items whose id key was misread (e.g. using role['name'] instead of role['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/5c97bc04c07e98f5. Report an issue: GitHub.