Generate graph code from DSL for LangGraph framework
Project description
langgraph-codegen
Generates python code for parts of langgraph
from langgraph_codegen import gen_graph
graph_spec = """
# required: start with StateClass and first_node
START(StateClass) => first_node
first_node
should_go_to_second => second_node
=> third_node
second_node => third_node
third_node => END
"""
graph_code = gen_graph("my_graph", graph_spec)
print(graph_code)
Output is:
# GENERATED code, creates compiled graph: my_graph
my_graph = StateGraph(StateClass)
my_graph.add_node('first_node', first_node)
my_graph.add_node('should_go_to_second', should_go_to_second)
my_graph.add_node('second_node', second_node)
my_graph.add_node('third_node', third_node)
my_graph.add_edge(START, 'first_node')
my_graph.add_edge('should_go_to_second', 'second_node')
my_graph.add_edge('should_go_to_second', 'third_node')
my_graph.add_edge('second_node', 'third_node')
my_graph.add_edge('third_node', END)
my_graph = my_graph.compile()
Syntax
START(StateClass) => first_node
required
# anything after pound sign is ignored
node_1 => node_2
unconditional edge
node_X
condition_A => node_Y
condition_B => node_Z
=> END # unconditional if all above conditions fail
node_1 => node_2, node_3
ok to transition to multiple nodes.
Why This DSL Was Made
The main thing I want to do is condense larger patterns into the DSL, to make it easier to experiment with and evaluate graph architectures.
The thing I like about the code with the DSL is that both Nodes and Conditional Edges are represented by functions that take the Graph State as a parameter. The second thing I like about it is that Nodes have a single name, it's in the text graph, and there's a function with that name.
The langgraph graph GraphBuilder is way more flexible, but in many cases an equivalent DSL version is easier to understand and easier to modify, and easier to experiment with different graph architectures.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for langgraph_codegen-0.1.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32463a366bf56cd7e0bf9c7da70fa8e79caa355311b9726dc5670d629c9ec9c9 |
|
MD5 | 48d17339fd73a6add84ba149ccb0745a |
|
BLAKE2b-256 | 4c1d09bde3be5aa6fed01886499ef75d09e43b929f9f5fffd41e918f35f79920 |