Skip to content

Conversation

@Pouyanpi
Copy link
Collaborator

#‪# Description

Add a new GuardrailsMiddleware class for seamless integration with LangChain's Agent Middleware system. The middleware intercepts agent conversations to apply NeMo Guardrails input/output validation before and after model calls.

This follows the LangChain Middleware pattern introduced in LangChain 1.0, providing before_model and after_model hooks for precise control over the agent loop.

Key features:

  • GuardrailsMiddleware - Full middleware with both input and output rails
  • InputRailsMiddleware - Input-only validation
  • OutputRailsMiddleware - Output-only validation
  • GuardrailViolation exception for programmatic error handling
  • Configurable blocking behavior (raise exception vs return blocked message)
  • Support for both config_path and config_yaml initialization

Usage example:

from langchain.agents import create_agent
from nemoguardrails.integrations.langchain.middleware import GuardrailsMiddleware

guardrails = GuardrailsMiddleware(
    config_path="./config",
    raise_on_violation=False,
)

agent = create_agent("openai:gpt-4o", middleware=[guardrails])

References

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 89.32039% with 22 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...emoguardrails/integrations/langchain/middleware.py 84.09% 21 Missing ⚠️
nemoguardrails/rails/llm/llmrails.py 98.21% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Pouyanpi Pouyanpi force-pushed the feat/guardrails-middleware branch from 8ed8285 to 49c598e Compare January 29, 2026 12:12
@Pouyanpi Pouyanpi changed the title Feat/guardrails middleware feat(integration): add GuardrailsMiddleware for LangChain agent Jan 29, 2026
@Pouyanpi Pouyanpi changed the base branch from develop to feat/check-methods January 29, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants