Schema Evolution
Apache Fory™ supports schema evolution in compatible mode, allowing fields to be added or removed
while maintaining compatibility. Xlang mode enables compatible mode by default. In native mode,
set compatible=True explicitly when Python-only payloads need schema evolution.
Xlang Default
import pyfory
f = pyfory.Fory(xlang=True)
Disable Evolution for Stable Classes
If a dataclass schema is stable and will not change, you can disable evolution for that class to avoid compatible metadata overhead. Use pyfory.dataclass with evolving=False:
import pyfory
@pyfory.dataclass(evolving=False)
class StableMessage:
id: int
name: str
pyfory.dataclass also supports slots=True:
@pyfory.dataclass(slots=True)
class SlotMessage:
id: int
Schema Evolution Example
import pyfory
from dataclasses import dataclass
# Version 1: Original class
@dataclass
class User:
name: str
age: pyfory.Int32
f = pyfory.Fory(xlang=True)
f.register(User, typename="User")
data = f.dumps(User("Alice", 30))
# Version 2: Add new field (backward compatible)
@dataclass
class User:
name: str
age: pyfory.Int32
email: str = "unknown@example.com" # New field with default
# Can still deserialize old data
user = f.loads(data)
print(user.email) # "unknown@example.com"
Supported Changes
- Add new fields: With default values
- Remove fields: Old data with extra fields will be skipped
- Reorder fields: Fields are matched by name, not position
Best Practices
- Always provide default values for new fields
- Use typename for cross-language compatibility
- Test schema changes before deploying
- Document schema versions for your team
Related Topics
- Configuration - Compatible mode settings
- Xlang Serialization - Schema evolution across languages
- Type Registration - Registration patterns