> ## Documentation Index
> Fetch the complete documentation index at: https://docs.halios.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents Framework

> Integrate HaliosAI guardrails with OpenAI Agents Framework

## Native Guardrail Integration

Agents framework supports guardrails natively. Halios provides guardrail implementation classes that directly plugs into the agent framework's native guardrail evaluation capabilities.

Add HaliosAI guardrails directly to your Agent definitions:

```python theme={null}
from agents import Agent, Runner
from haliosai.openai import HaliosInputGuardrail, HaliosOutputGuardrail

# Create agent with native HaliosAI guardrails
protected_agent = Agent(
    name="protected_assistant",
    instructions="You are a helpful assistant. Always be polite and professional.",
    input_guardrails=[
        HaliosInputGuardrail(
            agent_id="your-agent-id",
            name="halios_input_protection"
        )
    ],
    output_guardrails=[
        HaliosOutputGuardrail(
            agent_id="your-agent-id", 
            name="halios_output_protection"
        )
    ]
)

# Use the agent normally
runner = Runner()
result = await runner.run(
    starting_agent=protected_agent,
    input="Hello! Can you help me?"
)
```

## Complete Integration Example

```python theme={null}
import asyncio
from agents import Agent, Runner
from haliosai.openai import HaliosInputGuardrail, HaliosOutputGuardrail

async def main():
    # Create protected agent
    protected_agent = Agent(
        name="safe_assistant",
        instructions="You are a helpful assistant.",
        input_guardrails=[
            HaliosInputGuardrail(
                agent_id="your-agent-id",
                name="input_guardrail"
            )
        ],
        output_guardrails=[
            HaliosOutputGuardrail(
                agent_id="your-agent-id",
                name="output_guardrail"
            )
        ]
    )
    
    runner = Runner()
    
    try:
        # Run agent with automatic guardrail protection
        result = await runner.run(
            starting_agent=protected_agent,
            input="Write a professional email"
        )
        
        # Access response
        if hasattr(result, 'output'):
            print(f"Response: {result.output}")
        elif hasattr(result, 'final_output'):
            print(f"Response: {result.final_output}")
            
        # Check guardrail results
        if hasattr(result, 'input_guardrail_results'):
            for guardrail_result in result.input_guardrail_results:
                if guardrail_result.output.tripwire_triggered:
                    print(f"Input guardrail triggered: {guardrail_result.guardrail.get_name()}")
                    
        if hasattr(result, 'output_guardrail_results'):
            for guardrail_result in result.output_guardrail_results:
                if guardrail_result.output.tripwire_triggered:
                    print(f"Output guardrail triggered: {guardrail_result.guardrail.get_name()}")
                    
    except Exception as e:
        if "tripwire" in str(e).lower() or "guardrail" in str(e).lower():
            print(f"Request blocked by guardrails: {e}")
        else:
            print(f"Error: {e}")

asyncio.run(main())
```

## Integration Benefits

* **Native Support**: No client patching or complex setup required
* **Declarative Configuration**: Add guardrails directly to agent definitions
* **Full Framework Compatibility**: Works with all OpenAI Agents features
* **Detailed Results**: Access to guardrail evaluation metadata
* **Clean Error Handling**: Proper exception handling for guardrail triggers

## Key Components

* **`HaliosInputGuardrail`**: Evaluates user input before processing
* **`HaliosOutputGuardrail`**: Evaluates agent responses after generation
* **Automatic Evaluation**: Guardrails run automatically during agent execution
* **Result Inspection**: Access detailed guardrail results and metadata

## Example

See the [OpenAI Agents Integration example](https://github.com/HaliosAI/haliosai-sdk/blob/main/examples/05_openai_agents_guardrails_integration.py) for a complete implementation with multiple test scenarios and proper error handling.
