Skip to main content

Noma guardrail middleware integration for LangChain

Project description

noma-langchain-integration

Noma guardrail middleware for LangChain agents. Scans user and assistant messages to detect and handle unsafe content before and after agent execution.

Installation

pip install noma-langchain-integration

Quick start

from langchain.agents import create_agent
from langchain_noma.middleware import NomaGuardrailMiddleware

middleware = NomaGuardrailMiddleware(
    api_key="noma-...",  # or set NOMA_API_KEY env var
)

agent = create_agent(
    model="openai:gpt-4.1",
    tools=[...],
    middleware=[middleware],
)

The middleware runs two hooks automatically:

  • before_agent — scans user messages before the agent processes them.
  • after_agent — scans assistant messages after the agent responds.

Both sync and async hooks are supported (abefore_agent, aafter_agent).

Configuration

All parameters are optional and keyword-only:

Parameter Type Default Description
api_key str | None NOMA_API_KEY env API key for the Noma service. Required when using the Noma SaaS endpoint.
application_id str | None NOMA_APPLICATION_ID env or "langchain" Application identifier sent with each scan request.
block_failures bool True When True (fail-closed), block requests if the AIDR API is unreachable. When False (fail-open), allow requests through on scan failure.

Guardrail actions

The Noma API returns one of three actions for each scan:

Action Behavior
allow Request passes through unchanged.
mask Sensitive content in the message is replaced with an anonymized version. Falls back to block if anonymized content is unavailable.
block An AIMessage with a block notice replaces the response and the agent jumps to end.

Environment variables

Variable Required Description
NOMA_API_KEY Yes (SaaS) API key. Can also be passed via the api_key constructor parameter.
NOMA_APPLICATION_ID No Override the application identifier (defaults to "langchain").

Development

# Run unit tests
make tests

# Run integration tests (requires NOMA_API_KEY)
make integration_tests

# Lint and format
make lint
make format

# Type checking
make typing

Publishing

Publishing runs automatically after changes land on the main branch.

  1. Bump version in pyproject.toml.
  2. Merge the version bump to main.

The Publish PyPI GitHub Actions workflow can also be run manually from main for reruns after transient failures.

The workflow publishes to the PyPI project noma-langchain-integration with PyPI Trusted Publishing. No PyPI API token is required when the PyPI pending publisher is configured for this repository and publish_pypi.yml.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

noma_langchain_integration-0.1.0.tar.gz (96.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

noma_langchain_integration-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file noma_langchain_integration-0.1.0.tar.gz.

File metadata

File hashes

Hashes for noma_langchain_integration-0.1.0.tar.gz
Algorithm Hash digest
SHA256 445c5dc662101882dac8fab911153855388bb63efb4f95e4e5b65e8f8267ddfc
MD5 1faa7f67f3bcf82e90b0db614502294e
BLAKE2b-256 622b27130711cf121e8b7e1d60144dc13a234d2453ef10c54546a358be61a850

See more details on using hashes here.

Provenance

The following attestation bundles were made for noma_langchain_integration-0.1.0.tar.gz:

Publisher: publish_pypi.yml on Noma-Security/langchain-noma-integration-package

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file noma_langchain_integration-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for noma_langchain_integration-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a7ca216a36e4ed932b9c814027fc0e0155666ad6faa49a238c010c52d068a72
MD5 6706c078691401ecfe116e7c2e7d5e44
BLAKE2b-256 513b73c58d85fe1454b41f69752246ae1f17ab6bb6af96bd986cbdef429f8aaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for noma_langchain_integration-0.1.0-py3-none-any.whl:

Publisher: publish_pypi.yml on Noma-Security/langchain-noma-integration-package

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page