Skip to main content

langchain-neuraltrust

LangChain 1.x middleware for NeuralTrust TrustGuard. Evaluates agent input, model output, and optionally tool traffic with POST /v1/evaluate.

Install

pip install langchain-neuraltrust

or:

uv add langchain-neuraltrust

Configure

from langchain.agents import create_agent
from langchain_neuraltrust import TrustGuardMiddleware

agent = create_agent(
    model="gpt-4o-mini",
    tools=tools,
    middleware=[
        TrustGuardMiddleware(
            # api_key="tgk_...",          # or TRUSTGUARD_API_KEY
            # collector_key="tgcol_...",  # or TRUSTGUARD_COLLECTOR_KEY
            check_input=True,
            check_output=True,
            payload_tools=tools,          # not `tools=` — that name is reserved
        )
    ],
)

Call close() after invoke, or await aclose() after ainvoke, when the middleware owns the HTTP clients.

Setting Env var Default
api_key TRUSTGUARD_API_KEY required
api_base TRUSTGUARD_API_BASE https://trustguard.neuraltrust.ai
collector_key TRUSTGUARD_COLLECTOR_KEY omitted from the body when unset
session_id TRUSTGUARD_SESSION_ID omitted, then runtime.execution_info.thread_id
model_name TRUSTGUARD_MODEL_NAME omitted, then runtime.context.model
timeout TRUSTGUARD_TIMEOUT 5.0 seconds

payload_tools is included on input-stage evaluate payloads. Pass OpenAI tool dicts or LangChain tools. Do not set TrustGuardMiddleware.tools; create_agent reserves that attribute.

Verdicts

TrustGuard Middleware
allow Continue
report Continue. Fires on_violation and stores findings on additional_kwargs["trustguard"]
block Honors exit_behavior
transform Rewrites the matching messages preserving message.id so LangChain replaces instead of appending

exit_behavior:

  • end (default) — jump to the end of the agent with an AIMessage. On input, only the current turn is removed (from the last HumanMessage through the end), so earlier conversation is kept. On output, the last AI message is removed. On tool results, the originating AIMessage is removed with the ToolMessages so the thread is not left with unanswered tool_calls. SystemMessages are never removed.
  • error — raise TrustGuardBlockedError (hooks and wrap_tool_call)
  • replace — rewrite every non-system message in the evaluated span in place and continue. SystemMessages are left intact. Blocked AIMessages have tool_calls and additional_kwargs["tool_calls"] cleared. Blocked ToolMessages are converted to HumanMessages so they cannot orphan a tool response.

wrap_tool_call blocks return a ToolMessage(status="error") and do not call the tool, unless exit_behavior="error", which raises TrustGuardBlockedError. Fail-closed errors on the tool path follow the same rule. Exceptions raised by the tool handler (including LangGraph interrupts) propagate.

on_violation is a synchronous callback. It runs from both invoke and ainvoke. Exceptions from the callback propagate; they are not turned into a TrustGuard failure.

Fail-closed

unreachable_fallback applies only to connect errors, timeouts, HTTP 502/504, and HTTP 429 after retries are exhausted. Those statuses are retried with backoff (honoring Retry-After when present) before the fallback is applied.

HTTP 401/403, 503 entitlements, other 4xx/5xx, non-JSON 200, unknown verdicts, and unusable transforms always fail closed — including when unreachable_fallback="fail_open".

Transforms fail closed when they:

  • return only {"input": ...} for a multi-message span
  • return a messages array whose length differs from the evaluated span
  • omit role or disagree with the original message role
  • swap list-content block types or inject non-text parts
  • rewrite a tool name or id, or fill in a missing original identity

Streaming

after_model sees the assembled AIMessage after the model call finishes. Token-level streaming is not evaluated mid-stream. Do not rely on this middleware to redact tokens as they leave the provider.

Hooks

Flag Hook
check_input=True before_model / abefore_model
check_output=True after_model / aafter_model
check_tool_results=True tool outputs, during before_model (skipped when check_input is also true)
check_tool_calls=True wrap_tool_call / awrap_tool_call

If both check_input and check_tool_results are true, tool output is evaluated once as part of the conversation payload.

Both sync (invoke) and async (ainvoke) paths are implemented.

Develop

make install
make lint
make typing
make test-unit

Releases follow the TrustTest flow with PyPI for published versions. Push to develop publishes a wheel to the internal dev Artifact Registry (nt-python). Push to main (or master) runs an AI semver bump of langchain_neuraltrust/_version.py, creates a GitHub Release, and publishes that tag to PyPI.

Live tests against the prod demo tenant run only when tests/integration_tests/.creds.json is present (gitignored). The langchain-demo-* collectors reuse the demo policies: allow is a no-op, block/report trigger on the keyword forbidden, and transform redacts an SSN such as 123-45-6789.

Download files

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

Source Distribution

langchain_neuraltrust-0.1.0.tar.gz (153.5 kB view details)

Uploaded Source

Built Distribution

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

langchain_neuraltrust-0.1.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_neuraltrust-0.1.0.tar.gz
  • Upload date:
  • Size: 153.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_neuraltrust-0.1.0.tar.gz
Algorithm Hash digest
SHA256 749c60378c3c605c779c398f87e07db507a008b9581345afbbe3d84aa872bf1e
MD5 3107a51ee1cadefecb819a3dd33e3574
BLAKE2b-256 7c091a408c31179a5c8ae88776ba57098bec7fa55dcca18e647bf13c5e6d0734

See more details on using hashes here.

File details

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

File metadata

  • Download URL: langchain_neuraltrust-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_neuraltrust-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 884123d0ca84f6c82c34aa51626c22ef772a4396a04c4a224298085e16a02256
MD5 e094f7138288ff7cddd957c144795a2e
BLAKE2b-256 573d92ba640bc44a1581192f84f23605c7dddf71cf557cf4773a4c4f8e70b55d

See more details on using hashes here.

Supported by

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