Skip to main content

Guardrails for LangChain

Project description

HiddenLayer Guardrails for 🦜🔗 LangChain & LangGraph (Beta)

This package provides a LangChain agent middleware that integrates with the HiddenLayer Python SDK to scan, redact, and/or block content before and after the agent executes.

It follows the official LangChain custom guardrails middleware pattern using wrap-style hooks to intercept model and tool request and responses.

Installation

pip install hiddenlayer-langchain-guardrails

Configuration

Set your credentials in your environment variables to authenticate with HiddenLayer via the SDK:

  • HIDDENLAYER_CLIENT_ID
  • HIDDENLAYER_CLIENT_SECRET

Usage

from langchain.agents import create_agent
from langchain.tools import tool
from hiddenlayer_langchain_guardrails import HiddenLayerGuardrail, HiddenLayerParams

@tool
def get_weather(city: str) -> str:
    """Return simple weather info for the specified city."""
    return f"The weather in {city} is sunny."

agent = create_agent(
    model="gpt-4o-mini",
    tools=[get_weather],
    middleware=[HiddenLayerGuardrail(
        params=HiddenLayerParams(
            model="gpt-4o-mini",
            project_id=None,          # or your HL project id
            requester_id="example",   # optional but recommended
        )
    )],
)

result = agent.invoke(
    {
        "messages": [
            {"role": "system", "content": "Always respond in haiku form."},
            {"role": "user", "content": "What's the weather in Austin? Use the get_weather tool."},
        ]
    }
)

print(result["messages"][-1].content)

LangGraph Agent with Memory

Use InMemorySaver as a checkpointer to give your agent persistent conversation history across turns:

from langchain.agents import create_agent
from langchain.tools import tool
from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.memory import InMemorySaver

from hiddenlayer_langchain_guardrails import HiddenLayerGuardrail, HiddenLayerParams

@tool
def calculator(expression: str) -> str:
    """Evaluate a basic math expression. Example: '(3 + 5) * 2'."""
    try:
        result = eval(expression, {"__builtins__": {}}, {})  # noqa: S307
        return str(result)
    except Exception as exc:
        return f"Error evaluating expression: {exc}"

agent = create_agent(
    model="gpt-4o-mini",
    tools=[calculator],
    middleware=[HiddenLayerGuardrail(
        params=HiddenLayerParams(requester_id="calculator-agent")
    )],
    checkpointer=InMemorySaver(),
    system_prompt="You are a helpful calculator assistant.",
)

config: RunnableConfig = {"configurable": {"thread_id": "session-1"}}

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What is (12 * 34) + 1348? Use the calculator tool."}]},
    config=config,
)
print(result["messages"][-1].content)

Async Usage

from hiddenlayer_langchain_guardrails import (
    AsyncHiddenLayerGuardrail,
    HiddenLayerParams,
)

@tool
def get_weather(city: str) -> str:
    """Return simple weather info for the specified city."""
    return f"The weather in {city} is sunny."

guardrail = AsyncHiddenLayerGuardrail(
    params=HiddenLayerParams(
        model="gpt-4o-mini",
        project_id=None,          # or your HL project id
        requester_id="example",   # optional but recommended
    )
)

agent = create_agent(
    model="gpt-4o-mini",
    tools=[get_weather],
    middleware=[guardrail],
)

async def main() -> None:
    result = await agent.ainvoke(
        {
            "messages": [
                {"role": "system", "content": "Always respond in haiku form."},
                {
                    "role": "user",
                    "content": "What's the weather in Austin? Use the get_weather tool.",
                },
            ]
        }
    )

    print(result["messages"][-1].content)

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Capability Matrix

Alert Block Redact
Input Guardrails :white_check_mark: :white_check_mark: :white_check_mark:
Output Guardrails :white_check_mark: :white_check_mark: :white_check_mark:
Streaming Output Guardrails :white_check_mark: :x: :x:

Known Limitations

Streaming not supported

Due to a bug in LangChain, middleware guardrails do not run before tokens are streamed to the caller. This means that when using agent.stream() or agent.astream(), output guardrails cannot intercept content before it reaches the user, defeating their purpose for streaming workflows.

Workaround: Use agent.invoke() or agent.ainvoke() instead of the streaming variants to ensure guardrails are applied correctly.

Development

Run tests after installing dev deps (pytest and pytest-asyncio): pytest tests Code lives in src/hiddenlayer_langchain_guardrails/middleware.py; tests are under the tests directory.

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

hiddenlayer_langchain_guardrails-0.2.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file hiddenlayer_langchain_guardrails-0.2.0.tar.gz.

File metadata

File hashes

Hashes for hiddenlayer_langchain_guardrails-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2588154bcd48bc2266a27db7a61e3a5c9e6b6aa2463b18c76f5860d1f2012d17
MD5 5899aaa281e96f6ce8fd3600d3e1dff0
BLAKE2b-256 84aed6d0e5a82f6f3d5e3841e406cd9390e7f6a22117102d19b4d71a25d17b10

See more details on using hashes here.

Provenance

The following attestation bundles were made for hiddenlayer_langchain_guardrails-0.2.0.tar.gz:

Publisher: publish.yml on hiddenlayerai/hiddenlayer-langchain-guardrails

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

File details

Details for the file hiddenlayer_langchain_guardrails-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hiddenlayer_langchain_guardrails-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fdd8684eec27268f8d100a14c6d8e19a3fad0477e1039eef4dadd83911dd82e
MD5 c2c15fb8888c33d413f3e7f0c33f00b6
BLAKE2b-256 438521aeae12bbc25c3808b23011eb6ec1ea86e6f9cf82cb22e7c4e33472b94b

See more details on using hashes here.

Provenance

The following attestation bundles were made for hiddenlayer_langchain_guardrails-0.2.0-py3-none-any.whl:

Publisher: publish.yml on hiddenlayerai/hiddenlayer-langchain-guardrails

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