Skip to content

Galileo Protect Reference

__version__ module-attribute

__version__ = '0.15.0'

SubscriptionConfig

Bases: BaseModel

Parameters:

  • statuses (List[ExecutionStatus], default: [<ExecutionStatus.triggered: 'triggered'>] ) –

    List of statuses that will cause a notification to be sent to the configured URL.

  • url (Url) –

    URL to send the event to. This can be a webhook URL, a message queue URL, an event bus or a custom endpoint that can receive an HTTP POST request.

statuses class-attribute instance-attribute

statuses: List[ExecutionStatus] = Field(description='List of statuses that will cause a notification to be sent to the configured URL.', default=[triggered])

url class-attribute instance-attribute

url: Url = Field(description='URL to send the event to. This can be a webhook URL, a message queue URL, an event bus or a custom endpoint that can receive an HTTP POST request.')

possible_statuses cached property

possible_statuses: List[str]

List of possible statuses that will trigger a notification to be sent.

This is a list of the cased and uncased versions of the statuses in the statuses list.

Returns:

  • List[str]

    description

warn_if_paused

warn_if_paused(statuses: List[ExecutionStatus]) -> List[ExecutionStatus]

OverrideAction

Bases: BaseAction

Parameters:

  • type (Literal[ActionType], default: <ActionType.OVERRIDE: 'OVERRIDE'> ) –
  • subscriptions (List[SubscriptionConfig], default: [] ) –

    List of subscriptions to send a notification to when this action is applied and the ruleset status matches any of the configured statuses.

  • choices (Sequence[str]) –

    List of choices to override the response with. If there are multiple choices, one will be chosen at random when applying this action.

type class-attribute instance-attribute

type: Literal[OVERRIDE] = OVERRIDE

choices class-attribute instance-attribute

choices: Sequence[str] = Field(description='List of choices to override the response with. If there are multiple choices, one will be chosen at random when applying this action.', min_length=1)

apply

apply(response: str) -> ActionResult

PassthroughAction

Bases: BaseAction

Parameters:

  • type (Literal[ActionType], default: <ActionType.PASSTHROUGH: 'PASSTHROUGH'> ) –
  • subscriptions (List[SubscriptionConfig], default: [] ) –

    List of subscriptions to send a notification to when this action is applied and the ruleset status matches any of the configured statuses.

type class-attribute instance-attribute

type: Literal[PASSTHROUGH] = PASSTHROUGH

apply

apply(response: str) -> ActionResult

Payload

Bases: BaseModel

Parameters:

  • input (str | None, default: None ) –

    Input text to be processed.

  • output (str | None, default: None ) –

    Output text to be processed.

input class-attribute instance-attribute

input: Optional[str] = Field(default=None, description='Input text to be processed.')

output class-attribute instance-attribute

output: Optional[str] = Field(default=None, description='Output text to be processed.')

text cached property

text: str

ensure_input_or_output

ensure_input_or_output() -> Payload

Request

Bases: RulesetsMixin

Parameters:

  • rulesets (Sequence[Ruleset], default: [] ) –

    Rulesets to be applied to the payload.

  • payload (Payload) –

    Payload to be processed.

  • project_name (str | None, default: None ) –

    Project name.

  • project_id (Annotated[UUID, UuidVersion] | None, default: None ) –

    Project ID.

  • stage_name (str | None, default: None ) –

    Stage name.

  • stage_id (Annotated[UUID, UuidVersion] | None, default: None ) –

    Stage ID.

  • stage_version (int | None, default: None ) –

    Stage version to use for the request, if it's a central stage with a previously registered version.

  • timeout (float, default: 300.0 ) –

    Optional timeout for the guardrail execution in seconds. This is not the timeout for the request. If not set, a default timeout of 5 minutes will be used.

  • metadata (Dict[str, str] | None, default: None ) –

    Optional additional metadata. This will be echoed back in the response.

  • headers (Dict[str, str] | None, default: None ) –

    Optional additional HTTP headers that should be included in the response.

payload class-attribute instance-attribute

payload: Payload = Field(description='Payload to be processed.')

project_name class-attribute instance-attribute

project_name: Optional[str] = Field(default=None, description='Project name.', validate_default=True)

project_id class-attribute instance-attribute

project_id: Optional[UUID4] = Field(default=None, description='Project ID.', validate_default=True)

stage_name class-attribute instance-attribute

stage_name: Optional[str] = Field(default=None, description='Stage name.', validate_default=True)

stage_id class-attribute instance-attribute

stage_id: Optional[UUID4] = Field(default=None, description='Stage ID.', validate_default=True)

stage_version class-attribute instance-attribute

stage_version: Optional[int] = Field(default=None, description="Stage version to use for the request, if it's a central stage with a previously registered version.", validate_default=True)

timeout class-attribute instance-attribute

timeout: float = Field(default=total_seconds(), description='Optional timeout for the guardrail execution in seconds. This is not the timeout for the request. If not set, a default timeout of 5 minutes will be used.')

metadata class-attribute instance-attribute

metadata: Optional[Dict[str, str]] = Field(default=None, description='Optional additional metadata. This will be echoed back in the response.')

headers class-attribute instance-attribute

headers: Optional[Dict[str, str]] = Field(default=None, description='Optional additional HTTP headers that should be included in the response.')

model_config class-attribute instance-attribute

model_config = ConfigDict(populate_by_name=True)

rules cached property

rules: List[Rule]

metrics cached property

metrics: Set[str]

timeout_ns cached property

timeout_ns: float

project_name_from_env

project_name_from_env(value: Optional[str]) -> Optional[str]

project_id_from_env

project_id_from_env(value: Optional[UUID4]) -> Optional[UUID4]

stage_name_from_env

stage_name_from_env(value: Optional[str]) -> Optional[str]

stage_id_from_env

stage_id_from_env(value: Optional[UUID4]) -> Optional[UUID4]

stage_version_from_env

stage_version_from_env(value: Optional[int]) -> Optional[int]

validate_project_stage

validate_project_stage() -> Request

Validate that one of: 1. stage_id 2. stage_name and project_id 3. stage_name and project_name are provided.

Returns:

Raises:

  • ValueError

    If the validation fails.

Response

Bases: ExecutionStatusMixIn

Parameters:

  • status (ExecutionStatus, default: <ExecutionStatus.not_triggered: 'not_triggered'> ) –

    Status of the request after processing the rules.

  • text (str) –

    Text from the request after processing the rules.

  • trace_metadata (TraceMetadata) –

text class-attribute instance-attribute

text: str = Field(description='Text from the request after processing the rules.')

trace_metadata instance-attribute

trace_metadata: TraceMetadata

status class-attribute instance-attribute

status: ExecutionStatus = Field(default=not_triggered, description='Status of the request after processing the rules.')

model_config class-attribute instance-attribute

model_config = ConfigDict(extra='allow')

Rule

Bases: BaseModel

Parameters:

  • metric (str) –

    Name of the metric.

  • operator (RuleOperator) –

    Operator to use for comparison.

  • target_value (str | float | int | list | None) –

    Value to compare with for this metric (right hand side).

metric class-attribute instance-attribute

metric: str = Field(description='Name of the metric.')

operator class-attribute instance-attribute

operator: RuleOperator = Field(description='Operator to use for comparison.')

target_value class-attribute instance-attribute

target_value: Union[str, float, int, list, None] = Field(description='Value to compare with for this metric (right hand side).')

evaluate

evaluate(value: MetricValueType) -> bool

RuleMetrics

Bases: str, Enum

context_adherence_luna class-attribute instance-attribute

context_adherence_luna = 'context_adherence_luna'

input_pii class-attribute instance-attribute

input_pii = 'input_pii'

input_sexist class-attribute instance-attribute

input_sexist = 'input_sexist'

input_tone class-attribute instance-attribute

input_tone = 'input_tone'

input_toxicity class-attribute instance-attribute

input_toxicity = 'input_toxicity'

pii class-attribute instance-attribute

pii = 'pii'

prompt_injection class-attribute instance-attribute

prompt_injection = 'prompt_injection'

sexist class-attribute instance-attribute

sexist = 'sexist'

tone class-attribute instance-attribute

tone = 'tone'

toxicity class-attribute instance-attribute

toxicity = 'toxicity'

RuleOperator

Bases: str, Enum

gt class-attribute instance-attribute

gt = 'gt'

lt class-attribute instance-attribute

lt = 'lt'

gte class-attribute instance-attribute

gte = 'gte'

lte class-attribute instance-attribute

lte = 'lte'

eq class-attribute instance-attribute

eq = 'eq'

neq class-attribute instance-attribute

neq = 'neq'

contains class-attribute instance-attribute

contains = 'contains'

all class-attribute instance-attribute

all = 'all'

any class-attribute instance-attribute

any = 'any'

empty class-attribute instance-attribute

empty = 'empty'

not_empty class-attribute instance-attribute

not_empty = 'not_empty'

Ruleset

Bases: BaseModel

Parameters:

  • rules (Sequence[Rule], default: [] ) –

    List of rules to evaluate. Atleast 1 rule is required.

  • action (OverrideAction | PassthroughAction, default: PassthroughAction(type=<ActionType.PASSTHROUGH: 'PASSTHROUGH'>, subscriptions=[]) ) –

    Action to take if all the rules are met.

  • description (str | None, default: None ) –

    Description of the ruleset.

rules class-attribute instance-attribute

rules: Sequence[Rule] = Field(description='List of rules to evaluate. Atleast 1 rule is required.', default_factory=list, min_length=1)

action class-attribute instance-attribute

action: Action = Field(description='Action to take if all the rules are met.', default_factory=PassthroughAction)

description class-attribute instance-attribute

description: Optional[str] = Field(description='Description of the ruleset.', default=None)

validate_action

validate_action(value: Optional[BaseAction]) -> BaseAction

Stage

Bases: BaseModel

Parameters:

  • name (str) –

    Name of the stage. Must be unique within the project.

  • project_id (UUID) –

    ID of the project to which this stage belongs.

  • description (str | None, default: None ) –

    Optional human-readable description of the goals of this guardrail.

  • type (StageType, default: <StageType.local: 'local'> ) –

    Type of the stage.

  • paused (bool, default: False ) –

    Whether the action is enabled. If False, the action will not be applied.

name class-attribute instance-attribute

name: str = Field(description='Name of the stage. Must be unique within the project.')

project_id class-attribute instance-attribute

project_id: UUID4 = Field(description='ID of the project to which this stage belongs.')

description class-attribute instance-attribute

description: Optional[str] = Field(description='Optional human-readable description of the goals of this guardrail.', default=None)

type class-attribute instance-attribute

type: StageType = Field(description='Type of the stage.', default=local)

paused class-attribute instance-attribute

paused: bool = Field(description='Whether the action is enabled. If False, the action will not be applied.', default=False)

Config

Bases: BaseConfig

Parameters:

  • log_level (int, default: 30 ) –
  • skip_ssl_validation (bool, default: False ) –
  • console_url (Url) –
  • api_url (Annotated[Url, UrlConstraints] | None, default: None ) –
  • username (str | None, default: None ) –
  • password (SecretStr | None, default: None ) –
  • api_key (SecretStr | None, default: None ) –
  • jwt_token (SecretStr | None, default: None ) –
  • current_user (str | None, default: None ) –
  • validated_api_client (ApiClient | None, default: None ) –

    Validated API client that is with the user's JWT token after a successful login.

  • config_filename (str, default: 'protect-config.json' ) –
  • project_id (Annotated[UUID, UuidVersion] | None, default: None ) –
  • project_name (str | None, default: None ) –
  • stage_id (Annotated[UUID, UuidVersion] | None, default: None ) –
  • stage_name (str | None, default: None ) –
  • stage_version (int | None, default: None ) –

config_filename class-attribute instance-attribute

config_filename: str = 'protect-config.json'

project_id class-attribute instance-attribute

project_id: Optional[UUID4] = None

project_name class-attribute instance-attribute

project_name: Optional[str] = None

stage_id class-attribute instance-attribute

stage_id: Optional[UUID4] = None

stage_name class-attribute instance-attribute

stage_name: Optional[str] = None

stage_version class-attribute instance-attribute

stage_version: Optional[int] = None

reset

reset() -> None

ProtectParser

Bases: BaseModel

Parameters:

  • chain (Runnable) –

    The chain to trigger if the Protect invocation is not triggered.

  • ignore_trigger (bool, default: False ) –

    Ignore the status of the Protect invocation and always trigger the rest of the chain.

  • echo_output (bool, default: False ) –

    Echo the output of the Protect invocation.

chain class-attribute instance-attribute

chain: Runnable = Field(..., description='The chain to trigger if the Protect invocation is not triggered.')

ignore_trigger class-attribute instance-attribute

ignore_trigger: bool = Field(default=False, description='Ignore the status of the Protect invocation and always trigger the rest of the chain.')

echo_output class-attribute instance-attribute

echo_output: bool = Field(default=False, description='Echo the output of the Protect invocation.')

model_config class-attribute instance-attribute

model_config = ConfigDict(arbitrary_types_allowed=True)

parser

parser(response_raw_json: str) -> str

ProtectTool

Bases: BaseTool

Parameters:

  • name (str, default: 'GalileoProtect' ) –
  • description (str, default: "Protect your LLM applications from harmful content using Galileo Protect. This tool is a wrapper around Galileo's Protect API, can be used to scan text for harmful content, and can be used to trigger actions based on the results.The tool can be used synchronously or asynchronously, on the input text or output text,and can be configured with a set of rulesets to evaluate on." ) –
  • args_schema (Type[BaseModel], default: <class 'galileo_protect.langchain.PayloadV1'> ) –
  • return_direct (bool, default: False ) –
  • verbose (bool, default: False ) –
  • callbacks (List[BaseCallbackHandler] | BaseCallbackManager | None, default: None ) –
  • callback_manager (BaseCallbackManager | None, default: None ) –

    .. deprecated:: 0.1.7 Use callbacks instead.

    Callback manager to add to the run trace.

  • tags (List[str] | None, default: None ) –
  • metadata (Dict[str, Any] | None, default: None ) –
  • handle_tool_error (bool | str | Callable[list, str] | None, default: False ) –
  • handle_validation_error (bool | str | Callable[list, str] | None, default: False ) –
  • response_format (Literal[str, str], default: 'content' ) –
  • prioritized_rulesets (Sequence[Ruleset] | None, default: None ) –
  • project_id (Annotated[UUID, UuidVersion] | None, default: None ) –
  • stage_name (str | None, default: None ) –
  • stage_id (Annotated[UUID, UuidVersion] | None, default: None ) –
  • timeout (float, default: 10.0 ) –

name class-attribute instance-attribute

name: str = 'GalileoProtect'

description class-attribute instance-attribute

description: str = "Protect your LLM applications from harmful content using Galileo Protect. This tool is a wrapper around Galileo's Protect API, can be used to scan text for harmful content, and can be used to trigger actions based on the results.The tool can be used synchronously or asynchronously, on the input text or output text,and can be configured with a set of rulesets to evaluate on."

args_schema class-attribute instance-attribute

args_schema: Type[BaseModel] = PayloadV1

prioritized_rulesets class-attribute instance-attribute

prioritized_rulesets: Optional[Sequence[Ruleset]] = None

project_id class-attribute instance-attribute

project_id: Optional[UUID4] = None

stage_name class-attribute instance-attribute

stage_name: Optional[str] = None

stage_id class-attribute instance-attribute

stage_id: Optional[UUID4] = None

timeout class-attribute instance-attribute

timeout: float = TIMEOUT

is_dependency_available

is_dependency_available(name: str) -> bool

Check if a dependency is available.

Parameters:

  • name (str) –

    The name of the dependency to check.

Returns:

  • bool

    True if the dependency is available, False otherwise.

ainvoke async

ainvoke(payload: Payload, prioritized_rulesets: Optional[Sequence[Ruleset]] = None, project_id: Optional[UUID4] = None, project_name: Optional[str] = None, stage_id: Optional[UUID4] = None, stage_name: Optional[str] = None, timeout: float = TIMEOUT, metadata: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None) -> Response

Asynchronously invoke Protect with the given payload.

If using the local stage, the prioritized rulesets should be provided to ensure the correct rulesets are used for processing. If using a central stage, the rulesets will be fetched from the existing stage definition.

Project ID and stage name, or stage ID should be provided for all invocations.

Parameters:

  • payload (Payload) –

    Payload to be processed.

  • prioritized_rulesets (Optional[Sequence[Ruleset]], default: None ) –

    Prioritized rulesets to be used for processing. These should only be provided if using a local stage, by default None, i.e. empty list.

  • project_id (Optional[UUID4], default: None ) –

    Project ID to be used for processing, by default None.

  • project_name (Optional[str], default: None ) –

    Project name to be used for processing, by default None.

  • stage_id (Optional[UUID4], default: None ) –

    Stage ID to be used for processing, by default None.

  • stage_name (Optional[str], default: None ) –

    Stage name to be used for processing, by default None.

  • timeout (float, default: TIMEOUT ) –

    Timeout for the request, by default 10 seconds.

  • metadata (Optional[Dict[str, str]], default: None ) –

    Metadata to be added when responding, by default None.

  • headers (Optional[Dict[str, str]], default: None ) –

    Headers to be added to the response, by default None.

Returns:

  • Response

    Response from the Protect API.

invoke

invoke(payload: Payload, prioritized_rulesets: Optional[Sequence[Ruleset]] = None, project_id: Optional[UUID4] = None, project_name: Optional[str] = None, stage_id: Optional[UUID4] = None, stage_name: Optional[str] = None, timeout: float = TIMEOUT, metadata: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None) -> Response

Invoke Protect with the given payload.

If using the local stage, the prioritized rulesets should be provided to ensure the correct rulesets are used for processing. If using a central stage, the rulesets will be fetched from the existing stage definition.

Project ID and stage name, or stage ID should be provided for all invocations.

Parameters:

  • payload (Payload) –

    Payload to be processed.

  • prioritized_rulesets (Optional[Sequence[Ruleset]], default: None ) –

    Prioritized rulesets to be used for processing. These should only be provided if using a local stage, by default None, i.e. empty list.

  • project_id (Optional[UUID4], default: None ) –

    Project ID to be used for processing, by default None.

  • project_name (Optional[str], default: None ) –

    Project name to be used for processing, by default None.

  • stage_id (Optional[UUID4], default: None ) –

    Stage ID to be used for processing, by default None.

  • stage_name (Optional[str], default: None ) –

    Stage name to be used for processing, by default None.

  • timeout (float, default: TIMEOUT ) –

    Timeout for the request, by default 10 seconds.

  • metadata (Optional[Dict[str, str]], default: None ) –

    Metadata to be added when responding, by default None.

  • headers (Optional[Dict[str, str]], default: None ) –

    Headers to be added to the response, by default None.

Returns:

  • Response

    Response from the Protect API.

create_project

create_project(name: str = DEFAULT_PROJECT_NAME) -> ProjectResponse

Create a new Protect project.

Parameters:

  • name (str, default: DEFAULT_PROJECT_NAME ) –

    Name of the project, by default project with a timestamp.

Returns:

  • ProjectResponse

    Project creation response.

get_project

get_project(project_id: Optional[UUID4] = None, project_name: Optional[str] = None, raise_if_missing: bool = True) -> Optional[ProjectResponse]

Get a Protect project by either ID or name.

If both project_id and project_name are provided, project_id will take precedence.

Parameters:

  • project_id (Optional[UUID4], default: None ) –

    Project ID, by default None.

  • project_name (Optional[str], default: None ) –

    Project name, by default None.

  • raise_if_missing (bool, default: True ) –

    Raise an error if the project is not found, by default True.

Returns:

  • Optional[ProjectResponse]

    Project response object if the project is found, None otherwise.

Raises:

  • ValueError

    If neither project_id nor project_name is provided.

get_projects

get_projects() -> List[ProjectResponse]

Get all Protect projects.

Returns:

  • List[ProjectResponse]

    List of Protect projects.

create_stage

create_stage(project_id: Optional[UUID4] = None, name: Optional[str] = None, description: Optional[str] = None, pause: bool = False, type: StageType = StageType.local, prioritized_rulesets: Optional[Sequence[Ruleset]] = None) -> StageResponse

Create a stage.

Parameters:

  • project_id (Optional[UUID4], default: None ) –

    Project ID, by default we will try to get it from the config.

  • name (Optional[str], default: None ) –

    Stage name, by default created with a timestamp.

  • description (Optional[str], default: None ) –

    Stage description, by default None.

  • pause (bool, default: False ) –

    Pause the stage, by default False, i.e. the stage is not paused.

  • type (StageType, default: local ) –

    Stage type, by default StageType.local.

  • prioritized_rulesets (Optional[Sequence[Ruleset]], default: None ) –

    Prioritized rulesets, by default None.

Returns:

  • StageResponse

    Stage creation response.

Raises:

  • ValueError

    If the project ID is not provided or found.

get_stage

get_stage(project_id: Optional[UUID4] = None, project_name: Optional[str] = None, stage_id: Optional[UUID4] = None, stage_name: Optional[str] = None) -> StageResponse

Get a stage by ID or name.

Parameters:

  • project_id (Optional[UUID4], default: None ) –

    Project ID, by default, we will try to get it from the config.

  • project_name (Optional[str], default: None ) –

    Project name, by default we will try to get it from the server if the project ID is not provided.

  • stage_id (Optional[UUID4], default: None ) –

    Stage ID, by default we will try to get it from the config.

  • stage_name (Optional[str], default: None ) –

    Stage name, by default we will try to get it from the config.

Returns:

  • StageResponse

    The stage response.

Raises:

  • ValueError

    If the project ID is not provided or found.

  • ValueError

    If the stage ID or name is not provided.

pause_stage

pause_stage(project_id: Optional[UUID4] = None, stage_id: Optional[UUID4] = None) -> None

Pause a stage.

If the stage is already paused, the rulesets in the stage will not be evaluated.

Parameters:

  • project_id (Optional[UUID4], default: None ) –

    Project ID, by default None and will be taken from the config.

  • stage_id (Optional[UUID4], default: None ) –

    Stage ID, by default None and will be taken from the config.

Returns:

  • None

Raises:

  • ValueError

    If the project ID is not provided or found.

  • ValueError

    If the stage ID is not provided or found.

resume_stage

resume_stage(project_id: Optional[UUID4] = None, stage_id: Optional[UUID4] = None) -> None

update_stage

update_stage(project_id: Optional[UUID4] = None, project_name: Optional[str] = None, stage_id: Optional[UUID4] = None, stage_name: Optional[str] = None, prioritized_rulesets: Optional[Sequence[Ruleset]] = None) -> StageResponse

Update a stage by ID or name to create a new version.

Only applicable for central stages. This will create a new version of the stage with the provided rulesets.

Parameters:

  • project_id (Optional[UUID4], default: None ) –

    Project ID, by default we will try to get it from the config.

  • project_name (Optional[str], default: None ) –

    Project name, by default we will try to get it from the server if the project ID is not provided.

  • stage_id (Optional[UUID4], default: None ) –

    Stage ID, by default we will try to get it from the config.

  • stage_name (Optional[str], default: None ) –

    Stage name, by default we will try to get it from the config.

  • prioritized_rulesets (Optional[Sequence[Ruleset]], default: None ) –

    Prioritized rulesets, by default None.

Returns:

  • StageResponse

    The updated stage response.

Raises:

  • ValueError

    If the project ID is not provided or found.

  • ValueError

    If the stage ID is not provided or found.