Skip to content

Fix JSON serialization of team state by handling datetime objects in message dump #6797

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 11, 2025

Problem

When saving team state to JSON, users encountered a serialization error due to datetime objects in messages:

# This was failing with "Object of type datetime is not JSON serializable"
team_state = await team.save_state()
with open(state_file, "w") as f:
    json.dump(team_state, f)  # TypeError: Object of type datetime is not JSON serializable

The issue occurred because BaseChatMessage and BaseAgentEvent classes both have a created_at field of type datetime, which is not JSON serializable by default.

Solution

Changed the BaseMessage.dump() method to use Pydantic's JSON-aware serialization mode:

# Before
return self.model_dump()

# After  
return self.model_dump(mode="json")

This leverages Pydantic's built-in capability to automatically convert datetime objects to ISO 8601 format strings during serialization.

Benefits

  • Minimal change: Only 1 line changed + documentation update
  • Surgical fix: Targets the exact source of the problem
  • Backward compatible: All existing functionality works unchanged
  • Standards compliant: Uses ISO 8601 datetime format
  • Round-trip safe: Load/dump operations preserve data correctly

Testing

Verified the fix with comprehensive tests covering:

  • Original user scenario now works without errors
  • All message types serialize/deserialize correctly
  • Team state saving/loading works end-to-end
  • Backward compatibility maintained for existing APIs
  • Round-trip load/dump operations preserve all data

Example of working code after the fix:

# Create team and run task
team = Swarm([project_agent], termination_condition=termination)
result = await team.run(task="What is a Montague?")

# Save state to JSON - now works without errors!
team_state = await team.save_state()
with open(state_file, "w") as f:
    json.dump(team_state, f)  # ✅ Success!

# Load state back
with open(state_file, "r") as f:
    loaded_state = json.load(f)

Fixes #6793.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] JSON dumping of save state on teams fails because of datateimes Fix JSON serialization of team state by handling datetime objects in message dump Jul 11, 2025
@Copilot Copilot AI requested a review from ekzhu July 11, 2025 11:40
Copilot finished work on behalf of ekzhu July 11, 2025 11:40
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

Successfully merging this pull request may close these issues.

JSON dumping of save state on teams fails because of datateimes
2 participants