langgraphgo

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2025 License: MIT

README ΒΆ

πŸ¦œοΈπŸ”— LangGraphGo

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.

Directories ΒΆ

Path Synopsis
examples
basic_example command
basic_llm command
checkpointing command
listeners command
rag_pipeline command
subgraph command
visualization command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL