Skip to content

Constraints

openai_model_registry.constraints

Parameter constraints for the model registry.

This module defines the constraint types used to validate parameters for model calls.

Classes

EnumConstraint

Constraint for enumerated parameters.

Source code in src/openai_model_registry/constraints.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class EnumConstraint:
    """Constraint for enumerated parameters."""

    def __init__(
        self,
        allowed_values: List[str],
        description: str = "",
    ):
        """Initialize enum constraint.

        Args:
            allowed_values: List of allowed string values
            description: Description of the parameter
        """
        self.allowed_values = allowed_values
        self.description = description

    def validate(self, name: str, value: Any) -> None:
        """Validate a value against this constraint.

        Args:
            name: Parameter name for error messages
            value: Value to validate

        Raises:
            ModelRegistryError: If validation fails
        """
        # Validate type
        if not isinstance(value, str):
            raise ModelRegistryError(
                f"Parameter '{name}' must be a string, got {type(value).__name__}.\n" f"Description: {self.description}"
            )

        # Validate allowed values
        if value not in self.allowed_values:
            raise ModelRegistryError(
                f"Invalid value '{value}' for parameter '{name}'.\n"
                f"Description: {self.description}\n"
                f"Allowed values: {', '.join(map(str, sorted(self.allowed_values)))}"
            )
Functions
__init__(allowed_values, description='')

Initialize enum constraint.

Parameters:

Name Type Description Default
allowed_values List[str]

List of allowed string values

required
description str

Description of the parameter

''
Source code in src/openai_model_registry/constraints.py
109
110
111
112
113
114
115
116
117
118
119
120
121
def __init__(
    self,
    allowed_values: List[str],
    description: str = "",
):
    """Initialize enum constraint.

    Args:
        allowed_values: List of allowed string values
        description: Description of the parameter
    """
    self.allowed_values = allowed_values
    self.description = description
validate(name, value)

Validate a value against this constraint.

Parameters:

Name Type Description Default
name str

Parameter name for error messages

required
value Any

Value to validate

required

Raises:

Type Description
ModelRegistryError

If validation fails

Source code in src/openai_model_registry/constraints.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def validate(self, name: str, value: Any) -> None:
    """Validate a value against this constraint.

    Args:
        name: Parameter name for error messages
        value: Value to validate

    Raises:
        ModelRegistryError: If validation fails
    """
    # Validate type
    if not isinstance(value, str):
        raise ModelRegistryError(
            f"Parameter '{name}' must be a string, got {type(value).__name__}.\n" f"Description: {self.description}"
        )

    # Validate allowed values
    if value not in self.allowed_values:
        raise ModelRegistryError(
            f"Invalid value '{value}' for parameter '{name}'.\n"
            f"Description: {self.description}\n"
            f"Allowed values: {', '.join(map(str, sorted(self.allowed_values)))}"
        )

NumericConstraint

Constraint for numeric parameters.

Source code in src/openai_model_registry/constraints.py
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
class NumericConstraint:
    """Constraint for numeric parameters."""

    def __init__(
        self,
        min_value: float = 0.0,
        max_value: Optional[float] = None,
        allow_float: bool = True,
        allow_int: bool = True,
        description: str = "",
    ):
        """Initialize numeric constraint.

        Args:
            min_value: Minimum allowed value
            max_value: Maximum allowed value, or None for no upper limit
            allow_float: Whether floating point values are allowed
            allow_int: Whether integer values are allowed
            description: Description of the parameter
        """
        self.min_value = min_value
        self.max_value = max_value
        self.allow_float = allow_float
        self.allow_int = allow_int
        self.description = description

    def validate(self, name: str, value: Any) -> None:
        """Validate a value against this constraint.

        Args:
            name: Parameter name for error messages
            value: Value to validate

        Raises:
            ModelRegistryError: If validation fails
        """
        # Validate numeric type
        if not isinstance(value, (int, float)):
            raise ModelRegistryError(
                f"Parameter '{name}' must be a number, got {type(value).__name__}.\n"
                "Allowed types: "
                + (
                    "float and integer"
                    if self.allow_float and self.allow_int
                    else ("float only" if self.allow_float else "integer only")
                )
            )

        # Validate integer/float requirements
        if isinstance(value, float) and not self.allow_float:
            raise ModelRegistryError(
                f"Parameter '{name}' must be an integer, got float {value}.\n" f"Description: {self.description}"
            )
        if isinstance(value, int) and not self.allow_int:
            raise ModelRegistryError(
                f"Parameter '{name}' must be a float, got integer {value}.\n" f"Description: {self.description}"
            )

        # Handle special float values (NaN and infinity)
        if isinstance(value, float):
            if math.isnan(value):
                raise ModelRegistryError(
                    f"Parameter '{name}' cannot be NaN (not a number).\n" f"Description: {self.description}"
                )
            if math.isinf(value):
                raise ModelRegistryError(f"Parameter '{name}' cannot be infinity.\n" f"Description: {self.description}")

        # Validate range
        min_val = self.min_value
        max_val = self.max_value

        if value < min_val or (max_val is not None and value > max_val):
            max_desc = str(max_val) if max_val is not None else "unlimited"
            raise ModelRegistryError(
                f"Parameter '{name}' must be between {min_val} and {max_desc}.\n"
                f"Description: {self.description}\n"
                f"Current value: {value}"
            )
Functions
__init__(min_value=0.0, max_value=None, allow_float=True, allow_int=True, description='')

Initialize numeric constraint.

Parameters:

Name Type Description Default
min_value float

Minimum allowed value

0.0
max_value Optional[float]

Maximum allowed value, or None for no upper limit

None
allow_float bool

Whether floating point values are allowed

True
allow_int bool

Whether integer values are allowed

True
description str

Description of the parameter

''
Source code in src/openai_model_registry/constraints.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def __init__(
    self,
    min_value: float = 0.0,
    max_value: Optional[float] = None,
    allow_float: bool = True,
    allow_int: bool = True,
    description: str = "",
):
    """Initialize numeric constraint.

    Args:
        min_value: Minimum allowed value
        max_value: Maximum allowed value, or None for no upper limit
        allow_float: Whether floating point values are allowed
        allow_int: Whether integer values are allowed
        description: Description of the parameter
    """
    self.min_value = min_value
    self.max_value = max_value
    self.allow_float = allow_float
    self.allow_int = allow_int
    self.description = description
validate(name, value)

Validate a value against this constraint.

Parameters:

Name Type Description Default
name str

Parameter name for error messages

required
value Any

Value to validate

required

Raises:

Type Description
ModelRegistryError

If validation fails

Source code in src/openai_model_registry/constraints.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def validate(self, name: str, value: Any) -> None:
    """Validate a value against this constraint.

    Args:
        name: Parameter name for error messages
        value: Value to validate

    Raises:
        ModelRegistryError: If validation fails
    """
    # Validate numeric type
    if not isinstance(value, (int, float)):
        raise ModelRegistryError(
            f"Parameter '{name}' must be a number, got {type(value).__name__}.\n"
            "Allowed types: "
            + (
                "float and integer"
                if self.allow_float and self.allow_int
                else ("float only" if self.allow_float else "integer only")
            )
        )

    # Validate integer/float requirements
    if isinstance(value, float) and not self.allow_float:
        raise ModelRegistryError(
            f"Parameter '{name}' must be an integer, got float {value}.\n" f"Description: {self.description}"
        )
    if isinstance(value, int) and not self.allow_int:
        raise ModelRegistryError(
            f"Parameter '{name}' must be a float, got integer {value}.\n" f"Description: {self.description}"
        )

    # Handle special float values (NaN and infinity)
    if isinstance(value, float):
        if math.isnan(value):
            raise ModelRegistryError(
                f"Parameter '{name}' cannot be NaN (not a number).\n" f"Description: {self.description}"
            )
        if math.isinf(value):
            raise ModelRegistryError(f"Parameter '{name}' cannot be infinity.\n" f"Description: {self.description}")

    # Validate range
    min_val = self.min_value
    max_val = self.max_value

    if value < min_val or (max_val is not None and value > max_val):
        max_desc = str(max_val) if max_val is not None else "unlimited"
        raise ModelRegistryError(
            f"Parameter '{name}' must be between {min_val} and {max_desc}.\n"
            f"Description: {self.description}\n"
            f"Current value: {value}"
        )

ObjectConstraint

Constraint for object/dictionary parameters.

Source code in src/openai_model_registry/constraints.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class ObjectConstraint:
    """Constraint for object/dictionary parameters."""

    def __init__(
        self,
        description: str = "",
        required_keys: Optional[List[str]] = None,
        allowed_keys: Optional[List[str]] = None,
    ):
        """Initialize object constraint.

        Args:
            description: Description of the parameter
            required_keys: List of required keys in the object
            allowed_keys: List of allowed keys (if None, any keys are allowed)
        """
        self.description = description
        self.required_keys = required_keys or []
        self.allowed_keys = allowed_keys

    def validate(self, name: str, value: Any) -> None:
        """Validate a value against this constraint.

        Args:
            name: Parameter name for error messages
            value: Value to validate

        Raises:
            ModelRegistryError: If validation fails
        """
        # Validate type
        if not isinstance(value, dict):
            raise ModelRegistryError(
                f"Parameter '{name}' must be an object/dictionary, got {type(value).__name__}.\n"
                f"Description: {self.description}"
            )

        # Check required keys
        missing_keys = [key for key in self.required_keys if key not in value]
        if missing_keys:
            raise ModelRegistryError(
                f"Parameter '{name}' is missing required keys: {', '.join(missing_keys)}.\n"
                f"Description: {self.description}"
            )

        # Check allowed keys (if specified)
        if self.allowed_keys is not None:
            invalid_keys = [key for key in value.keys() if key not in self.allowed_keys]
            if invalid_keys:
                raise ModelRegistryError(
                    f"Parameter '{name}' contains invalid keys: {', '.join(invalid_keys)}.\n"
                    f"Description: {self.description}\n"
                    f"Allowed keys: {', '.join(self.allowed_keys)}"
                )
Functions
__init__(description='', required_keys=None, allowed_keys=None)

Initialize object constraint.

Parameters:

Name Type Description Default
description str

Description of the parameter

''
required_keys Optional[List[str]]

List of required keys in the object

None
allowed_keys Optional[List[str]]

List of allowed keys (if None, any keys are allowed)

None
Source code in src/openai_model_registry/constraints.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def __init__(
    self,
    description: str = "",
    required_keys: Optional[List[str]] = None,
    allowed_keys: Optional[List[str]] = None,
):
    """Initialize object constraint.

    Args:
        description: Description of the parameter
        required_keys: List of required keys in the object
        allowed_keys: List of allowed keys (if None, any keys are allowed)
    """
    self.description = description
    self.required_keys = required_keys or []
    self.allowed_keys = allowed_keys
validate(name, value)

Validate a value against this constraint.

Parameters:

Name Type Description Default
name str

Parameter name for error messages

required
value Any

Value to validate

required

Raises:

Type Description
ModelRegistryError

If validation fails

Source code in src/openai_model_registry/constraints.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def validate(self, name: str, value: Any) -> None:
    """Validate a value against this constraint.

    Args:
        name: Parameter name for error messages
        value: Value to validate

    Raises:
        ModelRegistryError: If validation fails
    """
    # Validate type
    if not isinstance(value, dict):
        raise ModelRegistryError(
            f"Parameter '{name}' must be an object/dictionary, got {type(value).__name__}.\n"
            f"Description: {self.description}"
        )

    # Check required keys
    missing_keys = [key for key in self.required_keys if key not in value]
    if missing_keys:
        raise ModelRegistryError(
            f"Parameter '{name}' is missing required keys: {', '.join(missing_keys)}.\n"
            f"Description: {self.description}"
        )

    # Check allowed keys (if specified)
    if self.allowed_keys is not None:
        invalid_keys = [key for key in value.keys() if key not in self.allowed_keys]
        if invalid_keys:
            raise ModelRegistryError(
                f"Parameter '{name}' contains invalid keys: {', '.join(invalid_keys)}.\n"
                f"Description: {self.description}\n"
                f"Allowed keys: {', '.join(self.allowed_keys)}"
            )

ParameterReference dataclass

Reference to a parameter constraint with optional metadata.

Source code in src/openai_model_registry/constraints.py
17
18
19
20
21
22
23
@dataclass
class ParameterReference:
    """Reference to a parameter constraint with optional metadata."""

    ref: str
    description: str = ""
    max_value: Optional[float] = None