Skip to content

send_message/asend_message store Agent objects in memory content causing JSON serialization errors #156

@Harsh-617

Description

@Harsh-617

Describe the bug

LLMAgent.send_message and LLMAgent.asend_message store sender and recipients
as Python Agent objects inside the memory content dict. Because Agent objects are not
JSON serializable, this silently corrupts memory content and causes a hard crash whenever
recorded events are saved to JSON (e.g. via SimulationRecorder).

The built-in speak_to tool already stores unique_id values instead of objects, so
the two messaging mechanisms are currently inconsistent.

To Reproduce

# Crash via SimulationRecorder
@record_model
class MyModel(mesa.Model):
    ...

model = MyModel()
agent_a.send_message("hello", [agent_b])
model.save_recording()          # TypeError: Object of type LLMAgent is not JSON serializable

# Or directly:
import json
agent_a.send_message("hello", [agent_b])
json.dumps(agent_a.memory.step_content)  # same error

Expected behavior

Message memory entries should store serializable identifiers:

content={
    "message": message,
    "sender": self.unique_id,
    "recipients": [r.unique_id for r in recipients],
}

This is already what speak_to does (llm_agent.py lines 213–218) and what
serialization-safe memory handling requires.

Current behavior

Both send_message (line 284) and asend_message (line 269) in llm_agent.py store:

content={
    "message": message,
    "sender": self,           # Agent object — not serializable
    "recipients": recipients, # list of Agent objects — not serializable
}

Additional context

Affects: llm_agent.py lines 269–273 (asend_message) and 284–288 (send_message)
The bug is silent until JSON serialization is attempted, making it hard to catch without recording enabled
speak_to (inbuilt_tools.py lines 213–218) handles this correctly and should be the reference implementation

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