Aggregation utils
AggregationError
Bases: ValueError
Raised when aggregation of structured outputs fails due to conflicts or inconsistencies.
Source code in src/kibad_llm/extractors/aggregation_utils.py
12 13 14 15 | |
aggregate_majority_vote(structured_outputs, skip_type_mismatches=False)
Aggregate structured outputs from multiple extractions.
Entries with the same key are aggregated based on their value types: - Primitive types (str, int, float, bool): majority vote - Dict types: majority vote - List types: majority vote per item
This is meant to aggregate outputs from repeated queries with the same schema, where each extraction may produce slightly different results due to LLM variability. The majority vote ensures that only values consistently appearing across extractions are kept, reducing noise and improving reliability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_outputs
|
InputType
|
list of structured outputs from multiple extractions |
required |
skip_type_mismatches
|
bool
|
If True, skips keys with inconsistent types across extractions instead of raising an error (default: False) |
False
|
Returns: aggregated structured output or None if all entries are None
Source code in src/kibad_llm/extractors/aggregation_utils.py
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 | |
aggregate_single_majority_vote_multi_union(structured_outputs, skip_type_mismatches=False)
Aggregate structured outputs from multiple extractions.
Entries with the same key are aggregated based on their value types: - Primitive types (str, int, float, bool): majority vote - Dict types: majority vote - List types: union of all items across extractions
This is meant to aggregate outputs from queries over different document chunks, where single-valued fields (primitives, dicts) should converge to a consistent value via majority vote, while multi-valued fields (lists) may contain different valid items from each chunk that should all be collected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_outputs
|
InputType
|
list of structured outputs from multiple extractions |
required |
skip_type_mismatches
|
bool
|
If True, skips keys with inconsistent types across extractions instead of raising an error (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
OutputType
|
aggregated structured output or None if all entries are None |
Source code in src/kibad_llm/extractors/aggregation_utils.py
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
aggregate_unanimous(structured_outputs, skip_type_mismatches=False)
Aggregate structured outputs with non-overlapping keys.
Combines results from multiple extractions where each extraction is expected to populate different keys (e.g., when a complex schema is split into multiple simpler queries). Each key should appear with a non-None value in at most one extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_outputs
|
InputType
|
list of structured outputs from multiple extractions |
required |
skip_type_mismatches
|
bool
|
If True, skips keys with inconsistent types across extractions instead of raising an error (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
OutputType
|
aggregated structured output or None if all entries are None |
Raises:
| Type | Description |
|---|---|
AggregationError
|
If the same key has non-None values in multiple extractions |
Source code in src/kibad_llm/extractors/aggregation_utils.py
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 | |
collect_values_and_type_per_key(structured_outputs, skip_type_mismatches=False)
Collect values and types per key from structured outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structured_outputs
|
list[dict | None]
|
list of structured outputs from multiple extractions |
required |
skip_type_mismatches
|
bool
|
If True, skips keys with inconsistent types across extractions instead of raising an error (default: False) |
False
|
Returns: tuple of: - dict mapping keys to list of values - dict mapping keys to their consistent type (or None if all values are None)
Source code in src/kibad_llm/extractors/aggregation_utils.py
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 | |