Sample.save() throws exception when in props is an vocabulary property set to None

Hi, as I wrote above, when using the sample.save() call, and if in sample.props is a vocabulary attribute set as None, I am required to select one of the vocabulary terms.

Pybis version is 1.35.1

Here is my function:

def save_object(session, object_type, properties, parent_info=None, space=config.SPACE, project=config.PROJECT, experiment=config.EXPERIMENT):
    """Salva un oggetto in openBIS"""
    try:
        if object_type in config.COORDINATES.keys():
            #OVERRIDE inputs
            space=config.COORDINATES[object_type][0]
            project=config.COORDINATES[object_type][1]
            experiment=config.COORDINATES[object_type][2]

            new_sample = session.new_sample(
                type=object_type,
                space=f"/{space}",
                project=f"/{space}/{project}",
                experiment=f"/{space}/{project}/{experiment}",
                props=properties)
            
        else:
            new_sample = session.new_sample(
                type=object_type,
                space=f"/{space}",
                project=f"/{space}/{project}" if project else None,
                experiment=f"/{space}/{project}/{experiment}" if experiment else None,
                props=properties)
        
        if parent_info:
            parent_samples = session.get_samples(where={'$name': parent_info['code']['$name']})
            st.text("Parent samples")
            st.text(parent_samples)
            # we should iterate here or consider entire list
            if parent_samples:
                new_sample.parents = [parent_samples[0]]

        new_sample.save()
        return new_sample
    except Exception as e:
        st.error(f"Errore nel salvataggio: {e}")
        return None

I copy-paste the exception message:

Errore nel salvataggio: Value for attribute «sediment.msl_sediment» must be one of these terms: MSL_SEDIMENT1, MSL_SEDIMENT2, MSL_SEDIMENT3, MSL_SEDIMENT4, MSL_SEDIMENT5, MSL_SEDIMENT6, MSL_SEDIMENT7, MSL_SEDIMENT8, MSL_SEDIMENT9, MSL_SEDIMENT10, MSL_SEDIMENT11, MSL_SEDIMENT12, MSL_SEDIMENT13, MSL_SEDIMENT14, MSL_SEDIMENT15, MSL_SEDIMENT16, MSL_SEDIMENT17, MSL_SEDIMENT18, MSL_SEDIMENT19, MSL_SEDIMENT20, MSL_SEDIMENT21, MSL_SEDIMENT22

Thank you for any help you can provide!
Best
Stefano

EDIT: I formatted the message a bit better
PS: in the ELN empty vocabulary attributes are allowed

Hello,

Please update to the newest version of pybis (1.37.3). I believe it should fix your issue.

Best regards,
Adam Laskowski

1 Like

I used a workaround to create the props of the new element before creating the object:

props_template = {}
props_template.update({'$name': "template"})                        
props_template.update({'finished_flag': False})
# + other properties
# create a if-loop to check if property is None, if true --> do not create this property

a second workarround would be to delete the property from the dict if its value is None:

del props_template["finished_flag"]

Create and save new object without the property which is None:

template = o.new_object(
    type       = "some_obj_type",
    experiment = "some_exp",
    props      = props_template
)
template.save()
1 Like