openai/openai-python · error · ValueError

Expected a non-empty value for `user_id` but received {user_

Error message

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

What it means

The sync Roles.create method validates user_id in addition to project_id before POSTing to /projects/{project_id}/users/{user_id}/roles. An empty user_id raises this ValueError client-side; no role assignment request is made.

Source

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

    ) -> RoleCreateResponse:
        """
        Assigns a project role to a user within a project.

        Args:
          role_id: Identifier of the role to assign.

          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}")
        return self._post(
            path_template("/projects/{project_id}/users/{user_id}/roles", project_id=project_id, user_id=user_id),
            body=maybe_transform({"role_id": role_id}, role_create_params.RoleCreateParams),
            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=RoleCreateResponse,
        )

    def retrieve(
        self,
        role_id: str,
        *,
        project_id: str,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Resolve the user_id (e.g. 'user-...') from client.admin.users.list or your identity store before assigning a role
  2. Guard: if not user_id: skip/log instead of calling create

Example fix

// before
roles.create(project_id=project_id, user_id="", role_id=role_id)
// after
user = next(u for u in client.admin.users.list() if u.email == email)
roles.create(project_id=project_id, user_id=user.id, role_id=role_id)
Defensive patterns

Strategy: validation

Validate before calling

if not user_id or not user_id.strip():
    raise ValueError("user_id required")
client.admin.organization.projects.users.roles.create(project_id=project_id, user_id=user_id, role_id=role_id)

Type guard

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

Try / catch

try:
    roles.create(project_id, user_id, role_id)
except ValueError as e:
    if "user_id" in str(e):
        return None  # skip this user
    raise

Prevention

When it happens

Trigger: Calling roles.create(..., user_id='', role_id='role_...') where the user id is blank, None, or a defaulted empty string.

Common situations: Looking up a user by email, failing to find them, and passing the empty result as user_id; or form input where the user field was left blank.

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