Specs
Looking for YAML spec syntax?
For the full YAML format, required fields, and examples for each spec type, see the Writing Specs user guide.
SpecCache
aitaem.specs.loader.SpecCache
Eagerly-loaded cache for metric, slice, and segment specs.
Use from_yaml() or from_string() as the primary entry points. The constructor creates an empty cache; specs can be added via add().
Source code in aitaem/specs/loader.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 146 147 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 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
metrics
property
Read-only view of all loaded metric specs, keyed by name.
slices
property
Read-only view of all loaded slice specs, keyed by name.
segments
property
Read-only view of all loaded segment specs, keyed by name.
__init__
Empty cache. Use from_yaml() or from_string() to load specs.
from_yaml
classmethod
from_yaml(metric_paths: str | list[str] | None = None, slice_paths: str | list[str] | None = None, segment_paths: str | list[str] | None = None) -> 'SpecCache'
Load and validate all specs from YAML files or directories.
Loading is eager — all specs are loaded and validated before returning.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if a path does not exist |
SpecValidationError
|
if any spec is invalid |
Source code in aitaem/specs/loader.py
from_string
classmethod
from_string(metric_yaml: str | list[str] | None = None, slice_yaml: str | list[str] | None = None, segment_yaml: str | list[str] | None = None) -> 'SpecCache'
Load specs from YAML strings. Validates eagerly.
Each argument can be a single YAML string or a list of YAML strings.
Raises:
| Type | Description |
|---|---|
SpecValidationError
|
if any spec is invalid |
Source code in aitaem/specs/loader.py
add
Add a spec programmatically.
Raises:
| Type | Description |
|---|---|
SpecValidationError
|
if a spec with the same name is already present. |
Source code in aitaem/specs/loader.py
get_metric
Return MetricSpec for the given name.
Raises:
| Type | Description |
|---|---|
SpecNotFoundError
|
if name not found |
Source code in aitaem/specs/loader.py
get_slice
Return SliceSpec for the given name.
Raises:
| Type | Description |
|---|---|
SpecNotFoundError
|
if name not found |
Source code in aitaem/specs/loader.py
get_segment
Return SegmentSpec for the given name.
Raises:
| Type | Description |
|---|---|
SpecNotFoundError
|
if name not found |
Source code in aitaem/specs/loader.py
Introspection
SpecCache exposes three read-only properties for iterating over loaded specs without
needing get_metric() / get_slice() / get_segment() lookups:
cache = SpecCache.from_yaml(metric_paths="metrics/", slice_paths="slices/")
# Iterate names
for name in cache.metrics:
print(name, cache.metrics[name].description)
# Access a spec directly
spec = cache.slices["geography"]
The returned Mapping is a live read-only view of the internal dict (MappingProxyType).
Mutation attempts raise TypeError.
Spec name constraints
All spec names (MetricSpec, SliceSpec, SegmentSpec) must be valid SQL identifiers:
- Match
^[A-Za-z_][A-Za-z0-9_]*$ - Letters, digits, and underscores only
- Must start with a letter or underscore (not a digit)
Names are validated at load time. Invalid names raise SpecValidationError with the
offending name and a suggested replacement. This constraint exists because SliceSpec
names are used as bare SQL column aliases (_slice_{name}), and all names are
validated consistently for simplicity.
Duplicate name enforcement
All loading paths (from_yaml, from_string, add) raise SpecValidationError if a
spec with the same name has already been loaded into the same cache. Uniqueness is
enforced per spec type — a metric and a slice may share a name without conflict.
ValidationResult
ValidationResult is returned by MetricSpec.validate(), SliceSpec.validate(),
SegmentSpec.validate(), and the lower-level validate_metric_spec() /
validate_slice_spec() / validate_segment_spec() functions.
| Field | Type | Description |
|---|---|---|
valid |
bool |
True if the spec passed all validation checks |
errors |
list[ValidationError] |
List of validation errors (empty when valid) |
referenced_columns |
dict[str, list[str]] \| None |
Column map — see below |
referenced_columns
Maps each spec field to the unqualified column names it references. Populated only when
valid is True; None when the spec is invalid.
Warning
Always check result.valid before using result.referenced_columns. When the spec is
invalid, the field is None — not an empty dict.
Keys for metric specs:
| Key | Source |
|---|---|
"numerator" |
SQL expression (AST-parsed) |
"denominator" |
SQL expression (AST-parsed), present only if the field is set |
"timestamp_col" |
Plain string field |
"entities" |
Plain list field, present only if the field is set |
Keys for slice leaf specs:
| Key | Source |
|---|---|
"values[i].where" |
SQL WHERE expression (AST-parsed), one key per value |
Keys for wildcard slice specs:
| Key | Source |
|---|---|
"where" |
The bare column name |
Keys for composite slice specs: empty dict {} — no SQL expressions to extract from.
Keys for segment specs:
| Key | Source |
|---|---|
"entity_id" |
Plain string field |
"join_keys" |
Plain list field, present only when join_keys is non-empty |
"values[i].where" |
SQL WHERE expression (AST-parsed), one key per value |
Example — metric spec:
result = metric_spec.validate()
if result.valid:
for field, columns in result.referenced_columns.items():
print(f"{field}: {columns}")
# numerator: ['revenue']
# denominator: ['impressions']
# timestamp_col: ['created_at']
# entities: ['user_id']
Example — slice spec:
result = slice_spec.validate()
if result.valid:
print(result.referenced_columns)
# {'values[0].where': ['region'], 'values[1].where': ['region', 'country']}
Column names are unqualified — for SUM(t.revenue) the extracted name is "revenue",
not "t.revenue". This is sufficient for single-source specs where each MetricSpec.source
points to one table.
Compatibility
CompatibilityResult and ScanResult are returned by MetricCompute.scan(). They carry the
pre-flight compatibility verdict for every metric × slice and metric × segment pair loaded into
a SpecCache.
CompatibilityResult
One result per metric × spec pair.
| Field | Type | Description |
|---|---|---|
metric_name |
str |
Name of the metric |
spec_name |
str |
Name of the slice or segment |
spec_type |
Literal["slice", "segment"] |
Which kind of spec this row covers |
compatible |
bool |
True when the spec is usable with this metric |
valid_join_keys |
list[str] |
Segment only — join-key candidates present in the metric's source table |
missing_columns |
list[str] |
Columns (slices) or join-key candidates (segments) absent from the source table |
reason |
str \| None |
Human-readable explanation when compatible is False; None when compatible is True |
ScanResult
Container for the full compatibility matrix. The results tuple holds every
CompatibilityResult in metric-declaration order.
| Method | Returns | Description |
|---|---|---|
compatible_slices(metric_name) |
list[str] |
Names of slices compatible with the given metric |
compatible_segments(metric_name) |
list[str] |
Names of segments compatible with the given metric |
compatible_metrics(spec_name) |
list[str] |
Metric names compatible with the given slice or segment |
for_metric(metric_name) |
list[CompatibilityResult] |
All rows for the given metric |
for_spec(spec_name) |
list[CompatibilityResult] |
All rows for the given slice or segment across all metrics |
MetricSpec
aitaem.specs.metric.MetricSpec
dataclass
Source code in aitaem/specs/metric.py
from_yaml
classmethod
Load and validate a MetricSpec from a YAML file path or YAML string.
If yaml_input is a Path or a string pointing to an existing file, it is read as a file. Otherwise, it is treated as a YAML string.
Raises:
| Type | Description |
|---|---|
SpecValidationError
|
if validation fails or YAML is malformed |
FileNotFoundError
|
if a Path is provided but the file does not exist |
Source code in aitaem/specs/metric.py
validate
Validate spec fields and return a ValidationResult (does not raise).
Source code in aitaem/specs/metric.py
SliceSpec
aitaem.specs.slice.SliceSpec
dataclass
Source code in aitaem/specs/slice.py
is_composite
property
True if this spec references other SliceSpecs via cross_product.
is_wildcard
property
True if this spec auto-discovers values from a column at query time.
from_yaml
classmethod
Load and validate a SliceSpec from a YAML file path or YAML string.
Expects top-level key 'slice:'.
Raises:
| Type | Description |
|---|---|
SpecValidationError
|
if validation fails or YAML is malformed |
FileNotFoundError
|
if a Path is provided but the file does not exist |
Source code in aitaem/specs/slice.py
validate
Validate spec fields and return a ValidationResult (does not raise).
Source code in aitaem/specs/slice.py
SliceValue
aitaem.specs.slice.SliceValue
dataclass
SegmentSpec
aitaem.specs.segment.SegmentSpec
dataclass
Source code in aitaem/specs/segment.py
from_yaml
classmethod
Load and validate a SegmentSpec from a YAML file path or YAML string.
Expects top-level key 'segment:'.
Raises:
| Type | Description |
|---|---|
SpecValidationError
|
if validation fails or YAML is malformed |
FileNotFoundError
|
if a Path is provided but the file does not exist |
Source code in aitaem/specs/segment.py
validate
Validate spec fields and return a ValidationResult (does not raise).