Skip to content

Method Mismatch in Function Serializatio #8

Open

Description

@Hdpbilly

Method Mismatch in Function Serialization

Issue Description

There is a method name mismatch between the Function class implementation and the SDK's serialization expectations. The SDK attempts to call to_dict() on Function objects, but the Function class only implements toJson().

Current Behavior

When attempting to simulate or deploy custom functions, the following error occurs:

Traceback (most recent call last):
  File "...\agent_test_suite.py", line 189, in <module>
    response = agent.simulate_twitter(session_id="research-session")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\virtuals_sdk\game.py", line 214, in simulate_twitter
    return self.game_sdk.simulate(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\virtuals_sdk\sdk.py", line 41, in simulate
    "customFunctions": [x.to_dict() for x in custom_functions]
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\virtuals_sdk\sdk.py", line 41, in <listcomp>
    "customFunctions": [x.to_dict() for x in custom_functions]
                        ^^^^^^^^^
AttributeError: 'Function' object has no attribute 'to_dict'

Expected Behavior

The SDK should either:

  1. Use toJson() instead of to_dict() in sdk.py, or
  2. The Function class should implement both methods for compatibility

Code Analysis

In game.py

@dataclass
class Function:
    # ...
    def toJson(self):
        return {
            "id": self.id,
            "fn_name": self.fn_name,
            "fn_description": self.fn_description,
            "args": [asdict(arg) for arg in self.args],
            "hint": self.hint,
            "config": asdict(self.config)
        }

In sdk.py

def simulate(self, session_id: str, goal: str, description: str, world_info: str, functions: list, custom_functions: list):
    # ...
    "customFunctions": [x.to_dict() for x in custom_functions]  # This line causes the error

Current Workaround

Users can implement a wrapper class to provide the missing method:

class CustomFunction(game.Function):
    def to_dict(self):
        return self.toJson()

Suggested Fix

Option 1 (Preferred):

# In sdk.py, change:
"customFunctions": [x.toJson() for x in custom_functions]

Option 2:

# In game.py, add to Function class:
def to_dict(self):
    return self.toJson()

Impact

This issue affects any users trying to implement custom functions with the SDK, particularly when using the simulation or deployment features.

Additional Notes

  • The issue appears to be a simple naming inconsistency rather than a functional problem
  • The toJson() method works correctly when called directly
  • The fix should be backward compatible regardless of which option is chosen

Labels

  • bug
  • documentation
  • enhancement

Activity

  1. ai-virtual-b commented on Dec 24, 2024

    @ai-virtual-b
    Contributor

    Thanks @Hdpbilly for the great description and for the catch! Do you mind making a PR with your fix and I'll have look at it?

  2. elTigre9 commented on Jan 5, 2025

    @elTigre9

    With the latest build, I ran into a similar problem. In deploy (within sdk.py), when adding the json object to the call, the customFunctions property is just the parameter custom_functions. Which causes a serialization error. This is not a problem in react or simulate, but does not happen in deploy.

    The line indeploy should be changed to "customFunctions": [x.toJson() for x in custom_functions] as is in both react and simulate.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions