Skip to content

to_graphml crashes on dict/list-valued attributes (node metadata, graph-level hyperedges) #1831

Description

@hofmockel

Bug

graphify export graphml crashes on any graph that has a dict or list-valued attribute — e.g. a per-node metadata dict, or the graph-level hyperedges list set by attach_hyperedges():

TypeError: GraphML does not support type <class 'dict'> as data values.
TypeError: GraphML does not support type <class 'list'> as data values.

nx.write_graphml only accepts scalar attribute values. to_graphml already coerces None -> "" for this exact reason (a prior fix for #1502), but dict/list values weren't handled the same way.

Repro

import networkx as nx
from graphify.export import to_graphml

G = nx.Graph()
G.add_node("a", metadata={"kind": "file"})
G.add_node("b")
G.add_edge("a", "b")
G.graph["hyperedges"] = [{"nodes": ["a", "b"], "label": "x"}]

to_graphml(G, {0: ["a", "b"]}, "/tmp/out.graphml")
# TypeError: GraphML does not support type <class 'list'> as data values.

Also reproduces on a real ~2,300-node/4,300-edge project graph (not just this synthetic case) — every export attempt fails, and a failed attempt leaves a 0-byte .graphml file on disk that downstream tooling can mistake for a completed (if empty) export.

Fix

Opened #1830: extends to_graphml's existing None-scrubbing step to also JSON-serialize dict/list-valued attributes across graph-level, node, and edge scopes before calling nx.write_graphml. Includes a regression test mirroring the existing None-value test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions