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 a TaskState enum.
- This happens both in the server and the CLI, depending on where the serialization is triggered.
- The root cause is that the standard
json module does not know how to serialize Python Enums, and Pydantic’s model_dump() does not convert Enums to their .value unless mode=\"json\" is used (Pydantic v2), or unless a custom default is provided to json.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
default function in json.dumps
- Optionally, set
use_enum_values=True in the Pydantic model config for affected models.
References:
- The error is triggered by models like
TaskStatus, TaskStatusUpdateEvent, etc., which use TaskState enums.
Thank you for your work on this project!
Hi,
I encountered a serialization error when using the A2A server and client stack. When a task status update or similar object containing a
TaskStateenum is serialized (e.g., for logging or output), the following error occurs:Details:
json.dumps(task.model_dump(by_alias=True), indent=2)(or similar) on a Pydantic model that includes aTaskStateenum.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:
or, for Pydantic v2:
But this is not consistent across the codebase.
Suggestion:
.model_dump(mode=\"json\")(Pydantic v2)defaultfunction injson.dumpsuse_enum_values=Truein the Pydantic model config for affected models.References:
TaskStatus,TaskStatusUpdateEvent, etc., which useTaskStateenums.Thank you for your work on this project!