RetellAI / retell-python-sdk

Apache License 2.0
38 stars 5 forks source link

KnowledgeBaseResponse.to_json() and .to_dict() crash with PydanticSerializationError #213

Open tbsvttr opened 6 days ago

tbsvttr commented 6 days ago

Example:

for knowledge_base_response in client.knowledge_base.list():
    backup_filename = f'./backups/knowledge_bases/{knowledge_base_response.knowledge_base_id}_{
        knowledge_base_response.knowledge_base_name}_{timestamp}.json'
    print(knowledge_base_response)
    with open(backup_filename, 'w') as backup_file:
        backup_file.write(knowledge_base_response.to_json())

Crashes with:

Traceback (most recent call last):
  File "/Users/username/Code/customername/projectname/retellai/./backup.py", line 36, in <module>
    backup_file.write(knowledge_base_response.to_json())
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Code/customername/projectname/.venv/lib/python3.12/site-packages/retell/_models.py", line 161, in to_json
    return self.model_dump_json(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Code/customername/projectname/.venv/lib/python3.12/site-packages/pydantic/main.py", line 352, in model_dump_json
    return self.__pydantic_serializer__.to_json(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: TypeError: 'MockValSer' object cannot be converted to 'SchemaSerializer'

Identical problem happens when using to_dict().

Note that it works with several other very similar implementations, but for other data:

for agent_response in client.agent.list():
    backup_filename = f'./backups/agents/{agent_response.agent_id}_{
        agent_response.agent_name}_{timestamp}.json'
    with open(backup_filename, 'w') as backup_file:
        backup_file.write(agent_response.to_json())

for llm_response in client.llm.list():
    backup_filename = f'./backups/llms/{llm_response.llm_id}_{timestamp}.json'
    with open(backup_filename, 'w') as backup_file:
        backup_file.write(agent_response.to_json())

for phone_number_response in client.phone_number.list():
    backup_filename = f'./backups/phone_numbers/{phone_number_response.phone_number}_{
        phone_number_response.nickname}_{timestamp}.json'
    with open(backup_filename, 'w') as backup_file:
        backup_file.write(phone_number_response.to_json())

It seems to be a known problem with Pydantic: https://github.com/pydantic/pydantic/issues/7713

Tried to downgrade Pydantic to 2.4.0. But still had the same error. Same with these versions: 2.10.2, 2.9.2, 2.8.2, 2.7.4, 2.6.4, and 2.4.2.

Using macOS 15.1.1 with zsh, asdf, etc. Python and venv as visible in paths of traceback.

Does this work and is tested on your end? Any idea how to solve this?

JustLime commented 1 day ago

@tbsvttr @weijia-yu @AustinZzx @RobertCraigie @toddlzt

Got the same problem here.

Does anyone know how to fix this?