Skip to content

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

Closed

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.

Activity

  1. safishamsi commented on Jul 12, 2026

    @safishamsi
    Member

    Fixed in a46eee4 (ships in 0.9.14). to_graphml now coerces None -> "" and JSON-serializes dict/list (and any other non-scalar) values across graph-level, node, and edge scopes, while GraphML-native int/float/bool/str pass through unchanged. Also addressed the 0-byte-file hazard you flagged: the export now writes to a sibling temp file and os.replaces it on success, so a failed export can't leave a partial/empty .graphml behind. Verified with a round-trip read (dict/list come back as JSON, scalars keep their types) plus a regression test. Thanks for the clean repro and for spotting the partial-file issue too, @hofmockel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions