Setting values for multi-select column using notion-py library

Working with Multi-Select Properties in Notion API

I’m building a Discord bot that creates database entries in Notion. I’m using the notion-py package to handle the API calls.

Here’s my current setup for adding new records:

def create_database_entry():
    view = client.get_collection_view(database_urls[current_type])
    new_entry = view.collection.add_row()
    new_entry.website_url = stored_url
    new_entry.entry_title = stored_title
    
    if current_type == 'articles':
        new_entry.category = stored_category
        labels = stored_labels.split(',')
        for label in labels:
            setup_multiselect_option("Labels", label)
    else:
        new_entry.section = stored_category

The issue I’m facing is with populating the Labels column which has a multi-select property type. I found a community solution and modified it:

label_options = ['technology', 'programming']

def setup_multiselect_option(column_name, option_value, color=None):
    if color is None:
        color = choice(label_options)
    
    schema = view.collection.get(["schema"])
    column_config = next(
        (config for key, config in schema.items() if config["name"] == column_name), None
    )
    
    if not column_config:
        raise ValueError(f'Column "{column_name}" not found!')
    
    if column_config["type"] != "multi_select":
        raise ValueError(f'Column "{column_name}" is not multi-select type!')
    
    existing = next(
        (item for item in column_config["options"] if item["value"] == option_value), None
    )
    
    if existing:
        raise ValueError(f'Option "{option_value}" already exists!')
    
    column_config["options"].append({
        "id": str(uuid1()), 
        "value": option_value, 
        "color": color
    })
    
    collection.set("schema", schema)

However, this throws an error: HTTPError: Unsaved transactions: Not allowed to edit column: schema

How can I properly assign values to a multi-select column in notion-py? What’s the correct approach for this?

The schema error occurs because notion-py doesn’t allow changes to the database structure on the fly. I resolved this by ensuring all possible multi-select values are pre-defined in Notion’s interface. After that, I simply assigned the labels using a list. Additionally, it’s wise to filter your labels to contain only those that match the existing schema to prevent errors when your Discord bot encounters unexpected labels. If you’re dealing with dynamic labels, consider implementing a separate tracking system or using a relation property that connects to another database where you can manage control more effectively.

This happens because you’re trying to change the database schema through code, which needs admin permissions. Don’t create new multi-select options via the API - instead, manually add all possible options in Notion’s web interface first. Once they exist, you can assign them like this: new_entry.labels = stored_labels.split(','). The notion-py library automatically matches your strings to existing options. Here’s the catch: if your label values don’t match exactly (case matters!), it’ll either fail silently or throw an error. I learned this when my bot kept breaking because I had “Bug” in code but “bug” in Notion.

no need to modify the schema. just assign values directly: new_entry.labels = ['technology', 'programming']. make sure those options already exist in your notion database, otherwise you’ll get an error.