Skip to main content

Generate graph code from DSL for LangGraph framework

Project description

langgraph-codegen

Quick Start

To generate a graph from text:

# View available example graphs, 'plan_and_execute' is one of the examples
lgcodegen --list

# View contents of a graph file
lgcodegen plan_and_execute

# Generate different components
lgcodegen --graph plan_and_execute    # Generate graph code
lgcodegen --nodes plan_and_execute    # Generate node code
lgcodegen --conditions plan_and_execute    # Generate condition code
lgcodegen --state plan_and_execute    # Generate state code

# complete running graph with mocked nodes, state, conditions
# Runnable code in: plan_and_execute/plan_and_execute.py
lgcodegen plan_and_execute --code
python plan_and_execute/plan_and_execute.py
How to use gen_graph function

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langgraph_codegen-0.1.25.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

langgraph_codegen-0.1.25-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file langgraph_codegen-0.1.25.tar.gz.

File metadata

  • Download URL: langgraph_codegen-0.1.25.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for langgraph_codegen-0.1.25.tar.gz
Algorithm Hash digest
SHA256 b6f40e31f2ee9fd1b799a5858baae2a048d51d8478b8d9400053e8d968c5aba9
MD5 bc447a71938b4b39ec37610c7fc6834b
BLAKE2b-256 67259ac2f8d33bd658c6eb0eb15bab5e6c826624b94f1468c6befc3e9ffea8c7

See more details on using hashes here.

File details

Details for the file langgraph_codegen-0.1.25-py3-none-any.whl.

File metadata

File hashes

Hashes for langgraph_codegen-0.1.25-py3-none-any.whl
Algorithm Hash digest
SHA256 be68c09f56a8d7e0638a62407a21727647bf0aef008c75b34332441978d3b707
MD5 d0654cd05592262112f1920035016b10
BLAKE2b-256 5b77f668b9cbbda8323dc525aab916a1519b163d19bd752a1342b59b3768807f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page