tessl install tessl/pypi-pydantic@2.11.0Data validation using Python type hints
Agent Success
Agent success rate when using this tile
90%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.3x
Baseline
Agent success rate without this tile
69%
Build a data transformation system that converts between internal application data models and external API formats. The system should handle field name conversions automatically during serialization while maintaining internal consistency.
You're building an internal application that integrates with an external API. The external API uses camelCase naming for its JSON fields, while your application uses snake_case naming internally. You need to create data models that:
Create a module api_transformer.py that defines the following:
A User model with these internal fields:
user_id (integer)full_name (string)email_address (string)account_status (string)An Address model with these internal fields:
street_name (string)city_name (string)zip_code (string)A UserProfile model that:
user field (User model)primary_address field (Address model)registration_date field (string)registration_date field should serialize as "regDate" (not "registrationDate")When serializing any of these models to dictionaries, the output should automatically use camelCase field names (except for the custom "regDate" field).
from api_transformer import User, Address, UserProfile
# Create models using internal snake_case names
user = User(
user_id=101,
full_name="Alice Smith",
email_address="alice@example.com",
account_status="active"
)
address = Address(
street_name="123 Main St",
city_name="Springfield",
zip_code="12345"
)
profile = UserProfile(
user=user,
primary_address=address,
registration_date="2024-01-15"
)
# Serialize to external API format
api_data = profile.to_api_format()
print(api_data)Expected output structure:
{
"user": {
"userId": 101,
"fullName": "Alice Smith",
"emailAddress": "alice@example.com",
"accountStatus": "active"
},
"primaryAddress": {
"streetName": "123 Main St",
"cityName": "Springfield",
"zipCode": "12345"
},
"regDate": "2024-01-15"
}Create test file test_api_transformer.py:
from api_transformer import User
def test_user_serialization():
user = User(
user_id=42,
full_name="Bob Jones",
email_address="bob@test.com",
account_status="pending"
)
result = user.to_api_format()
assert result == {
"userId": 42,
"fullName": "Bob Jones",
"emailAddress": "bob@test.com",
"accountStatus": "pending"
}from api_transformer import Address
def test_address_serialization():
address = Address(
street_name="456 Oak Ave",
city_name="Portland",
zip_code="97201"
)
result = address.to_api_format()
assert result == {
"streetName": "456 Oak Ave",
"cityName": "Portland",
"zipCode": "97201"
}from api_transformer import UserProfile, User, Address
def test_user_profile_custom_alias():
user = User(user_id=1, full_name="Test User",
email_address="test@example.com", account_status="active")
address = Address(street_name="Main St", city_name="Boston", zip_code="02101")
profile = UserProfile(user=user, primary_address=address, registration_date="2024-06-01")
result = profile.to_api_format()
assert "regDate" in result
assert result["regDate"] == "2024-06-01"
assert "registrationDate" not in resultfrom api_transformer import UserProfile, User, Address
def test_nested_serialization():
user = User(user_id=99, full_name="Jane Doe",
email_address="jane@example.com", account_status="active")
address = Address(street_name="Elm St", city_name="Seattle", zip_code="98101")
profile = UserProfile(user=user, primary_address=address, registration_date="2023-12-01")
result = profile.to_api_format()
assert result["user"]["userId"] == 99
assert result["primaryAddress"]["cityName"] == "Seattle"
assert result["regDate"] == "2023-12-01"Provides data validation and serialization support.