https://dagster.io/ logo
#ask-ai
Title
# ask-ai
s

Shane Keller

03/10/2024, 4:04 AM
Implement a custom dagster type that would support serializing uuids in configs and inputs and outputs
s

Scout

03/10/2024, 4:04 AM
To implement a custom Dagster type that supports serializing UUIDs in configs, inputs, and outputs, you can follow these steps: 1. Define a custom Dagster type loader that can parse UUIDs from config. 2. Use the
@usable_as_dagster_type
decorator to make your custom type usable as a Dagster type. 3. Implement serialization and deserialization logic for the UUID type. Here's an example based on the documentation:
Copy code
python from uuid import UUID from dagster import DagsterTypeLoader, DagsterTypeMaterializer, TypeCheck, usable_as_dagster_type, dagster_type_loader, dagster_type_materializer  # Define a custom type loader for UUIDs @dagster_type_loader(UUID) def uuid_loader(_context, value):     if isinstance(value, str):         try:             return UUID(value)         except ValueError:             raise TypeCheck(f"Value {value} is not a valid UUID string.")     raise TypeCheck(f"Expected a string, but got {type(value)}")  # Define a custom type materializer for UUIDs @dagster_type_materializer(UUID) def uuid_materializer(_context, _config, value):     if isinstance(value, UUID):         return str(value)     raise TypeCheck(f"Expected a UUID, but got {type(value)}")  # Use the custom loader and materializer with the UUID type @usable_as_dagster_type(loader=uuid_loader, materializer=uuid_materializer, name='UUIDType') class UUIDDagsterType(UUID):     pass  # Now you can use UUIDDagsterType in your assets, ops, or resources
In this example,
uuid_loader
is a function that takes a config value and attempts to parse it into a
UUID
object. If the value is not a valid UUID string, it raises a
TypeCheck
error. The
uuid_materializer
function takes a
UUID
object and serializes it to a string. The
@usable_as_dagster_type
decorator is used to attach the custom loader and materializer to the
UUID
type, making it usable in Dagster as
UUIDType
. You can now use
UUIDDagsterType
in your assets, ops, or resources to handle UUIDs in configs, inputs, and outputs. Please note that this example is a conceptual illustration based on the documentation's guidance on creating custom Dagster types with loaders and materializers. You may need to adjust the code to fit your specific use case and ensure that it works with your Dagster version and setup.
4 Views