Skip to content

crp.integrations

Auto-generated reference for the crp.integrations subpackage.

integrations

crp.integrations

Provider/framework hooks for uninstrumented call sites (CRP 2.3).

CRP 2.2 only enforced when the caller routed through :func:crp.core.dispatch_router.assemble_messages. Applications that call openai.chat.completions.create or anthropic.messages.create directly - or drive the model from LangChain / LlamaIndex - bypassed the enforcer completely.

This sub-package closes that gap with thin wrappers:

  • :func:wrap_openai - drop-in wrapper around an openai.OpenAI / openai.AsyncOpenAI client.
  • :func:wrap_anthropic - drop-in wrapper around an anthropic.Anthropic / anthropic.AsyncAnthropic client.
  • :class:CRPContextCallback - LangChain callback handler that runs the default enforcer on every LLM/ChatModel invocation.

Each wrapper:

  1. Intercepts the outgoing messages payload before the HTTP call.
  2. Runs the installed default enforcer (or the one injected explicitly).
  3. Raises :class:crp.core.errors.CRPError under REJECT policy; logs under WARN; records audit events under OBSERVE. Zero behavioural change for callers that don't install an enforcer.

Imports of the target SDKs are lazy inside the wrapper - CRP itself has zero runtime dependency on openai, anthropic, or langchain.

CRPContextCallback

Bases: _Base

LangChain callback that runs the CRP enforcer before each LLM call.

on_chat_model_start(serialized, messages, **kwargs)

Run the CRP enforcer before a chat-model call starts.

Parameters:

Name Type Description Default
serialized dict[str, Any]

LangChain serialized model info.

required
messages list[list[Any]]

LangChain message payload (list of lists).

required
**kwargs Any

Additional LangChain callback arguments.

{}

on_llm_start(serialized, prompts, **kwargs)

Run the CRP enforcer before a completion-style LLM call starts.

Parameters:

Name Type Description Default
serialized dict[str, Any]

LangChain serialized model info.

required
prompts list[str]

Raw prompt strings for the LLM call.

required
**kwargs Any

Additional LangChain callback arguments.

{}

wrap_openai(client, *, enforcer=None, manifest=None, session_id=None)

Return a proxy around an openai.OpenAI / openai.AsyncOpenAI client.

Every call to client.chat.completions.create(...) (and client.responses.create(...) when available) is preceded by :meth:ContextEnforcer.check_messages. No enforcement happens when no enforcer is installed.

wrap_anthropic(client, *, enforcer=None, manifest=None, session_id=None)

Return a proxy around an anthropic.Anthropic / anthropic.AsyncAnthropic client.

Every call to client.messages.create(...) / client.messages.stream(...) is preceded by :meth:ContextEnforcer.check_messages. The system= parameter and the messages= list are unified into a single chat history for uniform derivation.

integrations.anthropic_hook

crp.integrations.anthropic_hook

Anthropic client wrapper with CRP context enforcement (CRP 2.3).

wrap_anthropic(client, *, enforcer=None, manifest=None, session_id=None)

Return a proxy around an anthropic.Anthropic / anthropic.AsyncAnthropic client.

Every call to client.messages.create(...) / client.messages.stream(...) is preceded by :meth:ContextEnforcer.check_messages. The system= parameter and the messages= list are unified into a single chat history for uniform derivation.

integrations.app_discovery

crp.integrations.app_discovery

Discover an existing application's tools and context strategy.

This module introspects common LLM frameworks without hard-depending on them. Every import of an external package is guarded so CRP still works when the framework is not installed.

profile_from_langchain(llm=None, tools=None, chain=None)

Build an :class:ApplicationProfile from a LangChain setup.

Parameters:

Name Type Description Default
llm Any | None

A LangChain LLM/chat-model instance (used for provider hints).

None
tools Any | None

A list of LangChain tools or an agent/executor with .tools.

None
chain Any | None

A LangChain chain (used for framework detection).

None

profile_from_llamaindex(query_engine=None, tools=None)

Build an :class:ApplicationProfile from a LlamaIndex setup.

profile_from_openai_tools(tools, model='')

Build an :class:ApplicationProfile from an OpenAI-style tool list.

profile_from_mcp_servers(servers)

Build an :class:ApplicationProfile from a list of MCP server refs.

integrations.langchain_hook

crp.integrations.langchain_hook

LangChain callback for CRP context enforcement (CRP 2.3).

Usage::

from langchain_openai import ChatOpenAI
from crp.integrations import CRPContextCallback

llm = ChatOpenAI(
    model="gpt-4o",
    callbacks=[CRPContextCallback()],
)

The callback hooks on_chat_model_start and on_llm_start - both fire before the outbound request is built. If the default enforcer is in REJECT mode and a violation is detected, the callback raises :class:crp.core.errors.CRPError, which LangChain propagates as a chain failure.

The implementation avoids any hard dependency on the langchain package. When langchain_core.callbacks.BaseCallbackHandler is not importable, :class:CRPContextCallback is defined as a plain object that still exposes the hook methods - LangChain duck-types callbacks by attribute name, so integrators on older versions still work.

CRPContextCallback

Bases: _Base

LangChain callback that runs the CRP enforcer before each LLM call.

on_chat_model_start(serialized, messages, **kwargs)

Run the CRP enforcer before a chat-model call starts.

Parameters:

Name Type Description Default
serialized dict[str, Any]

LangChain serialized model info.

required
messages list[list[Any]]

LangChain message payload (list of lists).

required
**kwargs Any

Additional LangChain callback arguments.

{}

on_llm_start(serialized, prompts, **kwargs)

Run the CRP enforcer before a completion-style LLM call starts.

Parameters:

Name Type Description Default
serialized dict[str, Any]

LangChain serialized model info.

required
prompts list[str]

Raw prompt strings for the LLM call.

required
**kwargs Any

Additional LangChain callback arguments.

{}

integrations.openai_hook

crp.integrations.openai_hook

OpenAI client wrapper with CRP context enforcement (CRP 2.3).

Usage::

from openai import OpenAI
from crp.integrations import wrap_openai
from crp.core.context_enforcer import ContextEnforcer, EnforcementPolicy, set_default_enforcer

set_default_enforcer(ContextEnforcer(policy=EnforcementPolicy.REJECT))

client = wrap_openai(OpenAI())
# Every client.chat.completions.create(...) now runs the enforcer.
resp = client.chat.completions.create(model="gpt-4o", messages=[...])

The wrapper is a shallow proxy - attribute access falls through to the real client, so unsupported methods are unaffected. Only chat.completions.create and responses.create are intercepted.

wrap_openai(client, *, enforcer=None, manifest=None, session_id=None)

Return a proxy around an openai.OpenAI / openai.AsyncOpenAI client.

Every call to client.chat.completions.create(...) (and client.responses.create(...) when available) is preceded by :meth:ContextEnforcer.check_messages. No enforcement happens when no enforcer is installed.