HumanSignal/label-studio · error · KeyError

Can't find Organization by welcome URL: {url}

Error message

Can't find Organization by welcome URL: {url}

What it means

Organization.find_by_invite_url parses the last path segment of a welcome/invite URL as a token and raises KeyError when the token is empty. Note it does NOT catch DoesNotExist — an invalid non-empty token still raises Organization.DoesNotExist from .get(); this KeyError only fires for URLs whose stripped value has no trailing segment.

Source

Thrown at label_studio/organizations/models.py:143

    @classmethod
    def find_by_user(cls, user, check_deleted=False):
        memberships = OrganizationMember.objects.filter(user=user).prefetch_related('organization')
        if not memberships.exists():
            raise ValueError(f'No memberships found for user {user}')
        membership = memberships.first()
        if check_deleted:
            return (membership.organization, True) if membership.deleted_at else (membership.organization, False)

        return membership.organization

    @classmethod
    def find_by_invite_url(cls, url):
        token = url.strip('/').split('/')[-1]
        if len(token):
            return Organization.objects.get(token=token)
        else:
            raise KeyError(f"Can't find Organization by welcome URL: {url}")

    def has_user(self, user):
        return self.users.filter(pk=user.pk).exists()

    def has_deleted(self, user):
        return OrganizationMember.objects.filter(user=user, organization=self, deleted_at__isnull=False).exists()

    def has_project_member(self, user):
        return self.projects.filter(members__user=user).exists()

    def has_permission(self, user):
        return OrganizationMember.objects.filter(user=user, organization=self, deleted_at__isnull=True).exists()

    def add_user(self, user):
        if _workforce_closure_blocked(user):
            # A closed identity must never gain a NEW membership anywhere — this is the one
            # chokepoint every membership creation goes through (invites, SCIM, SAML, LDAP, admin).
            logger.warning(

View on GitHub (pinned to 0b49e9b539)

Solutions

  1. Inspect the URL — it must end with a non-empty token segment
  2. Re-generate the invite link from the organization settings page
  3. Check the org's token field is populated in the DB (Organization.token)
  4. Verify the env/config that injects the token into the welcome URL

Example fix

// before
org = Organization.find_by_invite_url(invite_url)  # invite_url may be ''
// after
if not invite_url or not invite_url.strip('/').split('/')[-1]:
    raise ImproperlyConfigured('Invite URL missing token')
org = Organization.find_by_invite_url(invite_url)
Defensive patterns

Strategy: validation

Validate before calling

token = url.strip('/').split('/')[-1] if url else ''
if not token:
    raise ImproperlyConfigured('invite URL has no token segment')

Try / catch

try:
    org = Organization.find_by_invite_url(url)
except KeyError:
    ...  # empty token — fix URL config
except Organization.DoesNotExist:
    ...  # token not recognized — link expired/regenerated

Prevention

When it happens

Trigger: Calling Organization.find_by_invite_url with an empty string, '', a URL like 'https://host/invite' with nothing after the last slash, or a URL that reduces to no token after strip('/').split('/').

Common situations: Misconfigured WELCOME_URL / invite link template missing the token; env var or org setting blank; copying the invite base URL without the token; trailing formatting stripping the token client-side.

Related errors


AI-assisted analysis of HumanSignal/label-studio@0b49e9b539 (2026-08-29). Data as JSON: /api/errors/6f3b0715d73be30c. Report an issue: GitHub.