Skip to content

fix: add serialization-safe agent_id and to_dict() to MemoryEntry (#156)#256

Closed
aatmaj28 wants to merge 1 commit intomesa:mainfrom
aatmaj28:fix/json-serialization-156
Closed

fix: add serialization-safe agent_id and to_dict() to MemoryEntry (#156)#256
aatmaj28 wants to merge 1 commit intomesa:mainfrom
aatmaj28:fix/json-serialization-156

Conversation

@aatmaj28
Copy link
Copy Markdown

Summary
Fixes #156 — MemoryEntry stores a direct LLMAgent object reference in its agent field, which causes TypeError: Object of type LLMAgent is not JSON serializable when memory entries are serialized.

Root Cause
The MemoryEntry dataclass (in mesa_llm/memory/memory.py) has agent: "LLMAgent" as a field. Every memory module (st_memory, lt_memory, st_lt_memory, episodic_memory) creates entries with agent=self.agent, embedding the full Agent object. Any downstream code that attempts json.dumps() on a MemoryEntry or its fields fails.

Changes

  • Added agent_id: int | None = None field to MemoryEntry with post_init that auto-populates from agent.unique_id
  • Added to_dict() method that returns a JSON-serializable dictionary (content, step, agent_id)
  • Fully backwards compatible: the existing agent field is unchanged, so all 9 call sites across 4 memory modules continue to work without modification

Testing
Existing tests unaffected — change is purely additive (new field with default + new method).

Context
I'm a GSoC 2026 candidate working on the Mesa-LLM stabilization project proposal. This is one of the critical issues I identified for Phase 1.

…sa#156)

MemoryEntry stores a direct reference to LLMAgent objects, which causes
JSON serialization errors when memory entries need to be serialized
(e.g., for logging, export, or LLM context construction).

Changes:
- Added agent_id field to MemoryEntry that auto-populates from agent.unique_id
- Added to_dict() method returning a JSON-serializable representation
- Backwards compatible: agent field unchanged, agent_id has default None

Fixes mesa#156
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 21, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fa1791c6-7c7d-4f29-9ded-35a269b72fd7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use your project's `ruff` configuration to improve the quality of Python code reviews.

Add a Ruff configuration file to your project to customize how CodeRabbit runs ruff.

Copy link
Copy Markdown

@IlamaranMagesh IlamaranMagesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coul you please elaborate what's the issue here? I see the issue #156 is already closed and it has some fixes on JSON serialisation error.

@aatmaj28
Copy link
Copy Markdown
Author

Coul you please elaborate what's the issue here? I see the issue #156 is already closed and it has some fixes on JSON serialisation error.

Hi @IlamaranMagesh , thanks for the review! You're right that #156 was closed, but looking at the fix that was merged, it addressed the send_message/asend_message methods specifically.
My PR addresses a related but different aspect: the MemoryEntry dataclass itself still stores the full LLMAgent object in its agent field (line 22 of memory.py). While the content dictionaries passed to add_to_memory were fixed to use unique_id, the MemoryEntry object that wraps them still holds a non-serializable agent reference. This means any code that tries to serialize a MemoryEntry directly (e.g., for logging, export, or passing to an LLM context) will still fail.
The agent_id field and to_dict() method I added provide a clean serialization path without breaking the existing agent field that other code depends on. Happy to close this if the maintainers feel it's not needed though!

@wang-boyu
Copy link
Copy Markdown
Member

Closing because #156 was already fixed, and this PR does not address that code path. Making MemoryEntry json-serializable could be a useful enhancement, but there is no current built-in logic that requires it. If that need comes up, we can revisit it in the future. Thanks for the effort though.

@wang-boyu wang-boyu closed this Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants