Skip to content

Enum values in TaskStatus/TaskState cause "not JSON serializable" error #1

@TheWhiteWord

Description

@TheWhiteWord

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions