Skip to content

Migrate from pydantic.v1 to native pydantic v2 (fixes Python 3.14 import error) - #264

Open
manu-j3400 wants to merge 1 commit into
peopledatalabs:mainfrom
manu-j3400:fix/pydantic-v2-native
Open

manu-j3400 wants to merge 1 commit into
peopledatalabs:mainfrom
manu-j3400:fix/pydantic-v2-native

Conversation

@manu-j3400

Copy link
Copy Markdown

Description of the change

Fixes #255. The SDK imported pydantic.v1 throughout. On Python 3.14 with pydantic 2.12.x, importing the package fails with ConfigError: unable to infer type for attribute "params", because pydantic's v1 compatibility layer doesn't support 3.14. This migrates the SDK to the native pydantic v2 API.

  • Import from pydantic instead of pydantic.v1 (src and tests)
  • root_validator(pre=True) -> model_validator(mode="before"), with a guard for non-dict input
  • validator(pre=True) -> field_validator(mode="before")
  • validate_arguments -> validate_call, constr(regex=) -> constr(pattern=), .dict() -> .model_dump()
  • Optional fields get explicit None defaults (v2 treats a bare Optional as required)
  • HttpUrl fields use a validated str type, since v2's HttpUrl is no longer a str and broke the base_path/URL concatenation
  • Shared PDLBaseModel with coerce_numbers_to_str=True, so numeric inputs like a postal_code keep coercing to str as they did under v1
  • Require pydantic >= 2.6 (for coerce_numbers_to_str); poetry.lock content hash updated with Poetry 1.8.5

Testing

  • Reproduced the Lacks python 3.14+ support (pydantic failure) #255 import error on main with Python 3.14 + pydantic 2.12.5; it imports and runs on this branch.
  • Test suite results match main on Python 3.12 (pydantic 2.6.0), 3.13 and 3.14: 41 passed. The remaining tests call the live API and need PEOPLEDATALABS_API_KEY, so they error without one on both branches.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • Chore (cleanup or minor QOL tweak that has little to no impact on functionality)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Fixes peopledatalabs#255. The SDK imported pydantic.v1 everywhere, which breaks on
Python 3.14 where the v1 compatibility layer is unsupported.

- Import from pydantic instead of pydantic.v1 (src and tests)
- root_validator(pre=True) -> model_validator(mode="before"), with a
  guard for non-dict input
- validator(pre=True) -> field_validator(mode="before")
- validate_arguments -> validate_call, constr(regex=) -> constr(pattern=)
- .dict() -> .model_dump()
- Optional fields get explicit None defaults (v2 treats bare Optional as
  required)
- HttpUrl fields use a validated str type, since v2 HttpUrl is no longer
  a str and broke base_path/url concatenation
- Shared PDLBaseModel with coerce_numbers_to_str=True to keep v1's
  number-to-str coercion (e.g. numeric postal_code)
- Require pydantic >= 2.6 for coerce_numbers_to_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Lacks python 3.14+ support (pydantic failure)

1 participant