Skip to main content

LangGraph Integration

ART’s LangGraph integration enables you to build sophisticated, multi-step AI agents that learn and improve through reinforcement training. By combining LangGraph’s powerful agent framework with ART’s training capabilities, you can create agents that reason, use tools, and adapt their behavior over time.

Installation

To use ART with LangGraph, install ART with the required extras:
The langgraph extra includes the LangGraph integration dependencies, while backend provides the training backend components.

Why Use ART with LangGraph?

LangGraph provides an excellent framework for building various types of agents - from ReAct-style reasoning agents to complex multi-agent workflows with supervisor patterns and parallel execution. However, getting these agents to perform optimally often requires extensive prompt engineering and manual tuning. ART’s integration with LangGraph addresses this by:
  • Automatic behavior improvement: Train your agents to get better at multi-step reasoning without manual prompt tuning
  • Tool usage optimization: Learn when and how to use tools more effectively through reinforcement learning
  • Adaptive decision making: Agents learn to make better choices about which actions to take in different situations
  • Scalable training: Train on diverse scenarios to build robust, generalizable agent behaviors

Key Features

  • Seamless integration: Drop-in replacement for LangGraph’s LLM initialization
  • Automatic logging: Captures all agent interactions for training data generation
  • Multi-step trajectory support: Handles complex agent workflows with tool calls and reasoning steps
  • RULER compatibility: Use ART’s general-purpose reward function to train agents without hand-crafted rewards

Code Examples

Here are easily readable code snippets demonstrating the LangGraph integration functionality:

Basic Setup and Initialization

Defining Tools for Your Agent

Creating and Running a LangGraph ReAct Agent

Trajectory Tracking and Scoring

Training Loop with LangGraph Integration

Correctness Evaluation

Key Components Summary

  1. LangGraph ReAct Agent: Uses create_react_agent() with custom tools and chat model
  2. Tool Definition: Custom tools decorated with @tool for specific functionality
  3. Trajectory Tracking: Custom trajectory class extends art.Trajectory
  4. Training Integration: Uses wrap_rollout() and art.gather_trajectory_groups()
  5. Evaluation: Automated correctness judging with retry logic
  6. Configuration: Flexible training parameters and agent limits

Complete Email Agent Example

Here’s a complete, runnable example that demonstrates training a LangGraph email search agent:
This complete example shows how to:
  1. Set up the environment with model, backend, and data structures
  2. Define custom tools for email search and retrieval
  3. Create a LangGraph ReAct agent with proper configuration
  4. Implement trajectory tracking with custom reward scoring
  5. Run the full training loop with proper error handling
  6. Use wrap_rollout to automatically capture agent interactions
To use this example, simply replace the mock email functions (search_emails, read_email) with your actual email API integration, and provide real training scenarios in the training_scenarios list.

Troubleshooting

Common Issues

Empty trajectories or no training data captured:
  • Ensure you’re using init_chat_model(model.get_inference_name()) in your rollout function
  • Verify your rollout function actually executes the agent and makes LLM calls
  • Check that init_chat_model() is called before creating your LangGraph agent
Import errors:
  • Install ART with the correct extras: uv pip install -U openpipe-art[backend,langgraph]>=0.4.9
  • Ensure you have the required LangGraph dependencies
Training not starting:
  • Verify you have trajectory data with await art.gather_trajectory_groups(...)
  • Check that the model is properly registered with await model.register(backend)

Best Practices

Agent Design

  • Clear tool descriptions: Ensure your tool functions have descriptive docstrings
  • Error handling: Include proper error handling in your tools for robust training
  • Final answer pattern: Use a dedicated tool for returning final answers to users

Training Data

  • Diverse scenarios: Create varied training scenarios that cover different use cases
  • Realistic complexity: Include both simple and complex multi-step tasks
  • Edge cases: Add scenarios that test error handling and edge cases

Performance Optimization

  • Tool efficiency: Optimize tool execution time since it affects training speed
  • Batch generation: Generate multiple trajectories efficiently using async patterns
  • Resource management: Monitor memory usage during long training runs
The ART-LangGraph integration makes it straightforward to build and train sophisticated AI agents that improve their performance over time, turning your prototype agents into production-ready intelligent systems.