I'm still not convinced that it's a necessary approach, but here's a really simple option how to achieve this:
from pydantic import BaseModel
class MyModel(BaseModel):
x: int
def __init__(self, x: int | str):
super().__init__(x=len(x) if isinstance(x, str) else x)
MyModel(x='test')