Variants are edits to a model that allow you to modify model behavior by adjusting feature activations and defining conditional behaviors.

Creating Variants

Basic Usage

Create a variant by instantiating the Variant class with a base model:

Adding features to a variant

Conditional Controls

You can create variants that respond dynamically to feature activations:

Methods

set()

Set a feature modification.

Parameters:

feature
Feature | FeatureGroup
required

Feature or feature group to modify

value
float
required

Modification value (typically between -1 and 1)

Example:

Set features
formal_feature = client.features.search("formal writing style", model=variant, top_k=1)[0]
casual_feature = client.features.search("casual writing style", model=variant, top_k=1)[0]
# Set a single feature
variant.set(formal_feature, 0.5)

# Set multiple features at once
variant.set({
    formal_feature: 0.5,
    casual_feature: -0.3
})

set_when()

Define conditional feature modifications.

Parameters:

condition
ConditionalGroup
required

Condition that triggers the modifications

values
dict[Feature, float] | FeatureEdits
required

Feature modifications to apply when condition is met

Example:

set_when example
whale_feature = client.features.search("whales", model=variant, top_k=1)[0]
pirate_feature = client.features.search("pirate speech", model=variant, top_k=1)[0]
# Activate pirate features when whale features are detected
variant.set_when(whale_feature > 0.3, {
    pirate_feature: 0.5
})

abort_when()

Abort generation when a condition is met.

Parameters:

condition
ConditionalGroup
required

Condition that triggers the abort

Example:

Abort example
inappropriate_feature = client.features.search("inappropriate content", model=variant, top_k=1)[0]
# Abort if inappropriate content is detected
variant.abort_when(inappropriate_feature > 0.8)

try:
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": "..."}],
        model=variant,
        stream=False
    )
except goodfire.exceptions.InferenceAbortedException:
    print("Aborted because of inappropriate content")

print(variant)

reset()

Remove all feature modifications.

Example:

Reset variant
variant.reset()

clear()

Remove modifications for specific features.

Parameters:

feature
Feature | FeatureGroup
required

Feature(s) to clear modifications for

Example:

Clear particular feature
variant.clear(feature)

Serialization

Variants can be serialized to and from JSON:

Storing and loading variants
# Save variant to JSON
variant_json = variant.json()

# Load variant from JSON
loaded_variant = Variant.from_json(variant_json)

Using with OpenAI SDK

Variants are compatible with the OpenAI SDK:

Using with OpenAI SDK
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_GOODFIRE_API_KEY",
    base_url="https://api.goodfire.ai/api/inference/v1"
)

response = client.chat.completions.create(
    messages=[{"role": "user", "content": "Hello"}],
    model=variant.base_model,
    extra_body={"controller": variant.controller.json()}
)

Classes

VariantMetaData

Metadata about a model variant.

Properties:

name
str

Name of the variant

base_model
str

Base model identifier

id
str

Unique identifier for the variant