Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 8, 2025

Problem

When running the app_team.py or app_agent.py examples from the agentchat_fastapi sample directory with AutoGen 0.7.5, users encountered a JSON serialization error:

Error: Object of type datetime is not JSON serializable

This error occurred when the application attempted to send messages via WebSocket or save conversation history to JSON files.

Root Cause

The sample applications were using model_dump() to serialize message objects before passing them to json.dumps() or websocket.send_json(). The model_dump() method returns datetime fields (like created_at) as Python datetime objects, which are not JSON serializable by default.

Example of the problematic code:

message = TextMessage(source="assistant", content="Hello!")
data = message.model_dump()  # created_at is still a datetime object
json.dumps(data)  # ❌ TypeError: Object of type datetime is not JSON serializable

Solution

Replace model_dump() with dump() method calls. The dump() method uses model_dump(mode="json") internally, which automatically converts datetime objects to ISO 8601 format strings.

Example of the fix:

message = TextMessage(source="assistant", content="Hello!")
data = message.dump()  # created_at is now "2025-10-08T04:58:48.686759Z"
json.dumps(data)  # ✅ Success!

Changes

Files modified:

  • python/samples/agentchat_fastapi/app_team.py - Lines 124, 127
  • python/samples/agentchat_fastapi/app_agent.py - Lines 91, 92

The changes are minimal (4 lines total) and use the recommended serialization method from the BaseMessage class, which is already tested in test_datetime_serialization_in_messages.

Testing

  • ✅ Verified existing unit test test_datetime_serialization_in_messages passes
  • ✅ Confirmed the fix resolves the exact error reported in the issue
  • ✅ Tested round-trip serialization (dump → load) preserves datetime values
  • ✅ Validated with comprehensive tests covering multiple message types
  • ✅ All code formatting and linting checks pass

This issue did not occur in AutoGen 0.5.7 because the serialization behavior changed in later versions. This fix ensures the samples work correctly with AutoGen 0.7.5 and later.

Original prompt

This section details on the original issue you should resolve

<issue_title>Error: Object of type datetime is not JSON serializable</issue_title>
<issue_description>### What happened?

Describe the bug

When running the example app_team.py from agentchat_fastapi directory using autogen 0.7.5, an error occurs right after sending a message from the user.
The webpage displays:

error
Error: Object of type datetime is not JSON serializable

To Reproduce

uv pip install -U "autogen-agentchat" "autogen-ext"
python app_team.py

Expected behavior

The user message should be sent successfully, and the agents should respond as expected without any serialization errors.

Screenshots

Image

Additional context

This issue does not occur in autogen 0.5.7.

Which packages was the bug in?

Python AgentChat (autogen-agentchat>=0.4.0)

AutoGen library version.

Python 0.7.5

Other library version.

No response

Model used

No response

Model provider

None

Other model provider

No response

Python version

None

.NET version

None

Operating system

None</issue_description>

<agent_instructions>Ensure datetime type is serialized correctly in the sample. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #7073

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Fix serialization error for datetime objects Fix datetime JSON serialization error in agentchat_fastapi samples Oct 8, 2025
@Copilot Copilot AI requested a review from ekzhu October 8, 2025 05:03
Copilot finished work on behalf of ekzhu October 8, 2025 05:03
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.

Error: Object of type datetime is not JSON serializable

2 participants