deepgram / deepgram-python-sdk

Official Python SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
218 stars 58 forks source link

AttributeError: 'list' object has no attribute 'items' #294

Closed leddy231 closed 7 months ago

leddy231 commented 7 months ago

What is the current behavior?

There seams to be something wrong with the __getitem__ method on the response objects, it tries it iterate over lists as if they are dicts.

Steps to reproduce

from deepgram import PrerecordedOptions, DeepgramClient, UrlSource

client = DeepgramClient()
transcribe = client.listen.prerecorded.v("1")
options = PrerecordedOptions(
    language="en-us",
    model="nova-2",
    tag=["data-automation", "test"],
)
response = transcribe.transcribe_url(
    source=UrlSource(url="https://ia803406.us.archive.org/1/items/HelloWorld/test_64kb.mp3"),
    options=options,
)

Using this response dataclass we can get the transcript:

print(response.results.channels[0].alternatives[0].transcript)

Which works, but if we use the legacy v2 syntax at any point in the chain

print(response.results.channels[0].alternatives[0]["transcript"])

We get a crash

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[10], line 3
      1 print(response.results.channels[0].alternatives[0].transcript)
----> 3 print(response.results.channels[0].alternatives[0]["transcript"])

File ~/.../.venv/lib/python3.10/site-packages/deepgram/clients/prerecorded/v1/response.py:351, in Alternative.__getitem__(self, key)
    348 _dict = self.to_dict()
    349 if _dict["words"] is not None:
    350     _dict["words"] = [
--> 351         Word.from_dict(words) for _, words in _dict["words"].items()
    352     ]
    353 if _dict["summaries"] is not None:
    354     _dict["summaries"] = [
    355         SummaryV1.from_dict(summaries)
    356         for _, summaries in _dict["summaries"].items()
    357     ]

AttributeError: 'list' object has no attribute 'items'

This crash happens on ["alternatives"], ["channels"] and so on

Expected behavior

Not crashing

Please tell us about your environment

python 3.10.4 linux deepgram-sdk 3.1.3

Other information

We are migrating a lot of code from v2 to v3 so we still use a lot of the legacy syntax. Upgrading to the new syntax is unfortunately not easy as everything is now Optional, which is a big pain. Inlcuding word on the Word dataclass, how that could ever be None im not sure.

dvonthenen commented 7 months ago

Will take a look. The intent is that the legacy syntax to access properties on results should work.

dvonthenen commented 7 months ago

Merged PR. Will be available in the next release.