Skip to content

Commit

Permalink
fix(python/sdk): Fix correct settings (#7098)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: b15aa8f053b040d42de832e9e2710e6697f12139
  • Loading branch information
ploeber committed Nov 7, 2024
1 parent 5439bb7 commit df4fec1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion assemblyai/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.35.0"
__version__ = "0.35.1"
23 changes: 16 additions & 7 deletions assemblyai/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

try:
# pydantic v2 import
from pydantic import UUID4, BaseModel, Field
from pydantic import UUID4, BaseModel, ConfigDict, Field
from pydantic_settings import BaseSettings, SettingsConfigDict

pydantic_v2 = True
except ImportError:
# pydantic v1 import
from pydantic.v1 import UUID4, BaseModel, BaseSettings, Field
from pydantic.v1 import ConfigDict as SettingsConfigDict
from pydantic.v1 import UUID4, BaseModel, BaseSettings, ConfigDict, Field

pydantic_v2 = False

from typing_extensions import Self

Expand Down Expand Up @@ -80,9 +83,15 @@ class Settings(BaseSettings):
base_url: str = "https://api.assemblyai.com"
"The base URL for the AssemblyAI API"

polling_interval: float = Field(default=3.0, gte=0.1)
polling_interval: float = Field(default=3.0, gt=0.0)
"The default polling interval for long-running requests (e.g. polling the `Transcript`'s status)"
model_config = SettingsConfigDict(env_prefix="assemblyai_")

if pydantic_v2:
model_config = SettingsConfigDict(env_prefix="assemblyai_")
else:

class Config:
env_prefix = "assemblyai_"


class TranscriptStatus(str, Enum):
Expand Down Expand Up @@ -571,7 +580,7 @@ class RawTranscriptionConfig(BaseModel):
"""
The speech model to use for the transcription.
"""
model_config = SettingsConfigDict(extra="allow")
model_config = ConfigDict(extra="allow")


class TranscriptionConfig:
Expand Down Expand Up @@ -1796,7 +1805,7 @@ class ListTranscriptParameters(BaseModel):

throttled_only: Optional[bool] = None
"Get only throttled transcripts, overrides the status filter"
model_config = SettingsConfigDict(use_enum_values=True)
model_config = ConfigDict(use_enum_values=True)


class PageDetails(BaseModel):
Expand Down
75 changes: 75 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
from importlib import reload

import assemblyai as aai


def test_api_key_settings():
"""
Tests that `ASSEMBLYAI_API_KEY` works correctly
"""
tmp1 = os.environ.pop("ASSEMBLYAI_API_KEY", None)
tmp2 = os.environ.pop("API_KEY", None)

aai.settings.api_key = None
reload(aai)
assert aai.settings.api_key is None

# this should not change the api key
os.environ["API_KEY"] = "test"
reload(aai)
assert aai.settings.api_key is None

# this should change the api key
os.environ["ASSEMBLYAI_API_KEY"] = "test"
reload(aai)
assert aai.settings.api_key == "test"

# reset
if tmp1:
os.environ["ASSEMBLYAI_API_KEY"] = tmp1
else:
os.environ.pop("ASSEMBLYAI_API_KEY", None)

if tmp2:
os.environ["API_KEY"] = tmp2
else:
os.environ.pop("API_KEY", None)

reload(aai)
aai.settings.api_key = "test"


def test_base_url_settings():
"""
Tests that `ASSEMBLY_BASE_URL` works correctly
"""
tmp1 = os.environ.pop("ASSEMBLYAI_BASE_URL", None)
tmp2 = os.environ.pop("BASE_URL", None)

aai.settings.base_url = "https://api.assemblyai.com"
reload(aai)
assert aai.settings.base_url == "https://api.assemblyai.com"

# this should not change the base url
os.environ["BASE_URL"] = "https://test.com"
reload(aai)
assert aai.settings.base_url == "https://api.assemblyai.com"

# this should change the base url
os.environ["ASSEMBLYAI_BASE_URL"] = "https://test.com"
reload(aai)
assert aai.settings.base_url == "https://test.com"

# reset
if tmp1:
os.environ["ASSEMBLYAI_BASE_URL"] = tmp1
else:
os.environ.pop("ASSEMBLYAI_BASE_URL", None)
if tmp2:
os.environ["BASE_URL"] = tmp2
else:
os.environ.pop("BASE_URL", None)

reload(aai)
aai.settings.api_key = "test"

0 comments on commit df4fec1

Please sign in to comment.