Users are the core entity in RecallrAI. Each user represents an individual whose context, memories, and conversations you want to track. Every session, memory, and merge conflict is associated with a specific user.
Create a new user to start tracking their context and memories.
Copy
from recallrai import RecallrAIclient = RecallrAI( api_key="rai_your_api_key", project_id="your_project_id")# Create a user with just an IDuser = client.create_user("alice_123")# Create a user with metadatauser = client.create_user( user_id="bob_456", metadata={ "name": "Bob Smith", "email": "bob@example.com", "plan": "premium", "signup_date": "2024-01-15" })print(f"Created user: {user.user_id}")print(f"Metadata: {user.metadata}")
# List users with paginationusers = client.list_users( offset=0, limit=20)print(f"Total users: {users.total}")print(f"Retrieved: {len(users.users)} users")for user in users.users: print(f"- {user.user_id} (active: {user.last_active_at})")# Get next pageif users.has_more(): next_users = client.list_users( offset=users.next_offset(), limit=20 )
Refresh a user object to get the latest data from the server.
Copy
# Refresh to get latest datauser.refresh()print(f"Refreshed at: {user.last_active_at}")print(f"Current metadata: {user.metadata}")
Use refresh() when you have a User object but want to ensure it has the latest data from the server (e.g., after another process might have modified it).
# Migrate user ID (e.g., from temporary to permanent ID)def migrate_user_id(user, new_id): """Migrate user to a new ID.""" old_id = user.user_id user.update(new_user_id=new_id) print(f"Migrated user from {old_id} to {new_id}") return user# Usagetemp_user = client.get_user("temp_guest_123")permanent_user = migrate_user_id(temp_user, "registered_user_456")
Raised when trying to access a user that doesn’t exist.
Copy
from recallrai.exceptions import UserNotFoundErrortry: user = client.get_user("nonexistent_user")except UserNotFoundError as e: print(f"User not found: {e.message}") # Handle appropriately (e.g., create user, show error)
UserAlreadyExistsError (409)
Raised when trying to create a user with an ID that already exists, or when updating to an existing user ID.
Copy
from recallrai.exceptions import UserAlreadyExistsErrortry: user = client.create_user("alice_123")except UserAlreadyExistsError as e: print(f"User already exists: {e.message}") # Handle appropriately (get existing user instead) user = client.get_user("alice_123")
ValidationError (422)
Raised when provided data doesn’t meet validation requirements.
Copy
from recallrai.exceptions import ValidationErrortry: # Invalid user_id format or invalid metadata user = client.create_user("") # Empty user_idexcept ValidationError as e: print(f"Invalid data: {e.message}")
Use meaningful user IDs: Use stable identifiers from your system (database IDs, auth provider IDs) rather than sequential numbers or temporary values.
Store relevant metadata: Include information that might be useful for personalization (preferences, plan type, demographics) but avoid storing sensitive data.
Handle not found errors: Always handle UserNotFoundError gracefully, especially in chat applications where users might be accessing stale links.
Don’t store PII unnecessarily: Only store metadata that’s needed for your use case. Avoid storing sensitive information like passwords, SSNs, or payment details.