karenina.benchmark.verification.utils.template_converter¶
template_converter
¶
Bidirectional converter between Python template code and TemplateSpec JSON.
This module provides the bridge between the visual template builder GUI (which works with TemplateSpec JSON) and the verification pipeline (which works with Python source code strings).
Functions:
| Name | Description |
|---|---|
python_to_spec |
Parse Python code into TemplateSpec JSON |
spec_to_python |
Generate Python code from TemplateSpec JSON |
detect_template_mode |
Classify template as 'verified', 'classic', or 'mixed' |
Classes¶
Functions¶
detect_template_mode
¶
detect_template_mode(
code: str,
) -> Literal["verified", "classic", "mixed"]
Detect whether template code uses VerifiedField, classic Field, or both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Python source code string for a template class. |
required |
Returns:
| Type | Description |
|---|---|
Literal['verified', 'classic', 'mixed']
|
'verified' if all fields use VerifiedField, |
Literal['verified', 'classic', 'mixed']
|
'classic' if no fields use VerifiedField, |
Literal['verified', 'classic', 'mixed']
|
'mixed' if some fields use VerifiedField and some use plain Field. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the code cannot be compiled or no Answer class is found. |
Source code in src/karenina/benchmark/verification/utils/template_converter.py
python_to_spec
¶
python_to_spec(code: str) -> TemplateSpec
Convert Python template code to a TemplateSpec JSON structure.
Only works with VerifiedField-based templates (mode 'verified' or 'mixed'). Classic templates raise ValueError since they cannot be represented losslessly in the TemplateSpec format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Python source code string for a VerifiedField template class. |
required |
Returns:
| Type | Description |
|---|---|
TemplateSpec
|
TemplateSpec instance with all field definitions and metadata. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the template is classic (no VerifiedField fields) or if the code cannot be compiled. |
Source code in src/karenina/benchmark/verification/utils/template_converter.py
spec_to_python
¶
spec_to_python(spec: TemplateSpec) -> str
Generate Python template code from a TemplateSpec.
Produces a complete, self-contained Python source string that defines a BaseAnswer subclass with VerifiedField declarations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
TemplateSpec
|
TemplateSpec with field definitions and optional strategy. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Python source code string, ready for exec() or file storage. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the spec fails validation. |
Source code in src/karenina/benchmark/verification/utils/template_converter.py
validate_spec
¶
validate_spec(spec: TemplateSpec) -> list[str]
Validate a TemplateSpec against the primitive registry.
Checks that every field has a valid type, a recognized primitive with correct parameters, and that the primitive is compatible with the field type. Returns a list of error messages; an empty list means the spec is valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
TemplateSpec
|
The TemplateSpec to validate. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of human-readable error strings. Empty if valid. |
Source code in src/karenina/benchmark/verification/utils/template_converter.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |