---
title: classify
---

# `marvin.fns.classify`

## Constants

### `DEFAULT_PROMPT`
```python
DEFAULT_PROMPT = "'\nYou are an expert classifier that always maintains as much semantic meaning\nas possible when labeling text. You use inference or deduction whenever\nnecessary to understand missing or omitted data. Classify the provided `data`,\ntext, or information as one of the provided labels. For boolean labels,\nconsider "truthy" or affirmative inputs to be "true".'"
```

### `PROMPT`
```python
PROMPT = DEFAULT_PROMPT
```

### `T`
```python
T = TypeVar('T')
```

## Functions

### `classify`
```python
def classify(data: Any, labels: Sequence[T] | type[T], multi_label: bool = False, instructions: str | None = None, agent: Agent | None = None, thread: Thread | str | None = None, context: dict[str, Any] | None = None, handlers: list[Handler | AsyncHandler] | None = None, prompt: str | None = None) -> T | list[T]
```
Classifies input data into one or more predefined labels using a language model.

This function uses a language model to analyze the input data and assign it to
the most appropriate label(s) from the provided sequence of labels or Enum class.

Args:
    data: The input data to classify. Can be any type.
    labels: Either a sequence of possible labels (of type T) or an Enum class to
        classify the data into. If an Enum class is provided, its values will be
        used as the labels.
    multi_label: If False (default), returns a single label. If True, returns
        multiple labels as a list.
    instructions: Optional additional instructions to guide the classification.
        Used to provide specific guidance about how to interpret or classify
        the data.
    agent: Optional custom agent to use for classification. If not provided,
        the default agent will be used.
    thread: Optional thread for maintaining conversation context. Can be
        either a Thread object or a string thread ID.
    context: Optional dictionary of additional context to include in the task.
    handlers: Optional list of handlers to use for the task.
Returns:
    - If labels is a Sequence[T]:
        - If multi_label is False: returns T
        - If multi_label is True: returns list[T]
    - If labels is an Enum class:
        - If multi_label is False: returns E (the Enum value)
        - If multi_label is True: returns list[E] (list of Enum values)

Examples:
    >>> # Using a sequence of labels
    >>> classify("red car", ["red", "blue", "green"])
    'red'

    >>> # Using an Enum class
    >>> class Colors(enum.Enum):
    ...     RED = "red"
    ...     BLUE = "blue"
    ...     GREEN = "green"
    >>> classify("red car", Colors)
    <Colors.RED: 'red'>

    >>> # Multi-label classification
    >>> classify("red and blue car", Colors, multi_label=True)
    [<Colors.RED: 'red'>, <Colors.BLUE: 'blue'>]

    >>> # Boolean classification
    >>> classify("2+2=4", bool)
    True

### `classify_async`
```python
def classify_async(data: Any, labels: Sequence[T] | type[T], multi_label: bool = False, instructions: str | None = None, agent: Agent | None = None, thread: Thread | str | None = None, context: dict[str, Any] | None = None, handlers: list[Handler | AsyncHandler] | None = None, prompt: str | None = None) -> T | list[T]
```
Asynchronously classifies input data into one or more predefined labels using a language model.

This function uses a language model to analyze the input data and assign it to
the most appropriate label(s) from the provided sequence of labels or Enum class.

Args:
    data: The input data to classify. Can be any type.
    labels: Either a sequence of possible labels (of type T) or an Enum class to
        classify the data into. If an Enum class is provided, its values will be
        used as the labels.
    multi_label: If False (default), returns a single label. If True, returns
        multiple labels as a list.
    instructions: Optional additional instructions to guide the classification.
        Used to provide specific guidance about how to interpret or classify
        the data.
    agent: Optional custom agent to use for classification. If not provided,
        the default agent will be used.
    thread: Optional thread for maintaining conversation context. Can be
        either a Thread object or a string thread ID.
    context: Optional dictionary of additional context to include in the task.
    prompt: Optional prompt to use for the task. If not provided, the default
        prompt will be used.
Returns:
    - If labels is a Sequence[T]:
        - If multi_label is False: returns T
        - If multi_label is True: returns list[T]
    - If labels is an Enum class:
        - If multi_label is False: returns E (the Enum value)
        - If multi_label is True: returns list[E] (list of Enum values)

Examples:
    >>> # Using a sequence of labels
    >>> await classify_async("red car", ["red", "blue", "green"])
    'red'

    >>> # Using an Enum class
    >>> class Colors(enum.Enum):
    ...     RED = "red"
    ...     BLUE = "blue"
    ...     GREEN = "green"
    >>> await classify_async("red car", Colors)
    <Colors.RED: 'red'>

    >>> # Multi-label classification
    >>> await classify_async("red and blue car", Colors, multi_label=True)
    [<Colors.RED: 'red'>, <Colors.BLUE: 'blue'>]

    >>> # Boolean classification
    >>> await classify_async("2+2=4", bool)
    True

---

**Parent Module:** [`fns`](marvin-fns)
