Utilities

Guardrails utils.

class smarter.apps.guardrail.utils.GuardrailExample(filepath, filename)[source]

Bases: object

A class for loading and working with built-in YAML-based guardrail examples.

This class reads guardrail example files in YAML format, parses their contents, and exposes metadata and serialization methods for inspection and testing.

Parameters:
  • filepath (str) – The directory path containing the YAML file.

  • filename (str) – The name of the YAML file to load.

See also

GuardrailExamples for managing collections of guardrail examples.

Example usage:

example = GuardrailExample(filepath="/path/to/examples", filename="my_guardrail.yaml")
print(example.name)
print(example.to_yaml())
print(example.to_json())
__init__(filepath, filename)[source]

Initialize the class from a yaml file.

convert_filename()[source]

Convert the filename to the desired format.

Return type:

Optional[str]

property filename: str | None

Return the name of the guardrail manifest.

property filepath: str | None

Return the filepath of the guardrail manifest.

property fullpath: str | None
property name: str | None

Return the name of the guardrail.

to_json()[source]

Return the guardrail as a dictionary.

Return type:

Union[list, dict, None]

to_yaml()[source]

Return the guardrail as a yaml string.

Return type:

Optional[str]

class smarter.apps.guardrail.utils.GuardrailExamples(*args, **kwargs)[source]

Bases: object

A class for managing a collection of GuardrailExample instances.

This class loads all YAML-based guardrail examples from a specified directory, providing access to the collection and utility methods for counting and retrieving examples.

Parameters:
  • args (tuple) – Optional positional arguments (unused).

  • kwargs (dict) – Optional keyword arguments (unused).

Note

Only files ending with .yaml in the guardrails path are loaded as examples.

Tip

Use count() to get the number of loaded guardrail examples, and the guardrails() property to access the list.

See also

GuardrailExample for individual example details.

Example usage:

examples = GuardrailExamples()
print(examples.count())
for example in examples.guardrails:
    print(example.filename, example.name)
GUARDRAILS_PATH = '/Users/mcdaniel/Desktop/gh/smarter-sh/smarter/smarter/smarter/apps/guardrail/data/guardrails'
HERE = '/Users/mcdaniel/Desktop/gh/smarter-sh/smarter/smarter/smarter/apps/guardrail'
__init__(*args, **kwargs)[source]

Initialize the class.

count()[source]

Return the number of guardrails.

Return type:

int

property guardrails: list[GuardrailExample]

Return a list of guardrails in dictionary format.

smarter.apps.guardrail.utils.add_builtin_guardrails(user_profile, verbose=False)[source]

Apply the built-in Guardrail manifests, in data/guardrails, for a user.

manage.py initialize_platform applies them for the Smarter admin user, so that every account’s LLMClients may list them in their spec.guardrails.

Parameters:

user_profile (Optional[UserProfile]) – The UserProfile instance representing the new user. Must not be None.

Returns:

Returns True if all example guardrails are created and validated successfully.

Return type:

bool

Raises:

SmarterValueError – If user_profile is not provided, or if manifest/secret application fails, or if a guardrail does not have a valid YAML representation.

Note

  • This function applies the manifests with the apply_manifest management command.

  • This function is called during deployment jobs.

Important

  • The user_profile parameter must be a valid UserProfile instance. Passing None or an incorrect type will result in an error.

  • If any manifest or secret update fails, the function raises an exception and does not proceed with guardrail creation.

See also

Example usage:

from smarter.apps.account.models import UserProfile
from smarter.apps.guardrail.utils import add_example_guardrails

user_profile = UserProfile.objects.get(user__username="newuser")
success = add_example_guardrails(user_profile)
if success:
    print("Example guardrails created successfully.")
smarter.apps.guardrail.utils.should_log(level)[source]

Check if logging should be done based on the waffle switch.