Skip to main content
This tutorial wraps an existing OpenAI Python client with Anthale. Unlike the direct SDK, this integration raises AnthalePolicyViolationError when Anthale returns block. If you already have an OpenAI client, the change is small: import the guard and wrap the client.
Before you start, create an Anthale API key and a policy, and make sure you already have an OpenAI API key. Use Create an API Key for the Anthale key and First Policy for the policy setup.

Install

Guard the OpenAI client

Add metadata

This integration already extracts the OpenAI request and response messages for you. Use metadata only for application context Anthale cannot infer on its own, such as tenant, user, conversation, or request ID. The keys below are examples. metadata can contain any fields your team uses for tracing and investigation.
The metadata object you pass to guard_openai_client is sent with every Anthale enforcement request created by that guarded client. For guidance on what belongs there, read Metadata & Logs.

What to expect

The guarded OpenAI client stops before the request proceeds when Anthale returns block. You do not inspect response.action here because the integration raises an exception instead.

Verify the integration

  • Send one benign request. The guarded client should complete the model call normally.
  • Send one request that your policy should block. The guarded client should raise AnthalePolicyViolationError before the model call completes.
  • If the policy is configured to redact, send content that should be sanitized and confirm the guarded client continues with sanitized content instead of the original value.
The integration is wired correctly when safe requests still complete, blocked requests raise AnthalePolicyViolationError, and redacted requests continue with sanitized content.

anthale-python

Python SDK source, integrations, and examples.

anthale-openapi

OpenAPI contract used to generate Anthale API clients.

Next steps

Next, compare this with the Python Direct SDK Path, LangChain Agent Middleware for Python, or OpenAI Integration for TypeScript.
Last modified on April 12, 2026