Troubleshooting
This page covers common issues and their solutions.
Common Issues
ImportError with Format Features
# Solution: Install Row format support
pip install pyfory[format]
# Or install from source with format support
pip install -e ".[format]"
Slow Serialization Performance
# Check if Cython acceleration is enabled
import pyfory
print(pyfory.ENABLE_FORY_CYTHON_SERIALIZATION) # Should be True
# If False, Cython extension may not be compiled correctly
# Reinstall with: pip install --force-reinstall --no-cache-dir pyfory
Cross-Language Compatibility Issues
# Use explicit type registration with consistent naming
f = pyfory.Fory(xlang=True)
f.register(MyClass, typename="com.package.MyClass") # Use same name in all languages
Circular Reference Errors or Duplicate Data
# Enable reference tracking
f = pyfory.Fory(ref=True) # Required for circular references
# Example with circular reference
class Node:
def __init__(self, value):
self.value = value
self.next = None
node1 = Node(1)
node2 = Node(2)
node1.next = node2
node2.next = node1 # Circular reference
data = f.dumps(node1)
result = f.loads(data)
assert result.next.next is result # Circular reference preserved
Schema Evolution Not Working
# Enable compatible mode for schema evolution
f = pyfory.Fory(xlang=True, compatible=True)
# Version 1: Original class
@dataclass
class User:
name: str
age: int
f.register(User, typename="User")
data = f.dumps(User("Alice", 30))
# Version 2: Add new field (backward compatible)
@dataclass
class User:
name: str
age: int
email: str = "[email protected]" # New field with default
# Can still deserialize old data
user = f.loads(data)
print(user.email) # "[email protected]"
Type Registration Errors in Strict Mode
# Register all custom types before serialization
f = pyfory.Fory(strict=True)
# Must register before use
f.register(MyClass, type_id=100)
f.register(AnotherClass, type_id=101)
# Or disable strict mode (NOT recommended for production)
f = pyfory.Fory(strict=False) # Use only in trusted environments
Debug Mode
Set environment variable BEFORE importing pyfory to disable Cython for debugging:
import os
os.environ['ENABLE_FORY_CYTHON_SERIALIZATION'] = '0'
import pyfory # Now uses pure Python implementation
# This is useful for:
# 1. Debugging protocol issues
# 2. Understanding serialization behavior
# 3. Development without recompiling Cython
Error Handling
Handle common serialization errors gracefully:
import pyfory
from pyfory.error import TypeUnregisteredError, TypeNotCompatibleError
fory = pyfory.Fory(strict=True)
try:
data = fory.dumps(my_object)
except TypeUnregisteredError as e:
print(f"Type not registered: {e}")
# Register the type and retry
fory.register(type(my_object), type_id=100)
data = fory.dumps(my_object)
except Exception as e:
print(f"Serialization failed: {e}")
try:
obj = fory.loads(data)
except TypeNotCompatibleError as e:
print(f"Schema mismatch: {e}")
# Handle version mismatch
except Exception as e:
print(f"Deserialization failed: {e}")
Development Setup
git clone https://github.com/apache/fory.git
cd fory/python
# Install dependencies
pip install -e ".[dev,format]"
# Run tests
pytest -v -s .
# Run specific test
pytest -v -s pyfory/tests/test_serializer.py
# Format code
ruff format .
ruff check --fix .
Related Topics
- Configuration - Fory parameters
- Type Registration - Registration best practices
- Security - Security configuration