ome-types

raw JSON →
0.6.3 verified Mon Apr 27 auth: no python

Python dataclasses for the OME (Open Microscopy Environment) data model. Provides type-annotated Python objects for OME-XML metadata, enabling type-safe construction and serialization of OME metadata. Current version 0.6.3, supports Python >=3.9. Maintained by tlambert03, regular releases.

pip install ome-types
error pydantic_core._pydantic_core.ValidationError: 1 validation error for OME
cause Missing required field when constructing OME or model objects.
fix
Check all required fields in the OME schema. For OME object, images is required (list of Image). For Image, id and name are required. For Pixels, id, dimension_order, size_x, size_y, size_c, size_z, size_t, type are required.
error ModuleNotFoundError: No module named 'ome_types.model.OME'
cause Trying to import OME from wrong submodule; in older versions OME was in ome_types.model, newer versions it is in ome_types directly.
fix
Use 'from ome_types import OME' (top-level import) for current versions (>=0.6.0).
error AttributeError: 'dict' object has no attribute 'to_xml'
cause Calling to_xml() on a dict instead of an OME model instance, often happens when using .model_dump() incorrectly.
fix
Ensure you have an OME object, not a dict. Use .model_dump(mode='python') to get dict representation, but to_xml() is only on the model object.
gotcha OME model fields are validated at construction; missing required fields raise pydantic ValidationError. Always check required fields (e.g., id, dimension_order, size_x, etc.) for Images and Pixels.
fix Refer to OME schema documentation for required attributes. Use pydantic's model_dump(mode='python') to debug.
deprecated In version 0.6.0, the OME class was moved from ome_types.model to ome_types directly. Importing from the old path still works but may be deprecated.
fix Use 'from ome_types import OME' instead of 'from ome_types.model import OME'.
gotcha Serializing to XML with to_xml() will produce a string with default namespaces. If you need custom namespaces or formatting, use to_xml(nsmap=...) but those are not fully documented.
fix Read the source or use the underlying pydantic XML serialization if needed.
gotcha When parsing XML with from_xml(), ensure the XML is valid OME-XML. Invalid XML raises ParseError without clear message. Use lxml's parser for better error messages.
fix Use lxml.etree.parse() to validate XML before passing to from_xml().

Create an OME object programmatically with required fields and serialize to XML.

from ome_types import OME
from ome_types.model import Image, Pixels

# Create minimal OME object
ome = OME(images=[Image(id='Image:0', name='test', pixels=Pixels(id='Pixels:0', dimension_order='XYZCT', size_x=10, size_y=10, size_c=1, size_z=1, size_t=1, type='uint8'))])
print(ome.to_xml())