-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
Hi,
I encountered a serialization error when using the A2A server and client stack. When a task status update or similar object containing a TaskState enum is serialized (e.g., for logging or output), the following error occurs:
TypeError: Object of type TaskState is not JSON serializable
Details:
- The error occurs when code calls
json.dumps(task.model_dump(by_alias=True), indent=2)(or similar) on a Pydantic model that includes aTaskStateenum. - This happens both in the server and the CLI, depending on where the serialization is triggered.
- The root cause is that the standard
jsonmodule does not know how to serialize Python Enums, and Pydantic’smodel_dump()does not convert Enums to their.valueunlessmode=\"json\"is used (Pydantic v2), or unless a customdefaultis provided tojson.dumps.
Workaround:
I patched my local code to use:
json.dumps(task.model_dump(by_alias=True), indent=2, default=lambda o: o.value if isinstance(o, Enum) else str(o))or, for Pydantic v2:
json.dumps(task.model_dump(by_alias=True, mode=\"json\"), indent=2)But this is not consistent across the codebase.
Suggestion:
- Ensure all serialization of Pydantic models containing Enums uses either:
.model_dump(mode=\"json\")(Pydantic v2)- or a custom
defaultfunction injson.dumps
- Optionally, set
use_enum_values=Truein the Pydantic model config for affected models.
References:
- The error is triggered by models like
TaskStatus,TaskStatusUpdateEvent, etc., which useTaskStateenums.
Thank you for your work on this project!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels