{"record":{"id":"4059195878518742","repo":"d2lang/d2","slug":"failed-to-unmarshal-input-to-graph-s","errorCode":null,"errorMessage":"failed to unmarshal input to graph: %s","messagePattern":"failed to unmarshal input to graph: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2plugin/serve.go","lineNumber":123,"sourceCode":"\tb, err := json.Marshal(flags)\n\tif err != nil {\n\t\treturn err\n\t}\n\t_, err = ms.Stdout.Write(b)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc layout(ctx context.Context, p Plugin, ms *xmain.State) error {\n\tin, err := io.ReadAll(ms.Stdin)\n\tif err != nil {\n\t\treturn err\n\t}\n\tvar g d2graph.Graph\n\tif err := d2graph.DeserializeGraph(in, &g); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal input to graph: %s\", in)\n\t}\n\terr = p.Layout(ctx, &g)\n\tif err != nil {\n\t\treturn err\n\t}\n\tb, err := d2graph.SerializeGraph(&g)\n\tif err != nil {\n\t\treturn err\n\t}\n\t_, err = ms.Stdout.Write(b)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc postProcess(ctx context.Context, p Plugin, ms *xmain.State) error {\n\tin, err := io.ReadAll(ms.Stdin)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2plugin/serve.go#L105-L141","documentation":"In the served plugin's layout command, stdin is read and deserialized into a d2graph.Graph. This error means the bytes on stdin were not a valid serialized d2 graph, so layout could not start.","triggerScenarios":"Calling the plugin binary with subcommand layout while stdin contains malformed, empty, or non-JSON data instead of d2graph.SerializeGraph output.","commonSituations":"Invoking the plugin manually without piping a graph, host/plugin protocol mismatch, an intermediate tool corrupting stdin.","solutions":["Ensure the caller serializes the graph with d2graph.SerializeGraph and pipes it to the plugin's stdin","Inspect what is actually on stdin (empty? logs mixed in?)","Check that host and plugin d2 versions match"],"exampleFix":"// before\nmyplugin layout < notes.txt\n// after\nd2plugin serialize graph.json | myplugin layout","handlingStrategy":"validation","validationCode":"var probe map[string]any\nif json.Unmarshal(stdinBytes, &probe) != nil { /* not a serialized graph */ }","typeGuard":"func isSerializedGraph(b []byte) bool {\n\tvar g d2graph.Graph\n\treturn d2graph.DeserializeGraph(b, &g) == nil\n}","tryCatchPattern":"if err := servePlugin(); err != nil {\n\tif strings.Contains(err.Error(), \"failed to unmarshal input to graph\") {\n\t\tlog.Printf(\"plugin stdin was not a serialized d2 graph\")\n\t}\n}","preventionTips":["Always pipe d2graph.SerializeGraph output into the plugin","Never mix logs into the plugin's stdin/stdout contract","Match d2 versions between host and plugin"],"tags":["plugin","json","stdin","deserialization"],"backgroundTag":"plugin-input-invalid-json","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}