Skip to content

🐛 Coerce workgraph node attributes to JSON-safe values on save#798

Open
elinscott wants to merge 1 commit into
aiidateam:mainfrom
elinscott:json-safe-workgraph-data
Open

🐛 Coerce workgraph node attributes to JSON-safe values on save#798
elinscott wants to merge 1 commit into
aiidateam:mainfrom
elinscott:json-safe-workgraph-data

Conversation

@elinscott

@elinscott elinscott commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

save_workgraph_data assigns the serialized workgraph straight onto node attributes (workgraph_data, workgraph_data_short, workgraph_error_handlers). Node attributes must be JSON-serializable, but workgraph data can carry arbitrary Python objects that are not - the reproducing case is an Enum default picked up from a task function signature:

class SpinChannel(str, Enum):
    UP = "up"
    DOWN = "down"

@task
def screen(channel: SpinChannel = SpinChannel.UP) -> float:
    ...

Storing the process node then fails deep inside the storage backend with an unhelpful serialization error, far from the offending task definition.

Change

New _ensure_json_safe helper applied to the three attribute payloads in save_workgraph_data. It walks the structure recursively and:

  1. passes through None / bool / int / float / str and containers of them unchanged;
  2. keeps anything that already survives json.dumps as-is;
  3. unwraps value-carrying objects (obj.value, non-callable - covers Enum members and similar wrappers) and recurses;
  4. falls back to str(value) as a last resort (a lossy-but-stored representation beats an engine crash at store time)

Ordinary workgraphs (already JSON-safe) pass through untouched.

Notes

  • The coercion applies only to the node-attribute copies used for display and provenance browsing; execution-side serialization of task inputs/outputs is unchanged.
  • this bug possibly points to a deeper structural issue, which this PR patches superficially rather han addressing

Node attributes must be JSON-serializable, but workgraph data can carry
non-serializable Python objects (e.g. enum defaults picked up from task
function signatures), which made save_workgraph_data raise on store.
Unwrap value-carrying objects recursively and fall back to str().

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant