Skip to content

OpenAI SDK Integration

Swap AsyncOpenAI for MendGuardrailsAsyncOpenAI. Everything else stays the same.

import asyncio
from mendguardrails import MendGuardrailsAsyncOpenAI, GuardrailEnforcementTriggered

async def main():
    client = MendGuardrailsAsyncOpenAI(name="Client 1")

    try:
        response = await client.responses.create(
            model="gpt-4.1-mini",
            input="Hello, can you help me?",
        )
        print(response.output_text)

    except GuardrailEnforcementTriggered as exc:
        print(f"Blocked by: {exc.guardrail_result.info['guardrail_name']}")

asyncio.run(main())

Multi-turn conversations

Pass the current user message inline — don't append to history until after guardrails pass. This prevents blocked messages from persisting in your conversation context.

messages: list[dict] = []

while True:
    user_input = input("You: ")

    try:
        # Pass user message inline, not pre-appended to messages
        response = await client.chat.completions.create(
            messages=messages + [{"role": "user", "content": user_input}],
            model="gpt-4.1-mini",
            suppress_enforcement=False,
        )

        content = response.choices[0].message.content
        print(f"Assistant: {content}")

        # Only append after guardrails pass
        messages.append({"role": "user", "content": user_input})
        messages.append({"role": "assistant", "content": content})

    except GuardrailEnforcementTriggered:
        print("Message blocked — not added to history")

Azure OpenAI

from mendguardrails import MendGuardrailsAsyncAzureOpenAI

client = MendGuardrailsAsyncAzureOpenAI(
    name="Client 1",
    azure_endpoint="https://your-resource.openai.azure.com/",
    api_key="your-azure-key",
    api_version="2025-01-01-preview",
)

Third-party models

MendGuardrailsAsyncOpenAI (and its sync counterpart) work with any OpenAI-compatible API — not just OpenAI itself. Pass base_url and the appropriate api_key to point the client at any provider.

Ollama (local)

from mendguardrails import MendGuardrailsAsyncOpenAI

client = MendGuardrailsAsyncOpenAI(
    name="Client 1",
    base_url="http://127.0.0.1:11434/v1/",
    api_key="ollama",  # Ollama ignores the key but the field is required
)

response = await client.chat.completions.create(
    model="llama3.2",
    messages=[{"role": "user", "content": "Hello"}],
)

LM Studio (local)

client = MendGuardrailsAsyncOpenAI(
    name="Client 1",
    base_url="http://localhost:1234/v1/",
    api_key="lm-studio",
)

Any OpenAI-compatible endpoint

client = MendGuardrailsAsyncOpenAI(
    name="Client 1",
    base_url="https://api.your-provider.com/v1/",
    api_key="your-provider-key",
)

Next steps