Skip to main content

Generate runnable LangGraph code from a concise graph notation

Project description

langgraph-codegen

Generate runnable LangGraph code from a concise graph notation.

Write a graph like this (plan_and_execute.lgraph):

# Plan and Execute Agent
START:PlanExecute -> plan_step -> execute_step -> replan_step
replan_step -> is_done ? END : execute_step

and lgcodegen plan_and_execute turns it into a runnable LangGraph program with a state class, node stubs, conditional edge functions, and the compiled graph — all with mock implementations you replace incrementally with real code.

The full notation is specified in graph_notation.md. The short version:

Pattern Meaning
START:StateClass -> node Entry point (or bare START to derive the class name from the file name)
a -> b -> c Chained edges
a -> b, c -> d Fan-out then fan-in
a -> cond ? b : c Boolean conditional routing
a -> switch(b, c, END) Switch routing (function returns the next node name)
a -> field | worker -> b Parallel workers over a state list field (Send API)
STATE: ClassName + field: type Explicit state field declarations

Here are the five graphs from the LangChain "Building Effective Agents" video in this notation:

# Agent
START:MessagesState -> llm_call
llm_call -> should_continue ? environment : END
environment -> llm_call
# Evaluator Optimizer
START:State -> llm_call_generator -> llm_call_evaluator
llm_call_evaluator -> route_joke ? END : llm_call_generator
# Orchestrator Worker
START:State -> orchestrator
orchestrator -> sections | llm_call -> synthesizer -> END
# Parallelization
START:State -> call_llm_1, call_llm_2, call_llm_3 -> aggregator -> END
# Prompt Chaining
START:State -> generate_joke
generate_joke -> check_punchline ? improve_joke : END
improve_joke -> polish_joke -> END

Install

pip install langgraph-codegen

Or from source:

git clone https://github.com/jojohannsen/langgraph-codegen
cd langgraph-codegen
pip install -e ".[dev]"

Quick Start

# List built-in examples (shown in --help epilog)
lgcodegen --help

# Show a built-in example's notation
lgcodegen plan_and_execute --show

# Generate everything into a plan_and_execute/ folder
lgcodegen plan_and_execute

# Run it (mock nodes print their names and track state)
cd plan_and_execute && python main.py

Output of the run — every node is a working mock, conditions route randomly:

NODE: plan_step
NODE: execute_step
NODE: replan_step
CONDITION: is_done. Result: False
NODE: execute_step
NODE: replan_step
CONDITION: is_done. Result: True
{'nodes_visited': ['plan_step', 'execute_step', 'replan_step', 'execute_step', 'replan_step'], 'counter': 5}

CLI

lgcodegen my_workflow.lgraph            # generate all files into my_workflow/
lgcodegen my_workflow.lgraph --state    # state class only
lgcodegen my_workflow.lgraph --nodes    # node functions only
lgcodegen my_workflow.lgraph --graph    # graph builder only
lgcodegen my_workflow.lgraph --stdout   # print instead of writing files
lgcodegen my_workflow.lgraph --show     # print the spec and exit
lgcodegen my_workflow.lgraph --verify   # check generated files execute
lgcodegen my_workflow.lgraph -o outdir  # custom output directory

Generated files for input my_workflow.lgraph:

File Contents
my_workflow_state.py State class (TypedDict) with reducers and initialize_state()
my_workflow_nodes.py Node function stubs (working mocks)
my_workflow_graph.py Conditions, workers, and the compiled StateGraph
main.py Entry point: draws the graph PNG and invokes it
my_workflow.lgraphx Expanded intermediate form of the notation

Replace any mock by editing the generated node functions — the graph wiring stays the same.

Library API

from langgraph_codegen import gen_graph

graph_spec = """
START:StateClass -> first_node
first_node -> should_go_on ? second_node : END
second_node -> END
"""

print(gen_graph("my_graph", graph_spec))

Other generators: gen_state, gen_nodes, gen_conditions, gen_worker_functions, plus parse_spec for the parsed representation.

Development

pip install -e ".[dev]"
pytest              # unit tests
./testit            # generate + execute all 14 examples, writes run.md
./clean             # remove generated example folders

Requires Python 3.10+ and langgraph >= 1.0.

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-3.0.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langgraph_codegen-3.0.0-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langgraph_codegen-3.0.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langgraph_codegen-3.0.0.tar.gz
Algorithm Hash digest
SHA256 50006b36e96248397f8d4d475ee588a24a1974576bcb1afb2fc036133c44771c
MD5 1e7b9058250bd8c16724bb4a1ef5f861
BLAKE2b-256 cd7ebe80154082d9211d6334842351898f2ee190be4442de83aacb63ca620753

See more details on using hashes here.

Provenance

The following attestation bundles were made for langgraph_codegen-3.0.0.tar.gz:

Publisher: publish-to-pypi.yml on jojohannsen/langgraph-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for langgraph_codegen-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 516b541003f64de488ac7bf66342713e30b22e469f7358f1fec6483b326af7c4
MD5 f643b810de7c2972a20a8015c87619a4
BLAKE2b-256 3dbbf6d0c21224d23dfbde7f2aaafef6ef016ebe48a30ce8f7ff2a1a4d9a74f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for langgraph_codegen-3.0.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on jojohannsen/langgraph-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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