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 Roles.create method requires both project_id and user_id to be non-empty because they form the URL /projects/{project_id}/users/{user_id}/roles. An empty project_id raises this ValueError before the POST assigning a role is sent.

Source

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

        extra_body: Body | None = None,
        timeout: float | httpx2.Timeout | None | NotGiven = not_given,
    ) -> 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,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass the correct non-empty project_id when assigning a role to a user
  2. Validate both project_id and user_id before the call

Example fix

// before
client.admin.organization.projects.users.roles.create(project_id="", user_id=user_id, role_id=role_id)
// after
client.admin.organization.projects.users.roles.create(project_id=project_id, user_id=user_id, role_id=role_id)
Defensive patterns

Strategy: validation

Validate before calling

if not project_id or not user_id:
    raise ValueError("project_id and user_id are required to assign a role")
client.admin.organization.projects.users.roles.create(project_id=project_id, user_id=user_id, role_id=role_id)

Type guard

def has_ids(**kwargs: object) -> bool:
    return all(isinstance(v, str) and v.strip() for v in kwargs.values())

Try / catch

try:
    roles.create(project_id, user_id, role_id)
except ValueError as e:
    logger.error("invalid role assignment args: %s", e)
    raise

Prevention

When it happens

Trigger: Calling client.admin.organization.projects.users.roles.create(project_id='', user_id='user_...', role_id='role_...') with a blank project id.

Common situations: Onboarding automation that assigns roles but reads the project id from a variable that was never populated.

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