Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make objects json serializable? #391

Open
gwpl opened this issue Oct 25, 2024 · 4 comments
Open

Make objects json serializable? #391

gwpl opened this issue Oct 25, 2024 · 4 comments

Comments

@gwpl
Copy link

gwpl commented Oct 25, 2024

Feature Description

Trying to adopt example to pretty print json

#!/usr/bin/env python3

from elevenlabs.client import ElevenLabs
import json

client = ElevenLabs(
#  api_key="YOUR_API_KEY", # Defaults to ELEVEN_API_KEY
)

response = client.voices.get_all()
audio = client.generate(text="Hello there!", voice=response.voices[0])

print(json.dumps(response.voices, indent=2))

currently trying in differnt ways and does not seem to work with json pretty printing...

Use Case

No response

Alternatives Considered

No response

Additional Context

No response

@gwpl
Copy link
Author

gwpl commented Oct 29, 2024

I also tried to make output to be JSONL (JSON Lines https://jsonlines.org/ ) and it also failed:

import json                                                  
                                                              
 for voice in response.voices:                                
     print(json.dumps(voice)) 

error:

Traceback (most recent call last):
  File "/.../elevenlabs_list_available_voices.py", line 14, in <module>
    print(json.dumps(voice))
          ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^

@dglinyanov
Copy link

Hi there, as soon as response is Pydantic BaseModel, you can pretty print using .json() method:

print(response.json(indent=2))

Also you can print each of response.voices item the same way, because they are also Pydantic models:

print(response.voices[0].json(indent=2))

Hope that helps!

@gwpl
Copy link
Author

gwpl commented Oct 29, 2024

Thanks you!

So, this works:

print(response.json(indent=2))

This does not work ("voices has no attribute json"):

print(response.voices.json(indent=2))

Iterating over voices , works:

for voice in response.voices:
    print(voice.json())

So, looks like each "voice" and "reponse" are Pydantic, but "voices" object is not Pydantic.

@dglinyanov
Copy link

Yes, that's correct. response.voices is a plain list, so it has no .json() method. I am not very familiar with both Pydantic and Fern, so I think it is possible to create Pydantic model for a list of objects, but I do not have any idea if Fern could autogenerate something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants