๐Ÿฆœ๏ธ๐Ÿ”— LangGraphGo

September 4, 2025 ยท View on GitHub

go.dev reference

๐Ÿ”€ Forked from tmc/langgraphgo - Enhanced with streaming, visualization, observability, and production-ready features.

๐Ÿ“ฆ Installation

go get github.com/paulnegz/langgraphgo

๐Ÿš€ Features

  • LangChain Compatible - Works with OpenAI, Anthropic, Google AI, and more
  • Graph Visualization - Export as Mermaid, DOT, or ASCII diagrams
  • Real-time Streaming - Live progress updates with event listeners
  • State Checkpointing - Pause and resume execution
  • Langfuse Integration - Automatic observability and tracing for workflows
  • Production Ready - Error handling, tracing, metrics, and backpressure

๐ŸŽฏ Quick Start

// Simple LLM pipeline
g := graph.NewMessageGraph()
g.AddNode("generate", func(ctx context.Context, state interface{}) (interface{}, error) {
    messages := state.([]llms.MessageContent)
    response, _ := model.GenerateContent(ctx, messages)
    return append(messages, llms.TextParts("ai", response.Choices[0].Content)), nil
})
g.AddEdge("generate", graph.END)
g.SetEntryPoint("generate")

// Compile and run
runnable, _ := g.Compile()
result, _ := runnable.Invoke(ctx, initialState)

๐Ÿ“š Examples

๐ŸŽจ Graph Visualization

%%{init: {'theme':'dark'}}%%
flowchart TD
    START(["๐Ÿš€ START"])
    query[["๐Ÿ” Query Classifier"]]
    retrieve["๐Ÿ“š Retrieve Docs"]
    rerank["๐ŸŽฏ Rerank"]
    check{"โœ… Relevance?"}
    generate["๐Ÿค– Generate"]
    fallback["๐ŸŒ Web Search"]
    format["๐Ÿ“ Format"]
    END(["โœ… END"])
    
    START --> query --> retrieve --> rerank --> check
    check -->|>0.7| generate
    check -->|โ‰ค0.7| fallback --> generate
    generate --> format --> END
    
    style START fill:#90EE90,stroke:#fff,stroke-width:2px
    style END fill:#FFB6C1,stroke:#fff,stroke-width:2px
    linkStyle default stroke:#fff,stroke-width:2px

Export Formats

exporter := graph.NewGraphExporter(g)
mermaid := exporter.DrawMermaid()  // Mermaid diagram
dot := exporter.DrawDOT()          // Graphviz DOT  
ascii := exporter.DrawASCII()      // Terminal output

๐Ÿ”ง Key Concepts

Conditional Routing

g.AddConditionalEdge("router", func(ctx context.Context, state interface{}) string {
    if state.(Task).Priority == "high" {
        return "urgent_handler"
    }
    return "normal_handler"
})

State Checkpointing

g := graph.NewCheckpointableMessageGraph()
g.SetCheckpointConfig(graph.CheckpointConfig{
    Store: graph.NewMemoryCheckpointStore(),
    AutoSave: true,
})

Event Listeners

progress := graph.NewProgressListener().WithTiming(true)
metrics := graph.NewMetricsListener()
node.AddListener(progress)
node.AddListener(metrics)

Callback Support for Tracing

// Create a callback handler (e.g., for Langfuse tracing)
config := &graph.Config{
    Callbacks: []graph.CallbackHandler{myCallbackHandler},
    Tags:      []string{"operation-name"},
    Metadata: map[string]interface{}{
        "user_id": "user123",
        "session_id": "session456",
    },
}

// Invoke with callbacks for automatic tracing
result, _ := runnable.InvokeWithConfig(ctx, initialState, config)

Langfuse Integration Example

// With a Langfuse callback adapter (see langfuse-go for implementation)
import langfuseCallbacks "github.com/paulnegz/langfuse-go/langchain"

handler := langfuseCallbacks.NewCallbackHandler()
handler.SetTraceParams("my-operation", "user123", "session456", metadata)

config := &graph.Config{
    Callbacks: []graph.CallbackHandler{handler},
}

result, _ := runnable.InvokeWithConfig(ctx, initialState, config)

๐Ÿ“ˆ Performance

  • Graph Operations: ~14-94ฮผs depending on format
  • Tracing Overhead: ~4ฮผs per execution
  • Event Processing: 1000+ events/second
  • Streaming Latency: <100ms

๐Ÿงช Testing

go test ./graph -v              # Run tests
go test ./graph -bench=.        # Run benchmarks

๐Ÿ“š API Documentation

๐Ÿค Contributing

This fork enhances tmc/langgraphgo with production features while maintaining API compatibility.

๐Ÿ“„ License

MIT License - see original repository for details.