LangGraph Integration
This guide demonstrates how to integrate the NeMo Guardrails library with LangGraph to build safe and controlled multi-agent workflows. LangGraph enables you to create sophisticated agent architectures with state management, conditional routing, and tool calling, while NeMo Guardrails provides the safety layer to ensure responsible AI behavior.
Overview
LangGraph is a library for building stateful, multi-actor applications with LLMs. When combined with the NeMo Guardrails library, you can create complex agent workflows that maintain safety and compliance throughout the entire conversation flow.
Key Benefits
- Stateful Safety: Guardrails persist across conversation turns and agent interactions.
- Tool Call Protection: Safety checks for both tool invocation and results.
- Multi-Agent Coordination: Each agent can have its own guardrail configuration.
- Graph-Based Control: Conditional routing with safety considerations.
- Conversation Memory: Maintained context with continuous safety monitoring.
Prerequisites
Install the required dependencies and set up your environment.
-
Install the required dependencies:
-
Make sure that you have OpenAI API keys set up in your environment:
Basic Integration Pattern
The simplest integration involves wrapping your LangGraph nodes with the NeMo Guardrails library using the RunnableRails interface.
Configuration Setup
First, create a simple guardrails configuration for your LangGraph integration. You will use this configuration throughout this section. Create two files:
config.yml:
prompts.yml:
Set the path to the configuration file as a Python variable called config_path.
Then load the configuration:
Basic Agent with Guardrails
The following is a complete example of a basic LangGraph agent with guardrails:
Tool Calling Integration
To enhance the functionality of your LangGraph agents, you can combine tool calling with guardrails. This ensures that both the decision to call tools and the tool results are safely validated.
Tool Definition
Define the following simplified example tools that demonstrate the integration pattern with the NeMo Guardrails library.
The first tool, search_knowledge, searches a predefined knowledge base for information matching the user’s query. It performs matching against keywords like "capital", "weather", and "python", returning relevant information or a generic response if no match is found.
The second tool, multiply, performs multiplication of two integers.
Tool Calling Graph with Guardrails
Create a tool calling graph with guardrails using the tools defined in the previous section.
Stateful Conversations
The checkpointing feature in LangGraph allows you to maintain conversation state across multiple interactions while keeping guardrails active throughout.
Multi-Agent Workflows
You can create sophisticated multi-agent systems where each agent has specialized roles and all are protected by guardrails.
Best Practices
1. Passthrough Mode Configuration
For tool calling and complex flows, use passthrough=True to maintain the original prompt structure:
2. Verbose Logging
Enable verbose mode during development to understand the guardrails flow:
3. Performance Considerations
- Guardrails add latency due to additional LLM calls for safety checks.
- Consider caching strategies for repeated safety validations.
- Monitor token usage as guardrails consume additional tokens.
Debugging and Troubleshooting
Common Issues
- Empty Content with Tool Calls: When using tools, ensure
passthrough=Trueis set. - Authorization Errors: Verify API keys for both main model and safety models.
- Configuration Not Found: Ensure guardrail config paths are correct.
Debugging Tips
- Enable verbose logging to see guardrail execution flow.
- Test without guardrails first to isolate integration issues.
- Check token limits for safety model calls.
- Validate configuration syntax.
Streaming Support
What Works
- Direct RunnableRails async streaming provides true token-by-token streaming.
What Does Not Work
- LangGraph integration with RunnableRails produces single large chunks after processing delays.
- Token-level streaming is not preserved when RunnableRails is integrated into LangGraph nodes.
RunnableRails supports streaming when used directly, but integration with LangGraph fundamentally conflicts with real-time streaming due to node execution requirements and safety validation needs.