Skip to content

Tripwires

Tripwires are the core mechanism by which Guardrails enforce safety policies. When a guardrail detects a violation, it triggers a tripwire that blocks execution by default.

How Tripwires Work

  1. Automatic Execution: Guardrails run on every API call
  2. Tripwire Detection: Violations trigger tripwires
  3. Default Behavior: Tripwires raise GuardrailTripwireTriggered exceptions
  4. Custom Handling: Suppress tripwires to handle violations manually

Default Behavior: Blocking

Tripwires raise exceptions by default:

Python

from pathlib import Path
from guardrails import GuardrailsAsyncOpenAI, GuardrailTripwireTriggered

client = GuardrailsAsyncOpenAI(config=Path("guardrails_config.json"))

try:
    response = await client.responses.create(
        model="gpt-5",
        input="Tell me a secret"
    )
    print(response.llm_response.output_text)

except GuardrailTripwireTriggered as exc:
    print(f"Guardrail triggered: {exc.guardrail_result.info}")

TypeScript

import { GuardrailsOpenAI, GuardrailTripwireTriggered } from '@guardrails/guardrails-ts';

const client = await GuardrailsOpenAI.create({
  version: 1,
  output: {
    version: 1,
    guardrails: [
      { name: 'Moderation', config: { categories: ['hate', 'violence'] } }
    ]
  }
});

try {
  const response = await client.responses.create({
    model: 'gpt-5',
    input: 'Tell me a secret'
  });
  console.log(response.llm_response.output_text);
} catch (err) {
  if (err instanceof GuardrailTripwireTriggered) {
    console.log(`Guardrail triggered: ${JSON.stringify(err.guardrailResult.info)}`);
  } else {
    throw err;
  }
}

Suppressing Tripwires

Handle violations manually with suppress_tripwire=True:

Python

response = await client.responses.create(
    model="gpt-5",
    input="Tell me a secret",
    suppress_tripwire=True
)

# Check guardrail results
for result in response.guardrail_results.all_results:
    if result.tripwire_triggered:
        print(f"Guardrail '{result.info.get('guardrail_name')}' triggered!")

TypeScript

const response = await client.responses.create({
  model: 'gpt-5',
  input: 'Tell me a secret',
  suppress_tripwire: true
});

// Check guardrail results
for (const result of response.guardrail_results.all_results) {
  if (result.tripwire_triggered) {
    console.log(`Guardrail '${result.info.guardrail_name}' triggered!`);
  }
}

Tripwire Results

The GuardrailTripwireTriggered exception contains:

  • tripwire_triggered (bool): Always True
  • info (dict): Guardrail-specific information

Python

except GuardrailTripwireTriggered as exc:
    result = exc.guardrail_result
    guardrail_name = result.info.get('guardrail_name')
    stage = result.info.get('stage_name')

TypeScript

} catch (err) {
  if (err instanceof GuardrailTripwireTriggered) {
    const result = err.guardrailResult;
    const guardrailName = result.info.guardrail_name;
    const stage = result.info.stage_name;
  }
}

Next Steps