diff --git a/generators/python/core_utilities/shared/pydantic_utilities.py b/generators/python/core_utilities/shared/pydantic_utilities.py index 65e4a1d2b82c..ffe9d85becb9 100644 --- a/generators/python/core_utilities/shared/pydantic_utilities.py +++ b/generators/python/core_utilities/shared/pydantic_utilities.py @@ -91,6 +91,20 @@ def json(self, **kwargs: Any) -> str: return super().model_dump_json(**kwargs_with_defaults) # type: ignore[misc] return super().json(**kwargs_with_defaults) + def model_dump_json(self, **kwargs: Any) -> str: + """ + Override model_dump_json to use our custom dict() method which properly + handles FieldMetadata aliases, then serialize to JSON. + """ + import json + + data = self.dict(**kwargs) + if IS_PYDANTIC_V2: + from pydantic_core import to_jsonable_python + + return json.dumps(to_jsonable_python(data, fallback=encode_by_type)) + return json.dumps(data, default=encode_by_type) + def dict(self, **kwargs: Any) -> Dict[str, Any]: """ Override the default dict method to `exclude_unset` by default. This function patches diff --git a/generators/python/core_utilities/shared/with_pydantic_v1_on_v2/pydantic_utilities.py b/generators/python/core_utilities/shared/with_pydantic_v1_on_v2/pydantic_utilities.py index 9e9c33b4b24b..0870daa20059 100644 --- a/generators/python/core_utilities/shared/with_pydantic_v1_on_v2/pydantic_utilities.py +++ b/generators/python/core_utilities/shared/with_pydantic_v1_on_v2/pydantic_utilities.py @@ -54,6 +54,16 @@ def json(self, **kwargs: Any) -> str: } return super().json(**kwargs_with_defaults) + def model_dump_json(self, **kwargs: Any) -> str: + """ + Override model_dump_json to use our custom dict() method which properly + handles FieldMetadata aliases, then serialize to JSON. + """ + import json + + data = self.dict(**kwargs) + return json.dumps(data, default=encode_by_type) + def dict(self, **kwargs: Any) -> Dict[str, Any]: """ Override the default dict method to `exclude_unset` by default. This function patches diff --git a/generators/python/sdk/versions.yml b/generators/python/sdk/versions.yml index b9ac151817eb..f433f64be77a 100644 --- a/generators/python/sdk/versions.yml +++ b/generators/python/sdk/versions.yml @@ -1,5 +1,15 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json # For unreleased changes, use unreleased.yml +- version: 4.43.0 + changelogEntry: + - summary: | + Add `model_dump_json()` override to `UniversalBaseModel` that properly handles `FieldMetadata` aliases. + When using the default `FieldMetadata` aliasing (not native Pydantic aliases), calling `model_dump_json()` + now correctly serializes field names to their camelCase aliases, matching the behavior of `dict()`. + type: feat + createdAt: "2025-12-05" + irVersion: 61 + - version: 4.42.0 changelogEntry: - summary: |