Skip to content

Commit 317b471

Browse files
(chore): remove pydantic aliases (elevenlabs#364)
1 parent a9e6d06 commit 317b471

File tree

10 files changed

+149
-44
lines changed

10 files changed

+149
-44
lines changed

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "elevenlabs"
3-
version = "1.8.2"
3+
version = "1.9.0"
44
description = ""
55
readme = "README.md"
66
authors = []

Diff for: src/elevenlabs/core/client_wrapper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_headers(self) -> typing.Dict[str, str]:
1616
headers: typing.Dict[str, str] = {
1717
"X-Fern-Language": "Python",
1818
"X-Fern-SDK-Name": "elevenlabs",
19-
"X-Fern-SDK-Version": "1.8.2",
19+
"X-Fern-SDK-Version": "1.9.0",
2020
}
2121
if self._api_key is not None:
2222
headers["xi-api-key"] = self._api_key

Diff for: src/elevenlabs/core/pydantic_utilities.py

+42-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pydantic
1111

1212
from .datetime_utils import serialize_datetime
13+
from .serialization import convert_and_respect_annotation_metadata
1314

1415
IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
1516

@@ -56,11 +57,12 @@
5657

5758

5859
def parse_obj_as(type_: typing.Type[T], object_: typing.Any) -> T:
60+
dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read")
5961
if IS_PYDANTIC_V2:
6062
adapter = pydantic.TypeAdapter(type_) # type: ignore # Pydantic v2
61-
return adapter.validate_python(object_)
63+
return adapter.validate_python(dealiased_object)
6264
else:
63-
return pydantic.parse_obj_as(type_, object_)
65+
return pydantic.parse_obj_as(type_, dealiased_object)
6466

6567

6668
def to_jsonable_with_fallback(
@@ -75,13 +77,40 @@ def to_jsonable_with_fallback(
7577

7678

7779
class UniversalBaseModel(pydantic.BaseModel):
78-
class Config:
79-
populate_by_name = True
80-
smart_union = True
81-
allow_population_by_field_name = True
82-
json_encoders = {dt.datetime: serialize_datetime}
83-
# Allow fields begining with `model_` to be used in the model
84-
protected_namespaces = ()
80+
if IS_PYDANTIC_V2:
81+
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
82+
# Allow fields begining with `model_` to be used in the model
83+
protected_namespaces=(),
84+
) # type: ignore # Pydantic v2
85+
86+
@pydantic.model_serializer(mode="wrap", when_used="json") # type: ignore # Pydantic v2
87+
def serialize_model(self, handler: pydantic.SerializerFunctionWrapHandler) -> typing.Any: # type: ignore # Pydantic v2
88+
serialized = handler(self)
89+
data = {k: serialize_datetime(v) if isinstance(v, dt.datetime) else v for k, v in serialized.items()}
90+
return data
91+
92+
else:
93+
94+
class Config:
95+
smart_union = True
96+
json_encoders = {dt.datetime: serialize_datetime}
97+
98+
@classmethod
99+
def model_construct(
100+
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
101+
) -> "Model":
102+
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
103+
return cls.construct(_fields_set, **dealiased_object)
104+
105+
@classmethod
106+
def construct(
107+
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
108+
) -> "Model":
109+
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
110+
if IS_PYDANTIC_V2:
111+
return super().model_construct(_fields_set, **dealiased_object) # type: ignore # Pydantic v2
112+
else:
113+
return super().construct(_fields_set, **dealiased_object)
85114

86115
def json(self, **kwargs: typing.Any) -> str:
87116
kwargs_with_defaults: typing.Any = {
@@ -117,7 +146,7 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
117146
"exclude_none": True,
118147
"exclude_unset": False,
119148
}
120-
return deep_union_pydantic_dicts(
149+
dict_dump = deep_union_pydantic_dicts(
121150
super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore # Pydantic v2
122151
super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore # Pydantic v2
123152
)
@@ -143,7 +172,9 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
143172
**kwargs,
144173
}
145174

146-
return super().dict(**kwargs_with_defaults_exclude_unset_include_fields)
175+
dict_dump = super().dict(**kwargs_with_defaults_exclude_unset_include_fields)
176+
177+
return convert_and_respect_annotation_metadata(object_=dict_dump, annotation=self.__class__, direction="write")
147178

148179

149180
def deep_union_pydantic_dicts(

Diff for: src/elevenlabs/projects/client.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ..types.edit_project_response_model import EditProjectResponseModel
1717
from ..types.project_snapshots_response import ProjectSnapshotsResponse
1818
from ..types.pronunciation_dictionary_version_locator import PronunciationDictionaryVersionLocator
19+
from ..core.serialization import convert_and_respect_annotation_metadata
1920
from ..core.client_wrapper import AsyncClientWrapper
2021

2122
# this is used as the default value for optional parameters
@@ -729,7 +730,11 @@ def update_pronunciation_dictionaries(
729730
f"v1/projects/{jsonable_encoder(project_id)}/update-pronunciation-dictionaries",
730731
method="POST",
731732
json={
732-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
733+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
734+
object_=pronunciation_dictionary_locators,
735+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
736+
direction="write",
737+
),
733738
},
734739
request_options=request_options,
735740
omit=OMIT,
@@ -1546,7 +1551,11 @@ async def main() -> None:
15461551
f"v1/projects/{jsonable_encoder(project_id)}/update-pronunciation-dictionaries",
15471552
method="POST",
15481553
json={
1549-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
1554+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
1555+
object_=pronunciation_dictionary_locators,
1556+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
1557+
direction="write",
1558+
),
15501559
},
15511560
request_options=request_options,
15521561
omit=OMIT,

Diff for: src/elevenlabs/pronunciation_dictionary/client.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .types.pronunciation_dictionary_rule import PronunciationDictionaryRule
1717
from ..types.add_pronunciation_dictionary_rules_response_model import AddPronunciationDictionaryRulesResponseModel
1818
from ..core.jsonable_encoder import jsonable_encoder
19+
from ..core.serialization import convert_and_respect_annotation_metadata
1920
from ..types.remove_pronunciation_dictionary_rules_response_model import RemovePronunciationDictionaryRulesResponseModel
2021
from ..types.get_pronunciation_dictionary_metadata_response import GetPronunciationDictionaryMetadataResponse
2122
from ..types.get_pronunciation_dictionaries_metadata_response_model import (
@@ -167,7 +168,9 @@ def add_rules_to_the_pronunciation_dictionary(
167168
f"v1/pronunciation-dictionaries/{jsonable_encoder(pronunciation_dictionary_id)}/add-rules",
168169
method="POST",
169170
json={
170-
"rules": rules,
171+
"rules": convert_and_respect_annotation_metadata(
172+
object_=rules, annotation=typing.Sequence[PronunciationDictionaryRule], direction="write"
173+
),
171174
},
172175
request_options=request_options,
173176
omit=OMIT,
@@ -610,7 +613,9 @@ async def main() -> None:
610613
f"v1/pronunciation-dictionaries/{jsonable_encoder(pronunciation_dictionary_id)}/add-rules",
611614
method="POST",
612615
json={
613-
"rules": rules,
616+
"rules": convert_and_respect_annotation_metadata(
617+
object_=rules, annotation=typing.Sequence[PronunciationDictionaryRule], direction="write"
618+
),
614619
},
615620
request_options=request_options,
616621
omit=OMIT,

Diff for: src/elevenlabs/text_to_speech/client.py

+65-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..types.pronunciation_dictionary_version_locator import PronunciationDictionaryVersionLocator
99
from ..core.request_options import RequestOptions
1010
from ..core.jsonable_encoder import jsonable_encoder
11+
from ..core.serialization import convert_and_respect_annotation_metadata
1112
from ..errors.unprocessable_entity_error import UnprocessableEntityError
1213
from ..types.http_validation_error import HttpValidationError
1314
from ..core.unchecked_base_model import construct_type
@@ -130,8 +131,14 @@ def convert(
130131
"text": text,
131132
"model_id": model_id,
132133
"language_code": language_code,
133-
"voice_settings": voice_settings,
134-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
134+
"voice_settings": convert_and_respect_annotation_metadata(
135+
object_=voice_settings, annotation=VoiceSettings, direction="write"
136+
),
137+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
138+
object_=pronunciation_dictionary_locators,
139+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
140+
direction="write",
141+
),
135142
"seed": seed,
136143
"previous_text": previous_text,
137144
"next_text": next_text,
@@ -262,8 +269,14 @@ def convert_with_timestamps(
262269
"text": text,
263270
"model_id": model_id,
264271
"language_code": language_code,
265-
"voice_settings": voice_settings,
266-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
272+
"voice_settings": convert_and_respect_annotation_metadata(
273+
object_=voice_settings, annotation=VoiceSettings, direction="write"
274+
),
275+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
276+
object_=pronunciation_dictionary_locators,
277+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
278+
direction="write",
279+
),
267280
"seed": seed,
268281
"previous_text": previous_text,
269282
"next_text": next_text,
@@ -404,8 +417,14 @@ def convert_as_stream(
404417
"text": text,
405418
"model_id": model_id,
406419
"language_code": language_code,
407-
"voice_settings": voice_settings,
408-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
420+
"voice_settings": convert_and_respect_annotation_metadata(
421+
object_=voice_settings, annotation=VoiceSettings, direction="write"
422+
),
423+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
424+
object_=pronunciation_dictionary_locators,
425+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
426+
direction="write",
427+
),
409428
"seed": seed,
410429
"previous_text": previous_text,
411430
"next_text": next_text,
@@ -535,8 +554,14 @@ def stream_with_timestamps(
535554
"text": text,
536555
"model_id": model_id,
537556
"language_code": language_code,
538-
"voice_settings": voice_settings,
539-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
557+
"voice_settings": convert_and_respect_annotation_metadata(
558+
object_=voice_settings, annotation=VoiceSettings, direction="write"
559+
),
560+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
561+
object_=pronunciation_dictionary_locators,
562+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
563+
direction="write",
564+
),
540565
"seed": seed,
541566
"previous_text": previous_text,
542567
"next_text": next_text,
@@ -684,8 +709,14 @@ async def main() -> None:
684709
"text": text,
685710
"model_id": model_id,
686711
"language_code": language_code,
687-
"voice_settings": voice_settings,
688-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
712+
"voice_settings": convert_and_respect_annotation_metadata(
713+
object_=voice_settings, annotation=VoiceSettings, direction="write"
714+
),
715+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
716+
object_=pronunciation_dictionary_locators,
717+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
718+
direction="write",
719+
),
689720
"seed": seed,
690721
"previous_text": previous_text,
691722
"next_text": next_text,
@@ -824,8 +855,14 @@ async def main() -> None:
824855
"text": text,
825856
"model_id": model_id,
826857
"language_code": language_code,
827-
"voice_settings": voice_settings,
828-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
858+
"voice_settings": convert_and_respect_annotation_metadata(
859+
object_=voice_settings, annotation=VoiceSettings, direction="write"
860+
),
861+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
862+
object_=pronunciation_dictionary_locators,
863+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
864+
direction="write",
865+
),
829866
"seed": seed,
830867
"previous_text": previous_text,
831868
"next_text": next_text,
@@ -974,8 +1011,14 @@ async def main() -> None:
9741011
"text": text,
9751012
"model_id": model_id,
9761013
"language_code": language_code,
977-
"voice_settings": voice_settings,
978-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
1014+
"voice_settings": convert_and_respect_annotation_metadata(
1015+
object_=voice_settings, annotation=VoiceSettings, direction="write"
1016+
),
1017+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
1018+
object_=pronunciation_dictionary_locators,
1019+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
1020+
direction="write",
1021+
),
9791022
"seed": seed,
9801023
"previous_text": previous_text,
9811024
"next_text": next_text,
@@ -1113,8 +1156,14 @@ async def main() -> None:
11131156
"text": text,
11141157
"model_id": model_id,
11151158
"language_code": language_code,
1116-
"voice_settings": voice_settings,
1117-
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
1159+
"voice_settings": convert_and_respect_annotation_metadata(
1160+
object_=voice_settings, annotation=VoiceSettings, direction="write"
1161+
),
1162+
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
1163+
object_=pronunciation_dictionary_locators,
1164+
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
1165+
direction="write",
1166+
),
11181167
"seed": seed,
11191168
"previous_text": previous_text,
11201169
"next_text": next_text,

Diff for: src/elevenlabs/types/audio_output.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from ..core.unchecked_base_model import UncheckedBaseModel
44
import typing
55
import pydantic
6+
import typing_extensions
7+
from ..core.serialization import FieldMetadata
68
from .normalized_alignment import NormalizedAlignment
79
from ..core.pydantic_utilities import IS_PYDANTIC_V2
810

@@ -14,14 +16,16 @@ class AudioOutput(UncheckedBaseModel):
1416
is MP3 encoded as a base64 string.
1517
"""
1618

17-
is_final: typing.Optional[bool] = pydantic.Field(alias="isFinal", default=None)
19+
is_final: typing_extensions.Annotated[typing.Optional[bool], FieldMetadata(alias="isFinal")] = pydantic.Field(
20+
default=None
21+
)
1822
"""
1923
Indicates if the generation is complete. If set to `True`, `audio` will be null.
2024
"""
2125

22-
normalized_alignment: typing.Optional[NormalizedAlignment] = pydantic.Field(
23-
alias="normalizedAlignment", default=None
24-
)
26+
normalized_alignment: typing_extensions.Annotated[
27+
typing.Optional[NormalizedAlignment], FieldMetadata(alias="normalizedAlignment")
28+
] = None
2529

2630
if IS_PYDANTIC_V2:
2731
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2

Diff for: src/elevenlabs/types/initialize_connection.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import pydantic
66
from .realtime_voice_settings import RealtimeVoiceSettings
77
from .generation_config import GenerationConfig
8+
import typing_extensions
9+
from ..core.serialization import FieldMetadata
810
from ..core.pydantic_utilities import IS_PYDANTIC_V2
911

1012

@@ -20,7 +22,7 @@ class InitializeConnection(UncheckedBaseModel):
2022
This property should only be provided in the first message you send.
2123
"""
2224

23-
xi_api_key: str = pydantic.Field(alias="xi-api-key")
25+
xi_api_key: typing_extensions.Annotated[str, FieldMetadata(alias="xi-api-key")] = pydantic.Field()
2426
"""
2527
Your ElevenLabs API key. This is a required parameter that should be provided in the first message you send.
2628
You can find your API key in the [API Keys section](https://elevenlabs.io/docs/api-reference/websockets#api-keys).

Diff for: src/elevenlabs/types/library_voice_response.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# This file was auto-generated by Fern from our API Definition.
22

33
from ..core.unchecked_base_model import UncheckedBaseModel
4-
import pydantic
4+
import typing_extensions
5+
from ..core.serialization import FieldMetadata
56
import typing
67
from ..core.pydantic_utilities import IS_PYDANTIC_V2
8+
import pydantic
79

810

911
class LibraryVoiceResponse(UncheckedBaseModel):
@@ -20,9 +22,11 @@ class LibraryVoiceResponse(UncheckedBaseModel):
2022
language: str
2123
description: str
2224
preview_url: str
23-
usage_character_count_1_y: int = pydantic.Field(alias="usage_character_count_1y")
24-
usage_character_count_7_d: int = pydantic.Field(alias="usage_character_count_7d")
25-
play_api_usage_character_count_1_y: int = pydantic.Field(alias="play_api_usage_character_count_1y")
25+
usage_character_count_1_y: typing_extensions.Annotated[int, FieldMetadata(alias="usage_character_count_1y")]
26+
usage_character_count_7_d: typing_extensions.Annotated[int, FieldMetadata(alias="usage_character_count_7d")]
27+
play_api_usage_character_count_1_y: typing_extensions.Annotated[
28+
int, FieldMetadata(alias="play_api_usage_character_count_1y")
29+
]
2630
cloned_by_count: int
2731
rate: float
2832
free_users_allowed: bool

0 commit comments

Comments
 (0)