Skip to content

Commit

Permalink
(chore): remove pydantic aliases (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] authored Sep 18, 2024
1 parent a9e6d06 commit 317b471
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "elevenlabs"
version = "1.8.2"
version = "1.9.0"
description = ""
readme = "README.md"
authors = []
Expand Down
2 changes: 1 addition & 1 deletion src/elevenlabs/core/client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_headers(self) -> typing.Dict[str, str]:
headers: typing.Dict[str, str] = {
"X-Fern-Language": "Python",
"X-Fern-SDK-Name": "elevenlabs",
"X-Fern-SDK-Version": "1.8.2",
"X-Fern-SDK-Version": "1.9.0",
}
if self._api_key is not None:
headers["xi-api-key"] = self._api_key
Expand Down
53 changes: 42 additions & 11 deletions src/elevenlabs/core/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pydantic

from .datetime_utils import serialize_datetime
from .serialization import convert_and_respect_annotation_metadata

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

Expand Down Expand Up @@ -56,11 +57,12 @@


def parse_obj_as(type_: typing.Type[T], object_: typing.Any) -> T:
dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read")
if IS_PYDANTIC_V2:
adapter = pydantic.TypeAdapter(type_) # type: ignore # Pydantic v2
return adapter.validate_python(object_)
return adapter.validate_python(dealiased_object)
else:
return pydantic.parse_obj_as(type_, object_)
return pydantic.parse_obj_as(type_, dealiased_object)


def to_jsonable_with_fallback(
Expand All @@ -75,13 +77,40 @@ def to_jsonable_with_fallback(


class UniversalBaseModel(pydantic.BaseModel):
class Config:
populate_by_name = True
smart_union = True
allow_population_by_field_name = True
json_encoders = {dt.datetime: serialize_datetime}
# Allow fields begining with `model_` to be used in the model
protected_namespaces = ()
if IS_PYDANTIC_V2:
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
# Allow fields begining with `model_` to be used in the model
protected_namespaces=(),
) # type: ignore # Pydantic v2

@pydantic.model_serializer(mode="wrap", when_used="json") # type: ignore # Pydantic v2
def serialize_model(self, handler: pydantic.SerializerFunctionWrapHandler) -> typing.Any: # type: ignore # Pydantic v2
serialized = handler(self)
data = {k: serialize_datetime(v) if isinstance(v, dt.datetime) else v for k, v in serialized.items()}
return data

else:

class Config:
smart_union = True
json_encoders = {dt.datetime: serialize_datetime}

@classmethod
def model_construct(
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
) -> "Model":
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
return cls.construct(_fields_set, **dealiased_object)

@classmethod
def construct(
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
) -> "Model":
dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read")
if IS_PYDANTIC_V2:
return super().model_construct(_fields_set, **dealiased_object) # type: ignore # Pydantic v2
else:
return super().construct(_fields_set, **dealiased_object)

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {
Expand Down Expand Up @@ -117,7 +146,7 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
"exclude_none": True,
"exclude_unset": False,
}
return deep_union_pydantic_dicts(
dict_dump = deep_union_pydantic_dicts(
super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore # Pydantic v2
super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore # Pydantic v2
)
Expand All @@ -143,7 +172,9 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
**kwargs,
}

return super().dict(**kwargs_with_defaults_exclude_unset_include_fields)
dict_dump = super().dict(**kwargs_with_defaults_exclude_unset_include_fields)

return convert_and_respect_annotation_metadata(object_=dict_dump, annotation=self.__class__, direction="write")


def deep_union_pydantic_dicts(
Expand Down
13 changes: 11 additions & 2 deletions src/elevenlabs/projects/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ..types.edit_project_response_model import EditProjectResponseModel
from ..types.project_snapshots_response import ProjectSnapshotsResponse
from ..types.pronunciation_dictionary_version_locator import PronunciationDictionaryVersionLocator
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.client_wrapper import AsyncClientWrapper

# this is used as the default value for optional parameters
Expand Down Expand Up @@ -729,7 +730,11 @@ def update_pronunciation_dictionaries(
f"v1/projects/{jsonable_encoder(project_id)}/update-pronunciation-dictionaries",
method="POST",
json={
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
},
request_options=request_options,
omit=OMIT,
Expand Down Expand Up @@ -1546,7 +1551,11 @@ async def main() -> None:
f"v1/projects/{jsonable_encoder(project_id)}/update-pronunciation-dictionaries",
method="POST",
json={
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
},
request_options=request_options,
omit=OMIT,
Expand Down
9 changes: 7 additions & 2 deletions src/elevenlabs/pronunciation_dictionary/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .types.pronunciation_dictionary_rule import PronunciationDictionaryRule
from ..types.add_pronunciation_dictionary_rules_response_model import AddPronunciationDictionaryRulesResponseModel
from ..core.jsonable_encoder import jsonable_encoder
from ..core.serialization import convert_and_respect_annotation_metadata
from ..types.remove_pronunciation_dictionary_rules_response_model import RemovePronunciationDictionaryRulesResponseModel
from ..types.get_pronunciation_dictionary_metadata_response import GetPronunciationDictionaryMetadataResponse
from ..types.get_pronunciation_dictionaries_metadata_response_model import (
Expand Down Expand Up @@ -167,7 +168,9 @@ def add_rules_to_the_pronunciation_dictionary(
f"v1/pronunciation-dictionaries/{jsonable_encoder(pronunciation_dictionary_id)}/add-rules",
method="POST",
json={
"rules": rules,
"rules": convert_and_respect_annotation_metadata(
object_=rules, annotation=typing.Sequence[PronunciationDictionaryRule], direction="write"
),
},
request_options=request_options,
omit=OMIT,
Expand Down Expand Up @@ -610,7 +613,9 @@ async def main() -> None:
f"v1/pronunciation-dictionaries/{jsonable_encoder(pronunciation_dictionary_id)}/add-rules",
method="POST",
json={
"rules": rules,
"rules": convert_and_respect_annotation_metadata(
object_=rules, annotation=typing.Sequence[PronunciationDictionaryRule], direction="write"
),
},
request_options=request_options,
omit=OMIT,
Expand Down
81 changes: 65 additions & 16 deletions src/elevenlabs/text_to_speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..types.pronunciation_dictionary_version_locator import PronunciationDictionaryVersionLocator
from ..core.request_options import RequestOptions
from ..core.jsonable_encoder import jsonable_encoder
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.http_validation_error import HttpValidationError
from ..core.unchecked_base_model import construct_type
Expand Down Expand Up @@ -130,8 +131,14 @@ def convert(
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -262,8 +269,14 @@ def convert_with_timestamps(
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -404,8 +417,14 @@ def convert_as_stream(
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -535,8 +554,14 @@ def stream_with_timestamps(
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -684,8 +709,14 @@ async def main() -> None:
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -824,8 +855,14 @@ async def main() -> None:
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -974,8 +1011,14 @@ async def main() -> None:
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down Expand Up @@ -1113,8 +1156,14 @@ async def main() -> None:
"text": text,
"model_id": model_id,
"language_code": language_code,
"voice_settings": voice_settings,
"pronunciation_dictionary_locators": pronunciation_dictionary_locators,
"voice_settings": convert_and_respect_annotation_metadata(
object_=voice_settings, annotation=VoiceSettings, direction="write"
),
"pronunciation_dictionary_locators": convert_and_respect_annotation_metadata(
object_=pronunciation_dictionary_locators,
annotation=typing.Sequence[PronunciationDictionaryVersionLocator],
direction="write",
),
"seed": seed,
"previous_text": previous_text,
"next_text": next_text,
Expand Down
12 changes: 8 additions & 4 deletions src/elevenlabs/types/audio_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ..core.unchecked_base_model import UncheckedBaseModel
import typing
import pydantic
import typing_extensions
from ..core.serialization import FieldMetadata
from .normalized_alignment import NormalizedAlignment
from ..core.pydantic_utilities import IS_PYDANTIC_V2

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

is_final: typing.Optional[bool] = pydantic.Field(alias="isFinal", default=None)
is_final: typing_extensions.Annotated[typing.Optional[bool], FieldMetadata(alias="isFinal")] = pydantic.Field(
default=None
)
"""
Indicates if the generation is complete. If set to `True`, `audio` will be null.
"""

normalized_alignment: typing.Optional[NormalizedAlignment] = pydantic.Field(
alias="normalizedAlignment", default=None
)
normalized_alignment: typing_extensions.Annotated[
typing.Optional[NormalizedAlignment], FieldMetadata(alias="normalizedAlignment")
] = None

if IS_PYDANTIC_V2:
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2
Expand Down
4 changes: 3 additions & 1 deletion src/elevenlabs/types/initialize_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pydantic
from .realtime_voice_settings import RealtimeVoiceSettings
from .generation_config import GenerationConfig
import typing_extensions
from ..core.serialization import FieldMetadata
from ..core.pydantic_utilities import IS_PYDANTIC_V2


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

xi_api_key: str = pydantic.Field(alias="xi-api-key")
xi_api_key: typing_extensions.Annotated[str, FieldMetadata(alias="xi-api-key")] = pydantic.Field()
"""
Your ElevenLabs API key. This is a required parameter that should be provided in the first message you send.
You can find your API key in the [API Keys section](https://elevenlabs.io/docs/api-reference/websockets#api-keys).
Expand Down
12 changes: 8 additions & 4 deletions src/elevenlabs/types/library_voice_response.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This file was auto-generated by Fern from our API Definition.

from ..core.unchecked_base_model import UncheckedBaseModel
import pydantic
import typing_extensions
from ..core.serialization import FieldMetadata
import typing
from ..core.pydantic_utilities import IS_PYDANTIC_V2
import pydantic


class LibraryVoiceResponse(UncheckedBaseModel):
Expand All @@ -20,9 +22,11 @@ class LibraryVoiceResponse(UncheckedBaseModel):
language: str
description: str
preview_url: str
usage_character_count_1_y: int = pydantic.Field(alias="usage_character_count_1y")
usage_character_count_7_d: int = pydantic.Field(alias="usage_character_count_7d")
play_api_usage_character_count_1_y: int = pydantic.Field(alias="play_api_usage_character_count_1y")
usage_character_count_1_y: typing_extensions.Annotated[int, FieldMetadata(alias="usage_character_count_1y")]
usage_character_count_7_d: typing_extensions.Annotated[int, FieldMetadata(alias="usage_character_count_7d")]
play_api_usage_character_count_1_y: typing_extensions.Annotated[
int, FieldMetadata(alias="play_api_usage_character_count_1y")
]
cloned_by_count: int
rate: float
free_users_allowed: bool
Expand Down
Loading

0 comments on commit 317b471

Please sign in to comment.