Auto-serialize EdgeSpec and NodeSpec to dictionaries#38
Merged
philippjfr merged 7 commits intomainfrom Feb 11, 2026
Merged
Conversation
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
…ds correctly Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Auto-serialize edges and nodes in EdgeSpec
Auto-serialize EdgeSpec and NodeSpec to dictionaries
Feb 9, 2026
…es-nodes Review feedback
MarcSkovMadsen
approved these changes
Feb 9, 2026
Contributor
MarcSkovMadsen
left a comment
There was a problem hiding this comment.
Local Claude and I am happy.
Manual Test App
"""Test app for verifying auto-serialization of NodeSpec/EdgeSpec.
This app demonstrates the new auto-serialization feature where NodeSpec
and EdgeSpec objects are passed directly to ReactFlow without calling
.to_dict() manually.
"""
import panel as pn
from panel_reactflow import EdgeSpec, NodeSpec, NodeType, ReactFlow
pn.extension("jsoneditor")
# Define a custom node type with a schema
node_types = {
"task": NodeType(
type="task",
label="Task",
schema={
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["pending", "running", "done"],
"title": "Status",
},
"priority": {
"type": "number",
"minimum": 1,
"maximum": 5,
"title": "Priority",
},
},
},
inputs=["input"],
outputs=["output"],
),
}
# Auto-serialization: pass NodeSpec objects directly, no .to_dict() needed
nodes = [
NodeSpec(
id="n1",
type="task",
position={"x": 50, "y": 50},
label="Fetch Data",
data={"status": "done", "priority": 3},
),
NodeSpec(
id="n2",
type="task",
position={"x": 300, "y": 50},
label="Transform",
data={"status": "running", "priority": 2},
),
NodeSpec(
id="n3",
type="task",
position={"x": 550, "y": 50},
label="Load Results",
data={"status": "pending", "priority": 1},
),
# Mix: plain dict alongside NodeSpec objects
{
"id": "n4",
"type": "panel",
"position": {"x": 300, "y": 250},
"label": "Plain dict node",
"data": {},
},
]
# Auto-serialization also works for EdgeSpec
edges = [
EdgeSpec(id="e1", source="n1", target="n2", label="pipe"),
EdgeSpec(id="e2", source="n2", target="n3", label="pipe"),
EdgeSpec(id="e3", source="n2", target="n4"),
]
flow = ReactFlow(
nodes=nodes,
edges=edges,
node_types=node_types,
min_height=500,
sizing_mode="stretch_both",
)
# Verify auto-serialization happened
for i, node in enumerate(flow.nodes):
assert isinstance(node, dict), f"Node {i} should be a dict, got {type(node)}"
for i, edge in enumerate(flow.edges):
assert isinstance(edge, dict), f"Edge {i} should be a dict, got {type(edge)}"
description = pn.pane.Markdown(
"""## Auto-Serialization Test
NodeSpec and EdgeSpec objects are passed **directly** to ReactFlow (no `.to_dict()` needed).
- **Nodes 1-3**: Created with `NodeSpec(...)` and passed directly
- **Node 4**: Created as a plain dict (still works)
- **Edges**: Created with `EdgeSpec(...)` and passed directly
- All nodes and edges are auto-converted to dicts internally
""",
sizing_mode="stretch_width",
)
layout = pn.Column(description, flow, sizing_mode="stretch_both", min_height=700)
layout.servable()
docs/how-to/define-nodes-edges.md
Outdated
|
|
||
| !!! note "Automatic Serialization" | ||
| `NodeSpec` and `EdgeSpec` objects are automatically converted to dictionaries | ||
| when passed to `ReactFlow`. You no longer need to call `.to_dict()` manually. |
Contributor
There was a problem hiding this comment.
Please remove the no longer. This framework is so new that almost no one would have read the documentation.
Contributor
Author
There was a problem hiding this comment.
Fixed in f343d26. Changed "You no longer need" to "You don't need" to avoid implying a previous state that users wouldn't be familiar with.
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
MarcSkovMadsen
approved these changes
Feb 9, 2026
philippjfr
approved these changes
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Auto-serialize EdgeSpec and NodeSpec Objects
This PR implements automatic serialization of
EdgeSpecandNodeSpecobjects to dictionaries, eliminating the need for users to manually call.to_dict().Problem Fixed
Previously, users had to call
.to_dict()on EdgeSpec/NodeSpec objects when passing them to ReactFlow:Without
.to_dict(), the code would fail with:Solution
Now EdgeSpec and NodeSpec objects are automatically converted to dictionaries:
Implementation
_normalize_nodes()and_normalize_edges()methods that convert Spec objects to dicts__init__for objects passed during initializationsuper().__init__()Changes
Code:
src/panel_reactflow/base.py: Implementation of auto-serializationtests/test_api.py: Comprehensive test coverageDocumentation:
docs/how-to/define-nodes-edges.md: Updated guide with new featureTesting
✅ All auto-serialization tests pass
✅ Original issue example works correctly
✅ No infinite recursion
✅ Backward compatible with existing dict-based code
✅ No security vulnerabilities detected
Benefits
.to_dict()Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.