Skip to content

Guardrails

Guardrails lets you manage the content safety guardrail resources attached to a CXAS app. Guardrails are protective layers that intercept model inputs and outputs — they can block harmful content, enforce topic restrictions, and ensure your agent stays on-brand.

You'll reach for this class when you want to programmatically audit which guardrails are active on an app, update guardrail configurations as part of a CI/CD pipeline, or create new guardrails from code rather than the Cloud Console.

Quick Example

from cxas_scrapi import Guardrails

app_name = "projects/my-project/locations/us/apps/my-app-id"
guardrails = Guardrails(app_name=app_name)

# See what's there
all_guardrails = guardrails.list_guardrails()
for g in all_guardrails:
    print(g.display_name, g.name)

# Get the name-to-display-name map
guardrails_map = guardrails.get_guardrails_map()
print(guardrails_map)

Reference

Guardrails

Guardrails(app_name, creds_path=None, creds_dict=None, creds=None, scope=None, **kwargs)

Bases: Apps

Core Class for managing Guardrail Resources.

Initializes the Guardrails client.

Source code in src/cxas_scrapi/core/guardrails.py
def __init__(
    self,
    app_name: str,
    creds_path: str = None,
    creds_dict: Dict[str, str] = None,
    creds: Any = None,
    scope: List[str] = None,
    **kwargs,
):
    """Initializes the Guardrails client."""
    project_id = app_name.split("/")[1]
    location = app_name.split("/")[3]

    super().__init__(
        project_id=project_id,
        location=location,
        creds_path=creds_path,
        creds_dict=creds_dict,
        creds=creds,
        scope=scope,
        **kwargs,
    )
    self.resource_type = "guardrails"
    self.app_name = app_name

list_guardrails

list_guardrails()

Lists guardrails within a specific app.

Source code in src/cxas_scrapi/core/guardrails.py
def list_guardrails(self) -> List[types.Guardrail]:
    """Lists guardrails within a specific app."""
    request = types.ListGuardrailsRequest(parent=self.app_name)
    response = self.client.list_guardrails(request=request)
    return list(response)

get_guardrails_map

get_guardrails_map(reverse=False)

Creates a map of Guardrail full names to display names.

Parameters:

Name Type Description Default
reverse bool

If True, map display_name -> name.

False
Source code in src/cxas_scrapi/core/guardrails.py
def get_guardrails_map(self, reverse: bool = False) -> Dict[str, str]:
    """Creates a map of Guardrail full names to display names.

    Args:
        reverse: If True, map display_name -> name.
    """
    guardrails = self.list_guardrails()
    guardrails_dict: Dict[str, str] = {}

    for guardrail in guardrails:
        display_name = guardrail.display_name
        name = guardrail.name
        if display_name and name:
            if reverse:
                guardrails_dict[display_name] = name
            else:
                guardrails_dict[name] = display_name
    return guardrails_dict

get_guardrail

get_guardrail(guardrail_id)

Gets a specific guardrail.

Source code in src/cxas_scrapi/core/guardrails.py
def get_guardrail(self, guardrail_id: str) -> types.Guardrail:
    """Gets a specific guardrail."""
    request = types.GetGuardrailRequest(
        name=f"{self.app_name}/guardrails/{guardrail_id}"
    )
    return self.client.get_guardrail(request=request)

create_guardrail

create_guardrail(guardrail_id, display_name, payload, action='DENY', description='', enabled=True)

Creates a new guardrail given a specific payload dictionary.

The payload controls which of the 5 mutually exclusive guardrail types is instantiated (content_filter, llm_policy, llm_prompt_security, model_safety, code_callback).

Source code in src/cxas_scrapi/core/guardrails.py
def create_guardrail(
    self,
    guardrail_id: str,
    display_name: str,
    payload: Dict[str, Any],
    action: str = "DENY",
    description: str = "",
    enabled: bool = True,
) -> types.Guardrail:
    """Creates a new guardrail given a specific payload dictionary.

    The payload controls which of the 5 mutually exclusive guardrail
    types is instantiated (content_filter, llm_policy,
    llm_prompt_security, model_safety, code_callback).
    """
    # Ensure any existing basic field inside payload doesn't conflict
    if "display_name" in payload:
        payload.pop("display_name")
    if "description" in payload:
        payload.pop("description")

    guardrail = types.Guardrail(
        display_name=display_name,
        description=description,
        action=action,
        enabled=enabled,
        **payload,
    )

    request = types.CreateGuardrailRequest(
        parent=self.app_name, guardrail_id=guardrail_id, guardrail=guardrail
    )
    return self.client.create_guardrail(request=request)

update_guardrail

update_guardrail(guardrail_id, **kwargs)

Updates specific fields of an existing Guardrail.

Source code in src/cxas_scrapi/core/guardrails.py
def update_guardrail(self, guardrail_id: str, **kwargs) -> types.Guardrail:
    """Updates specific fields of an existing Guardrail."""
    guardrail = types.Guardrail(
        name=f"{self.app_name}/guardrails/{guardrail_id}"
    )
    mask_paths = []

    for key, value in kwargs.items():
        setattr(guardrail, key, value)
        mask_paths.append(key)

    request = types.UpdateGuardrailRequest(
        guardrail=guardrail,
        update_mask=field_mask_pb2.FieldMask(paths=mask_paths),
    )
    return self.client.update_guardrail(request=request)

delete_guardrail

delete_guardrail(guardrail_id)

Deletes a specific guardrail.

Source code in src/cxas_scrapi/core/guardrails.py
def delete_guardrail(self, guardrail_id: str) -> None:
    """Deletes a specific guardrail."""
    request = types.DeleteGuardrailRequest(
        name=f"{self.app_name}/guardrails/{guardrail_id}"
    )
    self.client.delete_guardrail(request=request)