Skip to main content

Overview

User scoping in Morphik allows multi-tenant applications to isolate data on a per-user basis. This ensures that in applications serving multiple users, each user can only access their own documents and data. User scoping is particularly valuable for building customer-facing applications where data privacy and separation are essential.

Creating User Scopes

Operations Within a User Scope

Once you have a user scope, all operations performed on it are automatically scoped to that specific user. Documents created or retrieved are only accessible to that user.

User Scope Methods

The UserScope class provides the same document operations as the main Morphik client, but automatically scoped to the specific user:
  • ingest_text - Ingest text content for this user
  • ingest_file - Ingest a file for this user
  • ingest_files - Ingest multiple files for this user
  • ingest_directory - Ingest all files from a directory for this user
  • retrieve_chunks - Retrieve chunks matching a query from this user’s documents (supports reverse image search)
  • retrieve_docs - Retrieve documents matching a query from this user’s documents
  • query - Generate a completion using context from this user’s documents (supports llm_config parameter for custom LLM configuration)
  • list_documents - List all documents owned by this user
  • batch_get_documents - Get multiple documents by their IDs for this user
  • batch_get_chunks - Get specific chunks by source for this user
  • delete_document_by_filename - Delete a document by filename for this user

Scoping to Multiple Folders

User scopes can include additional folder filters using additional_folders on retrieval/list/query helpers:

Using Custom LLM Configuration with User Scopes

You can pass a custom LLM configuration when querying within a user scope:

Example: Customer Support Application

A common use case for user scoping is in customer support applications where each customer has their own set of documents and needs personalized assistance:

Combining User Scope with Folder Scope

For advanced data organization, you can combine user scoping with folder scoping. This allows you to organize by both user and functional area:

Example: SaaS Application with Multi-Project Support

Here’s a more complex example showing how to build a SaaS application that supports multiple customers (tenants), each with their own multiple projects:

Bulk Document Processing for Multiple Users

When developing applications that serve multiple users, you might need to process documents in bulk. Here’s an example showing how to ingest documents for multiple users:
See Folder Management for more details on working with folder scopes.