Skip to main content

langwatch-ksuid

A modern, zero-dependency Python library for generating prefixed, k-sorted globally unique identifiers (KSUIDs).

PyPI Version Build Status Python Zero Dependencies

Features

  • ✅ Zero Dependencies - No external dependencies
  • ✅ Type Hints - Full type annotation support
  • ✅ K-Sortable - IDs are naturally sortable by creation time
  • ✅ Environment Aware - Support for different environments (prod, dev, staging, etc.)
  • ✅ Instance Detection - Automatic detection of Docker containers, MAC addresses, and PIDs

Installation

pip install langwatch-ksuid
# or
uv add langwatch-ksuid

Quick Start

Basic Usage

from langwatch_ksuid import generate, parse

# Generate a KSUID
id = generate('user')
print(id)
# Output: user_00028U9MDT583X9eXPG1IU0ptdl1l

# Parse a KSUID
parsed = parse('user_00028U9MDT583X9eXPG1IU0ptdl1l')
print(parsed.resource)  # 'user'
print(parsed.environment)  # 'prod'
print(parsed.date)  # datetime object

Environment-Specific IDs

from langwatch_ksuid import generate, set_environment

# Set environment
set_environment('dev')

# Generate dev-specific KSUID
dev_ksuid = generate('order')
print(dev_ksuid)
# Output: dev_order_00028U9MDT583X9eXPG1IU0ptdl1l

Advanced Usage

from langwatch_ksuid import Ksuid, Instance, InstanceScheme
import time

# Create a custom instance
custom_instance = Instance(
    InstanceScheme.RANDOM,
    bytes([1, 2, 3, 4, 5, 6, 7, 8])
)

# Create KSUID manually
id = Ksuid(
    'prod',
    'product',
    int(time.time()),
    custom_instance,
    0
)

# Access KSUID properties
print(ksuid.timestamp)  # Unix timestamp
print(ksuid.sequence_id)  # Sequence number
print(ksuid.instance.scheme)  # Instance scheme
print(ksuid.to_json())  # Full JSON representation

API Reference

Main Functions

generate(resource: str) -> Ksuid

Generates a new KSUID for the specified resource.

ksuid = generate('user')

parse(input: str) -> Ksuid

Parses a KSUID string and returns a Ksuid instance.

ksuid = parse('user_00028U9MDT583X9eXPG1IU0ptdl1l')

Environment Management

get_environment() -> str

Returns the current environment.

set_environment(value: str) -> None

Sets the current environment.

set_environment('dev')
env = get_environment()  # 'dev'

Instance Management

get_instance() -> Instance

Returns the current instance.

set_instance(value: Instance) -> None

Sets the current instance.

instance = Instance(InstanceScheme.RANDOM, bytes(8))
set_instance(instance)

Classes

Ksuid

The main KSUID class.

Properties:

  • environment: str - The environment (prod, dev, etc.)
  • resource: str - The resource type (user, order, etc.)
  • timestamp: int - Unix timestamp
  • instance: Instance - The instance identifier
  • sequence_id: int - Sequence number
  • date: datetime - Python datetime object

Methods:

  • __str__() -> str - Returns the string representation
  • equals(other: Ksuid) -> bool - Compares two KSUIDs
  • to_json() -> dict - Returns JSON representation

Instance

Represents a machine/container instance.

Schemes:

  • InstanceScheme.RANDOM - Random identifier
  • InstanceScheme.MAC_AND_PID - MAC address + PID
  • InstanceScheme.DOCKER_CONT - Docker container ID

Node

Advanced class for custom KSUID generation.

Error Classes

  • ValidationError - Thrown for validation failures
  • Base62Error - Thrown for base62 encoding/decoding errors

Platform Support

This library works in the following environments:

  • Python 3.8+ - Full support with automatic instance detection
  • Windows - Uses MAC address + PID detection
  • macOS - Uses MAC address + PID detection
  • Linux - Uses Docker container ID or MAC address + PID detection

Instance Detection

The library automatically detects the best instance identifier:

  1. Docker Containers - Uses container ID (if available)
  2. Linux/macOS/Windows - Uses MAC address + PID (if available)
  3. Fallback - Uses cryptographically secure random bytes

Development

Setup

git clone https://github.com/langwatch/ksuid.git
cd ksuid/python
uv sync

Scripts

uv run build      # Build the library
uv run test       # Run tests
uv run test:coverage # Run tests with coverage
uv run lint       # Run linting
uv run type-check # Type checking

Testing

The library has comprehensive test coverage:

uv run test

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Acknowledgments

This library is inspired by the original KSUID specification and the @cuvva/ksuid implementation.

Metadata

Release files for langwatch-ksuid 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langwatch-ksuid 1.0.1
File Size Uploaded Provenance Actions
langwatch_ksuid-1.0.1.tar.gz 18.0 kB Without provenance Details for langwatch_ksuid-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langwatch-ksuid 1.0.1
File Interpreter ABI Platform Actions
langwatch_ksuid-1.0.1-py3-none-any.whl Python 3 none any Details for langwatch_ksuid-1.0.1-py3-none-any.whl

Total release size: 34.4 kB

Release files / langwatch_ksuid-1.0.1.tar.gz

Download URL langwatch_ksuid-1.0.1.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
14bdbee7103681ded139be1a94c1b7a5dd3b06437ec1a9c2908d6a4b21a6e809
BLAKE2b-256 checksum
How to use checksums
8f31c70ee10ecc5f6be1498b0e90d77620095b9f12ee6c3bdd7b10a761921d8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / langwatch_ksuid-1.0.1-py3-none-any.whl

Download URL langwatch_ksuid-1.0.1-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc7ffb6b2aaa501169ff2c9e4c16b415e0bb1e80276df3a4dfe8dcc0b9de3a92
BLAKE2b-256 checksum
How to use checksums
cc854f624fb3f4e922d1a5b401048074dc1880e6608eeca503d102d3028ba0f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for langwatch-ksuid 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page