Skip to content

write_graph(g, format = "gml") fails because NULL id coerced to numeric(0) #2710

Description

@sshugars

What happens, and what did you expect instead?

write_graph(g, format = "gml") fails for any graph without an id attribute, raising:

Error in write_graph_gml_impl(...): 
  Size of id vector must match vertex count. Invalid value
Source: io/gml.c:1057

Expected behavior
With no id argument, the GML writer should generate default node ids and write the file (as it did in an earlier igraph version).

Actual behavior
It always errors at gml.c:1057.

Apparent root cause
The high-level write.graph.gml() guards its coercion:

if (!is.null(id)) {
  id <- as.numeric(id)
}

so a NULL id is passed through to the auto-generated write_graph_gml_impl(). That function, however, coerces unconditionally:
id <- as.numeric(id)
and as.numeric(NULL) returns numeric(0), not NULL. The C routine therefore receives a non-NULL, length-0 id vector instead of "no id supplied." In gml.c, myid becomes non-NULL with size 0, so the if (myid) branch runs and the size check (0 != vcount) fails. This is consistent with the error firing before any vertex data is examined, and with an explicit correctly-sized id avoiding it.

A possible fix would be to guard the coercion in write_graph_gml_impl() the same way write.graph.gml() does (if (!is.null(id)) id <- as.numeric(id)), so a NULL id reaches C as a null pointer.

Workaround
Explicitly specify ids:
write_graph(g, file, format = "gml", id = seq_len(vcount(g)))

To reproduce

library(igraph)
write_graph(make_ring(5), tempfile(fileext = ".gml"), "gml")

Error in write_graph_gml_impl(graph = graph, outstream = file, options = "default", :
Size of id vector must match vertex count. Invalid value
Source: io/gml.c:1057

Supplying an explicit id works:

g <- make_ring(5)
write_graph(g, tempfile(fileext = ".gml"), "gml", id = seq_len(vcount(g)))   # no error

System information

sessionInfo()
R version 4.6.0 (2026-04-24)
Platform: aarch64-apple-darwin23
Running under: macOS Tahoe 26.5.1

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Detroit
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] igraph_2.3.2

loaded via a namespace (and not attached):
[1] compiler_4.6.0 magrittr_2.0.5 cli_3.6.6 tools_4.6.0 lifecycle_1.0.5
[6] pkgconfig_2.0.3 rlang_1.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    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