Skip to content

Commit 98095de

Browse files
committed
implementation and unit tests
1 parent 1200c83 commit 98095de

File tree

2 files changed

+408
-11
lines changed

2 files changed

+408
-11
lines changed

deepgram/clients/agent/v1/websocket/options.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,52 @@ class Agent(BaseResponse):
273273
or (isinstance(f, Think) and not f)
274274
),
275275
)
276-
speak: Speak = field(
277-
default_factory=Speak,
278-
metadata=dataclass_config(
279-
exclude=lambda f: f is None
280-
or (isinstance(f, dict) and not f)
281-
or (isinstance(f, Speak) and not f)
282-
),
283-
)
276+
speak: Union[Speak, List[Speak]] = field(default_factory=Speak)
284277
greeting: Optional[str] = field(
285278
default=None, metadata=dataclass_config(exclude=lambda f: f is None)
286279
)
287280

281+
def __post_init__(self):
282+
"""Handle conversion of dict/list data to proper Speak objects"""
283+
# Handle listen conversion (existing pattern)
284+
if (
285+
not isinstance(self.listen, Listen)
286+
and self.listen is not None
287+
and not (isinstance(self.listen, dict) and not self.listen)
288+
):
289+
self.listen = Listen.from_dict(self.listen)
290+
291+
# Handle think conversion (existing pattern)
292+
if (
293+
not isinstance(self.think, Think)
294+
and self.think is not None
295+
and not (isinstance(self.think, dict) and not self.think)
296+
):
297+
self.think = Think.from_dict(self.think)
298+
299+
# Handle speak conversion (new OneOf pattern)
300+
if isinstance(self.speak, list):
301+
# Convert list of dicts to list of Speak objects
302+
self.speak = [
303+
Speak.from_dict(item) if isinstance(item, dict) else item
304+
for item in self.speak
305+
]
306+
elif isinstance(self.speak, dict):
307+
# Convert single dict to Speak object
308+
self.speak = Speak.from_dict(self.speak)
309+
# If it's already a Speak object or None, leave it as is
310+
288311
def __getitem__(self, key):
289312
_dict = self.to_dict()
290313
if "listen" in _dict and isinstance(_dict["listen"], dict):
291314
_dict["listen"] = Listen.from_dict(_dict["listen"])
292315
if "think" in _dict and isinstance(_dict["think"], dict):
293316
_dict["think"] = Think.from_dict(_dict["think"])
294-
if "speak" in _dict and isinstance(_dict["speak"], dict):
295-
_dict["speak"] = Speak.from_dict(_dict["speak"])
317+
if "speak" in _dict:
318+
if isinstance(_dict["speak"], list):
319+
_dict["speak"] = [Speak.from_dict(item) for item in _dict["speak"]]
320+
elif isinstance(_dict["speak"], dict):
321+
_dict["speak"] = Speak.from_dict(_dict["speak"])
296322
return _dict[key]
297323

298324

@@ -395,7 +421,19 @@ class UpdateSpeakOptions(BaseResponse):
395421
"""
396422

397423
type: str = str(AgentWebSocketEvents.UpdateSpeak)
398-
speak: Speak = field(default_factory=Speak)
424+
speak: Union[Speak, List[Speak]] = field(default_factory=Speak)
425+
426+
def __post_init__(self):
427+
"""Handle conversion of dict/list data to proper Speak objects"""
428+
if isinstance(self.speak, list):
429+
# Convert list of dicts to list of Speak objects
430+
self.speak = [
431+
Speak.from_dict(item) if isinstance(item, dict) else item
432+
for item in self.speak
433+
]
434+
elif isinstance(self.speak, dict):
435+
# Convert single dict to Speak object
436+
self.speak = Speak.from_dict(self.speak)
399437

400438

401439
# InjectAgentMessage

0 commit comments

Comments
 (0)