Skip to content

fenic.core.types.semantic_examples

Module for handling semantic examples in query processing.

This module provides classes and utilities for building, managing, and validating semantic examples used in query processing.

Classes:

BaseExampleCollection

BaseExampleCollection(examples: List[ExampleType] = None)

Bases: ABC, Generic[ExampleType]

Abstract base class for all semantic example collections.

Semantic examples demonstrate the expected input-output relationship for a given task, helping guide language models to produce consistent and accurate responses. Each example consists of inputs and the corresponding expected output.

These examples are particularly valuable for:

  • Demonstrating the expected reasoning pattern
  • Showing correct output formats
  • Handling edge cases through demonstration
  • Improving model performance without changing the underlying model

Initialize a collection of semantic examples.

Parameters:

  • examples (List[ExampleType], default: None ) –

    Optional list of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.

Note

The examples list is initialized as empty if no examples are provided. Each example in the provided list will be processed through create_example() to ensure proper formatting and validation.

Methods:

  • create_example

    Create an example in the collection.

  • from_pandas

    Create a collection from a Pandas DataFrame.

  • from_polars

    Create a collection from a Polars DataFrame.

  • to_pandas

    Convert the collection to a Pandas DataFrame.

  • to_polars

    Convert the collection to a Polars DataFrame.

Source code in src/fenic/core/types/semantic_examples.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(self, examples: List[ExampleType] = None):
    """Initialize a collection of semantic examples.

    Args:
        examples: Optional list of examples to add to the collection. Each example
            will be processed through create_example() to ensure proper formatting
            and validation.

    Note:
        The examples list is initialized as empty if no examples are provided.
        Each example in the provided list will be processed through create_example()
        to ensure proper formatting and validation.
    """
    self.examples: List[ExampleType] = []
    if examples:
        for example in examples:
            self.create_example(example)

create_example

create_example(example: ExampleType) -> BaseExampleCollection

Create an example in the collection.

example: The semantic example to add. Must be an instance of the collection's example_class.

Returns:

Source code in src/fenic/core/types/semantic_examples.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def create_example(self, example: ExampleType) -> BaseExampleCollection:
    """Create an example in the collection.

    Args:
    example: The semantic example to add. Must be an instance of the
            collection's example_class.

    Returns:
        Self for method chaining.
    """
    if not isinstance(example, self.example_class):
        raise InvalidExampleCollectionError(
            f"Expected example of type {self.example_class.__name__}, got {type(example).__name__}"
        )
    self.examples.append(example)
    return self

from_pandas classmethod

from_pandas(df: DataFrame) -> BaseExampleCollection

Create a collection from a Pandas DataFrame.

Parameters:

  • df (DataFrame) –

    The Pandas DataFrame containing example data. The specific column structure requirements depend on the concrete collection type.

Returns:

Raises:

Source code in src/fenic/core/types/semantic_examples.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
@classmethod
def from_pandas(cls, df: pd.DataFrame) -> BaseExampleCollection:
    """Create a collection from a Pandas DataFrame.

    Args:
        df: The Pandas DataFrame containing example data. The specific
            column structure requirements depend on the concrete collection type.

    Returns:
        A new example collection populated with examples from the DataFrame.

    Raises:
        InvalidExampleCollectionError: If the DataFrame's structure doesn't match
            the expected format for this collection type.
    """
    polars_df = pl.from_pandas(data=df)
    return cls.from_polars(polars_df)

from_polars abstractmethod classmethod

from_polars(df: DataFrame) -> BaseExampleCollection

Create a collection from a Polars DataFrame.

Parameters:

  • df (DataFrame) –

    The Polars DataFrame containing example data. The specific column structure requirements depend on the concrete collection type.

Returns:

Raises:

Source code in src/fenic/core/types/semantic_examples.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
@classmethod
@abstractmethod
def from_polars(cls, df: pl.DataFrame) -> BaseExampleCollection:
    """Create a collection from a Polars DataFrame.

    Args:
        df: The Polars DataFrame containing example data. The specific
            column structure requirements depend on the concrete collection type.

    Returns:
        A new example collection populated with examples from the DataFrame.

    Raises:
        InvalidExampleCollectionError: If the DataFrame's structure doesn't match
            the expected format for this collection type.
    """
    pass

to_pandas

to_pandas() -> pd.DataFrame

Convert the collection to a Pandas DataFrame.

Returns:

  • DataFrame

    A Pandas DataFrame representing the collection's examples.

  • DataFrame

    Returns an empty DataFrame if the collection contains no examples.

Source code in src/fenic/core/types/semantic_examples.py
199
200
201
202
203
204
205
206
207
def to_pandas(self) -> pd.DataFrame:
    """Convert the collection to a Pandas DataFrame.

    Returns:
        A Pandas DataFrame representing the collection's examples.
        Returns an empty DataFrame if the collection contains no examples.
    """
    rows = self._as_df_input()
    return pd.DataFrame(rows)

to_polars

to_polars() -> pl.DataFrame

Convert the collection to a Polars DataFrame.

Returns:

  • DataFrame

    A Polars DataFrame representing the collection's examples.

  • DataFrame

    Returns an empty DataFrame if the collection contains no examples.

Source code in src/fenic/core/types/semantic_examples.py
189
190
191
192
193
194
195
196
197
def to_polars(self) -> pl.DataFrame:
    """Convert the collection to a Polars DataFrame.

    Returns:
        A Polars DataFrame representing the collection's examples.
        Returns an empty DataFrame if the collection contains no examples.
    """
    rows = self._as_df_input()
    return pl.DataFrame(rows)

ClassifyExample

Bases: BaseModel

A single semantic example for classification operations.

Classify examples demonstrate the classification of an input string into a specific category string, used in a semantic.classify operation.

ClassifyExampleCollection

ClassifyExampleCollection(examples: List[ExampleType] = None)

Bases: BaseExampleCollection[ClassifyExample]

Collection of text-to-category examples for classification operations.

Stores examples showing which category each input text should be assigned to. Each example contains an input string and its corresponding category label.

Methods:

  • from_polars

    Create collection from a Polars DataFrame. Must have an 'output' column and an 'input' column.

Source code in src/fenic/core/types/semantic_examples.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(self, examples: List[ExampleType] = None):
    """Initialize a collection of semantic examples.

    Args:
        examples: Optional list of examples to add to the collection. Each example
            will be processed through create_example() to ensure proper formatting
            and validation.

    Note:
        The examples list is initialized as empty if no examples are provided.
        Each example in the provided list will be processed through create_example()
        to ensure proper formatting and validation.
    """
    self.examples: List[ExampleType] = []
    if examples:
        for example in examples:
            self.create_example(example)

from_polars classmethod

from_polars(df: DataFrame) -> ClassifyExampleCollection

Create collection from a Polars DataFrame. Must have an 'output' column and an 'input' column.

Source code in src/fenic/core/types/semantic_examples.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
@classmethod
def from_polars(cls, df: pl.DataFrame) -> ClassifyExampleCollection:
    """Create collection from a Polars DataFrame. Must have an 'output' column and an 'input' column."""
    collection = cls()

    if EXAMPLE_INPUT_KEY not in df.columns:
        raise InvalidExampleCollectionError(
            f"Classify Examples DataFrame missing required '{EXAMPLE_INPUT_KEY}' column"
        )
    if EXAMPLE_OUTPUT_KEY not in df.columns:
        raise InvalidExampleCollectionError(
            f"Classify Examples DataFrame missing required '{EXAMPLE_OUTPUT_KEY}' column"
        )

    for row in df.iter_rows(named=True):
        if row[EXAMPLE_INPUT_KEY] is None:
            raise InvalidExampleCollectionError(
                f"Classify Examples DataFrame contains null values in '{EXAMPLE_INPUT_KEY}' column"
            )
        if row[EXAMPLE_OUTPUT_KEY] is None:
            raise InvalidExampleCollectionError(
                f"Classify Examples DataFrame contains null values in '{EXAMPLE_OUTPUT_KEY}' column"
            )

        example = ClassifyExample(
            input=row[EXAMPLE_INPUT_KEY],
            output=row[EXAMPLE_OUTPUT_KEY],
        )
        collection.create_example(example)

    return collection

JoinExample

Bases: BaseModel

A single semantic example for semantic join operations.

Join examples demonstrate the evaluation of two input variables across different datasets against a specific condition, used in a semantic.join operation.

JoinExampleCollection

JoinExampleCollection(examples: List[JoinExample] = None)

Bases: BaseExampleCollection[JoinExample]

Collection of comparison examples for semantic join operations.

Stores examples showing which pairs of values should be considered matches for joining data. Each example contains a left value, right value, and boolean output indicating whether they match.

Initialize a collection of semantic join examples.

Parameters:

  • examples (List[JoinExample], default: None ) –

    List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.

Methods:

  • create_example

    Create an example in the collection with type validation.

  • from_polars

    Create collection from a Polars DataFrame. Must have 'left_on', 'right_on', and 'output' columns.

Source code in src/fenic/core/types/semantic_examples.py
566
567
568
569
570
571
572
573
574
575
def __init__(self, examples: List[JoinExample] = None):
    """Initialize a collection of semantic join examples.

    Args:
        examples: List of examples to add to the collection. Each example
            will be processed through create_example() to ensure proper formatting
            and validation.
    """
    self._type_validator = _ExampleTypeValidator()
    super().__init__(examples)

create_example

create_example(example: JoinExample) -> JoinExampleCollection

Create an example in the collection with type validation.

Validates that left_on and right_on values have consistent types across examples. The first example establishes the types and cannot have None values. Subsequent examples must have matching types but can have None values.

Parameters:

Returns:

Raises:

  • InvalidExampleCollectionError

    If the example type is wrong, if the first example contains None values, or if subsequent examples have type mismatches.

Source code in src/fenic/core/types/semantic_examples.py
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
def create_example(self, example: JoinExample) -> JoinExampleCollection:
    """Create an example in the collection with type validation.

    Validates that left_on and right_on values have consistent types across
    examples. The first example establishes the types and cannot have None values.
    Subsequent examples must have matching types but can have None values.

    Args:
        example: The JoinExample to add.

    Returns:
        Self for method chaining.

    Raises:
        InvalidExampleCollectionError: If the example type is wrong, if the
            first example contains None values, or if subsequent examples
            have type mismatches.
    """
    if not isinstance(example, JoinExample):
        raise InvalidExampleCollectionError(
            f"Expected example of type {JoinExample.__name__}, got {type(example).__name__}"
        )

    # Convert to dict format for validation
    example_dict = {
        LEFT_ON_KEY: example.left_on,
        RIGHT_ON_KEY: example.right_on
    }

    example_num = len(self.examples) + 1
    self._type_validator.process_example(example_dict, example_num)

    self.examples.append(example)
    return self

from_polars classmethod

from_polars(df: DataFrame) -> JoinExampleCollection

Create collection from a Polars DataFrame. Must have 'left_on', 'right_on', and 'output' columns.

Source code in src/fenic/core/types/semantic_examples.py
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
@classmethod
def from_polars(cls, df: pl.DataFrame) -> JoinExampleCollection:
    """Create collection from a Polars DataFrame. Must have 'left_on', 'right_on', and 'output' columns."""
    collection = cls()

    required_columns = [
        LEFT_ON_KEY,
        RIGHT_ON_KEY,
        EXAMPLE_OUTPUT_KEY,
    ]
    for col in required_columns:
        if col not in df.columns:
            raise InvalidExampleCollectionError(
                f"Join Examples DataFrame missing required '{col}' column"
            )

    for row in df.iter_rows(named=True):
        for col in required_columns:
            if row[col] is None:
                raise InvalidExampleCollectionError(
                    f"Join Examples DataFrame contains null values in '{col}' column"
                )

        example = JoinExample(
            left_on=row[LEFT_ON_KEY],
            right_on=row[RIGHT_ON_KEY],
            output=row[EXAMPLE_OUTPUT_KEY],
        )
        collection.create_example(example)

    return collection

MapExample

Bases: BaseModel

A single semantic example for semantic mapping operations.

Map examples demonstrate the transformation of input variables to a specific output string or structured model used in a semantic.map operation.

MapExampleCollection

MapExampleCollection(examples: List[MapExample] = None)

Bases: BaseExampleCollection[MapExample]

Collection of input-output examples for semantic map operations.

Stores examples that demonstrate how input data should be transformed into output text or structured data. Each example shows the expected output for a given set of input fields.

Initialize a collection of semantic map examples.

Parameters:

  • examples (List[MapExample], default: None ) –

    List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.

Methods:

  • create_example

    Create an example in the collection with output and input type validation.

  • from_polars

    Create collection from a Polars DataFrame. Must have an 'output' column and at least one input column.

Source code in src/fenic/core/types/semantic_examples.py
258
259
260
261
262
263
264
265
266
267
def __init__(self, examples: List[MapExample] = None):
    """Initialize a collection of semantic map examples.

    Args:
        examples: List of examples to add to the collection. Each example
            will be processed through create_example() to ensure proper formatting
            and validation.
    """
    self._type_validator = _ExampleTypeValidator()
    super().__init__(examples)

create_example

create_example(example: MapExample) -> MapExampleCollection

Create an example in the collection with output and input type validation.

Ensures all examples in the collection have consistent output types (either all strings or all BaseModel instances) and validates that input fields have consistent types across examples.

For input validation: - The first example establishes the schema and cannot have None values - Subsequent examples must have the same fields but can have None values - Non-None values must match the established type for each field

Parameters:

Returns:

Raises:

  • InvalidExampleCollectionError

    If the example output type doesn't match the existing examples in the collection, if the first example contains None values, or if subsequent examples have type mismatches.

Source code in src/fenic/core/types/semantic_examples.py
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
def create_example(self, example: MapExample) -> MapExampleCollection:
    """Create an example in the collection with output and input type validation.

    Ensures all examples in the collection have consistent output types
    (either all strings or all BaseModel instances) and validates that input
    fields have consistent types across examples.

    For input validation:
    - The first example establishes the schema and cannot have None values
    - Subsequent examples must have the same fields but can have None values
    - Non-None values must match the established type for each field

    Args:
        example: The MapExample to add.

    Returns:
        Self for method chaining.

    Raises:
        InvalidExampleCollectionError: If the example output type doesn't match
            the existing examples in the collection, if the first example contains
            None values, or if subsequent examples have type mismatches.
    """
    if not isinstance(example, MapExample):
        raise InvalidExampleCollectionError(
            f"Expected example of type {MapExample.__name__}, got {type(example).__name__}"
        )

    # Validate output type consistency
    self._validate_single_example_output_type(example)

    # Validate input types
    example_num = len(self.examples) + 1
    self._type_validator.process_example(example.input, example_num)

    self.examples.append(example)
    return self

from_polars classmethod

from_polars(df: DataFrame) -> MapExampleCollection

Create collection from a Polars DataFrame. Must have an 'output' column and at least one input column.

Source code in src/fenic/core/types/semantic_examples.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
@classmethod
def from_polars(cls, df: pl.DataFrame) -> MapExampleCollection:
    """Create collection from a Polars DataFrame. Must have an 'output' column and at least one input column."""
    collection = cls()

    if EXAMPLE_OUTPUT_KEY not in df.columns:
        raise ValueError(
            f"Map Examples DataFrame missing required '{EXAMPLE_OUTPUT_KEY}' column"
        )

    input_cols = [col for col in df.columns if col != EXAMPLE_OUTPUT_KEY]

    if not input_cols:
        raise ValueError(
            "Map Examples DataFrame must have at least one input column"
        )

    for row in df.iter_rows(named=True):
        input_dict = {col: row[col] for col in input_cols}
        example = MapExample(input=input_dict, output=row[EXAMPLE_OUTPUT_KEY])
        collection.create_example(example)

    return collection

PredicateExample

Bases: BaseModel

A single semantic example for semantic predicate operations.

Predicate examples demonstrate the evaluation of input variables against a specific condition, used in a semantic.predicate operation.

PredicateExampleCollection

PredicateExampleCollection(examples: List[PredicateExample] = None)

Bases: BaseExampleCollection[PredicateExample]

Collection of input-to-boolean examples for predicate operations.

Stores examples showing which inputs should evaluate to True or False based on some condition. Each example contains input fields and a boolean output indicating whether the condition holds.

Initialize a collection of semantic predicate examples.

Parameters:

  • examples (List[PredicateExample], default: None ) –

    List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.

Methods:

  • create_example

    Create an example in the collection with input type validation.

  • from_polars

    Create collection from a Polars DataFrame.

Source code in src/fenic/core/types/semantic_examples.py
463
464
465
466
467
468
469
470
471
472
def __init__(self, examples: List[PredicateExample] = None):
    """Initialize a collection of semantic predicate examples.

    Args:
        examples: List of examples to add to the collection. Each example
            will be processed through create_example() to ensure proper formatting
            and validation.
    """
    self._type_validator = _ExampleTypeValidator()
    super().__init__(examples)

create_example

create_example(example: PredicateExample) -> PredicateExampleCollection

Create an example in the collection with input type validation.

Validates that input fields have consistent types across examples. The first example establishes the schema and cannot have None values. Subsequent examples must have the same fields but can have None values.

Parameters:

Returns:

Raises:

  • InvalidExampleCollectionError

    If the example type is wrong, if the first example contains None values, or if subsequent examples have type mismatches.

Source code in src/fenic/core/types/semantic_examples.py
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
def create_example(self, example: PredicateExample) -> PredicateExampleCollection:
    """Create an example in the collection with input type validation.

    Validates that input fields have consistent types across examples.
    The first example establishes the schema and cannot have None values.
    Subsequent examples must have the same fields but can have None values.

    Args:
        example: The PredicateExample to add.

    Returns:
        Self for method chaining.

    Raises:
        InvalidExampleCollectionError: If the example type is wrong, if the
            first example contains None values, or if subsequent examples
            have type mismatches.
    """
    if not isinstance(example, PredicateExample):
        raise InvalidExampleCollectionError(
            f"Expected example of type {PredicateExample.__name__}, got {type(example).__name__}"
        )

    # Validate input types
    example_num = len(self.examples) + 1
    self._type_validator.process_example(example.input, example_num)

    self.examples.append(example)
    return self

from_polars classmethod

from_polars(df: DataFrame) -> PredicateExampleCollection

Create collection from a Polars DataFrame.

Source code in src/fenic/core/types/semantic_examples.py
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
@classmethod
def from_polars(cls, df: pl.DataFrame) -> PredicateExampleCollection:
    """Create collection from a Polars DataFrame."""
    collection = cls()

    # Validate output column exists
    if EXAMPLE_OUTPUT_KEY not in df.columns:
        raise InvalidExampleCollectionError(
            f"Predicate Examples DataFrame missing required '{EXAMPLE_OUTPUT_KEY}' column"
        )

    input_cols = [col for col in df.columns if col != EXAMPLE_OUTPUT_KEY]

    if not input_cols:
        raise InvalidExampleCollectionError(
            "Predicate Examples DataFrame must have at least one input column"
        )

    for row in df.iter_rows(named=True):
        if row[EXAMPLE_OUTPUT_KEY] is None:
            raise InvalidExampleCollectionError(
                f"Predicate Examples DataFrame contains null values in '{EXAMPLE_OUTPUT_KEY}' column"
            )

        input_dict = {col: row[col] for col in input_cols if row[col] is not None}

        example = PredicateExample(input=input_dict, output=row[EXAMPLE_OUTPUT_KEY])
        collection.create_example(example)

    return collection