/protean:upcasterfrom protean
Creates Protean event upcasters that transform old event payloads to match the current schema version, enabling event schema evolution without breaking stored events. Extends BaseUpcaster with an upcast(data) method and registers via @domain.upcaster().
Ships in protean. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
An upcaster transforms old event payloads to the current schema during deserialization. Define one by extending BaseUpcaster and implementing upcast():
from protean import Domain
from protean.core.upcaster import BaseUpcaster
from protean.fields import Decimal, Identifier, String
domain = Domain()
@domain.event(part_of="Order")
class OrderPlaced:
__version__ = 2
order_id = Identifier(required=True)
amount = Decimal(precision=19, scale=4, required=True)
currency = String(required=True)
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=2)
class UpcastV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["currency"] = "USD"
return data
BaseUpcaster: Import from protean.core.upcaster@domain.upcaster(event_type=..., from_version=..., to_version=...): All three options are requiredupcast(self, data: dict) -> dict: Receives the raw event payload dict, returns the transformed dictevent_type is always the CURRENT event class: Not the old version. The upcaster knows which event it targets by its current definition__version__ on the event class: Set __version__ = 2 on the event when you add an upcaster targeting v2. Default version is 1domain.init()domain.init() detects duplicates, cycles, non-convergent chains, and missing event classes. It does not catch a missing step: a stored payload whose version has no upcaster path is passed through unchanged and fails to deserialize at read time. check reports that gap as UPCASTER_GAP@apply), event handlers (@handle), and projectors all receive upcast eventsBasic structure shows adding a required field with a default value.
@domain.upcaster(event_type=OrderPlaced, from_version=2, to_version=3)
class UpcastV2ToV3(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["total_amount"] = data.pop("amount")
return data
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=2)
class UpcastV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data.pop("legacy_code", None)
return data
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=2)
class UpcastV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["line_item_count"] = len(data.get("items", []))
return data
This example evolves a CustomerRegistered event whose v2 stores the address as one nested value:
@domain.event(part_of="Customer")
class CustomerRegistered:
__version__ = 2
customer_id = Identifier(required=True)
address = Dict()
@domain.aggregate
class Customer:
name = String()
@domain.upcaster(event_type=CustomerRegistered, from_version=1, to_version=2)
class UpcastCustomerRegisteredV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["address"] = {
"street": data.pop("street", ""),
"city": data.pop("city", ""),
"zip_code": data.pop("zip_code", ""),
}
return data
When an event evolves through multiple versions, register one upcaster per step. The framework chains them automatically:
@domain.event(part_of="Order")
class OrderPlaced:
__version__ = 3
order_id = Identifier(required=True)
total_amount = Decimal(precision=19, scale=4, required=True)
currency = String(required=True)
# v1 → v2: add currency
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=2)
class UpcastV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["currency"] = "USD"
return data
# v2 → v3: rename amount → total_amount
@domain.upcaster(event_type=OrderPlaced, from_version=2, to_version=3)
class UpcastV2ToV3(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["total_amount"] = data.pop("amount")
return data
A stored v1 event passes through both: v1→v2→v3. A stored v2 event passes through only v2→v3. A v3 event skips upcasting entirely.
Upcasting is especially valuable for ES aggregates because every reconstruction replays all events. With upcasters, @apply handlers only handle the current schema:
@domain.aggregate(event_sourced=True)
class Order:
order_id = Identifier(identifier=True)
total_amount = Decimal(precision=19, scale=4)
currency = String()
@apply
def on_placed(self, event: OrderPlaced):
# Always receives current v3 schema: upcasters handle old versions
self.order_id = event.order_id
self.total_amount = event.total_amount
self.currency = event.currency
Upcasting also applies to asynchronous event processing. Old events are upcast before reaching @handle. The handler below belongs to the Order aggregate, whose stream carries OrderPlaced, and records revenue through a helper:
def record_revenue(amount, currency):
print(f"Revenue: {amount} {currency}")
@domain.event_handler(part_of=Order)
class OrderRevenueHandler:
@handle(OrderPlaced)
def on_order_placed(self, event: OrderPlaced):
# Always receives current schema, even for historical replays
record_revenue(event.total_amount, event.currency)
# fragment
# WRONG: if v2 existed in production, you need v1→v2 AND v2→v3
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=3)
class SkipV2(BaseUpcaster): ...
Instead: register one upcaster per step, as in Multi-step chains. A stored version with no upcaster path is what check reports as UPCASTER_GAP (Key rule 9).
# fragment
# WRONG: upcasting runs on the deserialization path and must stay fast
class SlowUpcaster(BaseUpcaster):
def upcast(self, data: dict) -> dict:
customer = db.query(Customer, data["customer_id"]) # NO! No I/O
data["currency"] = customer.currency
return data
Instead: transform the dict only, with defaults for values the old payload lacks.
# fragment
# WRONG: event_type must be the CURRENT event class
@domain.upcaster(event_type=OrderPlacedV1, from_version=1, to_version=2)
Instead: point event_type at the current class (Key rule 4).
# fragment
# WRONG: event still at default v1, but upcaster targets v2
@domain.event(part_of="Order")
class OrderShipped:
# __version__ not set: defaults to `1`
order_id = Identifier(required=True)
carrier = String(required=True)
@domain.upcaster(event_type=OrderShipped, from_version=1, to_version=2)
class UpcastOrderShippedV1ToV2(BaseUpcaster): ...
Instead: set __version__ on the event to the chain's final version, here __version__ = 2.
See Anti-patterns for more.
event: Event definition, __version__, naming conventionsevent-sourced-aggregate: @apply decorator, from_events(), ES repositoryevent-handler: @handle decorator, event handler structureprojector: @handle/@on decorator, projector structurecheck and resolve what it reportsassets/upcaster_basic.py (opens in a new tab)assets/upcaster_multi_step_chain.py (opens in a new tab)assets/upcaster_with_es_aggregate.py (opens in a new tab)assets/upcaster_with_event_handler.py (opens in a new tab)references/anti-patterns.md (opens in a new tab)references/upcaster-chain.md (opens in a new tab)references/when-to-upcast.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub proteanhq/protean --plugin protean39 skills in this plugin
/add-commandAdd a complete command flow to an existing aggregate. It creates a command class, a FastAPI API endpoint, and a command handler with repository logic. This is the primary workflow for adding write operations to a Protean domain. Use when the user wants to "add a command", "add an action", "add an operation", "expose a write endpoint", "create a command flow", "wire up a POST endpoint", "add a new operation to an aggregate", or describes an action like "I want users to be able to [verb] a [thing]" (e.g., "place an order", "register a user", "cancel a reservation"). This workflow composes the command, command-handler, and api-endpoint element skills./add-domain-service-flowBuild a domain service flow in Protean for business logic that spans two or more aggregates. Domain services are stateless, encapsulate cross-aggregate business rules, and run pre/post invariants for validation. They are used when business logic doesn't naturally belong to a single aggregate. Wire them into command handlers or application services for persistence. Use when the user asks to "add a domain service", "implement cross-aggregate logic", "coordinate multiple aggregates", "add a business operation spanning aggregates", "validate across aggregates", or when they describe scenarios like "placing an order checks inventory and reserves stock", "transferring funds between accounts", "scheduling involves both resource and calendar".Other plugins' skills for the same job