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

RepeatedComposite is not JSON serializable error with Gemini and SingleStore Persistent Storage #1682

Open
sahill3 opened this issue Jan 2, 2025 · 2 comments

Comments

@sahill3
Copy link

sahill3 commented Jan 2, 2025

I was trying to test the SingleStore Persistent Storage but the code throws an error after the thinking process.

from phi.storage.agent.singlestore import S2AgentStorage
from dotenv import load_dotenv
from phi.model.google import Gemini
from phi.agent import Agent, RunResponse
from sqlalchemy import create_engine
import os
load_dotenv()

# SingleStore Configuration
USERNAME = os.getenv("SINGLESTORE_USERNAME")
PASSWORD = os.getenv("SINGLESTORE_PASSWORD")
HOST = os.getenv("SINGLESTORE_HOST")
PORT = os.getenv("SINGLESTORE_PORT")
DATABASE = os.getenv("SINGLESTORE_DATABASE")
SSL_CERT = os.getenv("SINGLESTORE_SSL_CERT", None)

# SingleStore DB URL
db_url = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}?charset=utf8mb4"
if SSL_CERT:
    db_url += f"&ssl_ca={SSL_CERT}&ssl_verify_cert=true"

# Create a database engine
db_engine = create_engine(db_url)

# Create a storage backend using the Singlestore database
storage = S2AgentStorage(
    # store sessions in the ai.sessions table
    table_name="agent_sessions",
    # db_engine: Singlestore database engine
    db_engine=db_engine,
    # schema: Singlestore schema
    schema=DATABASE,
)

# Add storage to the Agent
agent = Agent(
    model=Gemini(id="gemini-1.5-flash"),
    storage=storage,
    add_history_to_messages=True,
    num_history_responses=5,
    description="You are a general purpose agent.",
    markdown=True,
)

agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")
agent.print_response("Which country are we speaking about?")

Code referred from phidata Docs.

Error

▰▰▰▰▱▱▱ Thinking...
Traceback (most recent call last):
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\storage\agent\singlestore.py", line 204, in upsert
    "memory": json.dumps(session.memory, ensure_ascii=False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type RepeatedComposite is not JSON serializable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ML\Egenie\egenie-chatbot\version3\version3\test.py", line 50, in <module> 
    agent.print_response("How many people live in Canada?")
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\agent\agent.py", line 2904, in print_response
    run_response = self.run(message=message, messages=messages, stream=False, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\agent\agent.py", line 2070, in run
    return next(resp)
           ^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\agent\agent.py", line 1922, in _run
    self.write_to_storage()
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\agent\agent.py", line 749, in write_to_storage
    self._agent_session = self.storage.upsert(session=self.get_agent_session())      
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      
  File "C:\Users\petro\AppData\Local\pypoetry\Cache\virtualenvs\version3-3qoseo5I-py3.12\Lib\site-packages\phi\storage\agent\singlestore.py", line 227, in upsert
    "memory": json.dumps(session.memory, ensure_ascii=False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\petro\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", 
line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type RepeatedComposite is not JSON serializable
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1735829064.935814   16056 init.cc:229] grpc_wait_for_shutdown_with_timeout() timed out.

I would appreciate any help.

@willemcdejongh
Copy link

Hey @sahill3
Thank you for using Phidata and bringing this to our attention!
The team has been looking into the issue and we were able to reproduce your findings. The core of the issue lies in the Gemini responses and that they contain certain protobuf objects like the RepeatedComposite that are not easily accepted into mysql.
Do you at the moment have a hard requirement to use Gemini and SingleStore?
Could you consider using the postgres storage option or perhaps another model in the meantime? The team has an open ticket to address this properly within the next two weeks.

@OdinHoang03
Copy link

Hey @sahill3 Thank you for using Phidata and bringing this to our attention! The team has been looking into the issue and we were able to reproduce your findings. The core of the issue lies in the Gemini responses and that they contain certain protobuf objects like the RepeatedComposite that are not easily accepted into mysql. Do you at the moment have a hard requirement to use Gemini and SingleStore? Could you consider using the postgres storage option or perhaps another model in the meantime? The team has an open ticket to address this properly within the next two weeks.

I'm looking forward to it

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

3 participants