Utilities for pipelines

Transformers documentation

Natural Language Processing

Performance and scalability

Reinforcement learning models

This page lists all the utility functions the library provides for pipelines.

Most of those are only useful if you are studying the code of the models in the library.

## [](#transformers.pipelines.ArgumentHandler)Argument handling

### class transformers.pipelines.ArgumentHandler

[](#transformers.pipelines.ArgumentHandler)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L418)

( )

Base interface for handling arguments for each [Pipeline](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.Pipeline).

### class transformers.pipelines.ZeroShotClassificationArgumentHandler

[](#transformers.pipelines.ZeroShotClassificationArgumentHandler)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/zero_shot_classification.py#L13)

( )

Handles arguments for zero-shot for text classification by turning each possible label into an NLI premise/hypothesis pair.

### class transformers.pipelines.QuestionAnsweringArgumentHandler

[](#transformers.pipelines.QuestionAnsweringArgumentHandler)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/question_answering.py#L149)

( )

QuestionAnsweringPipeline requires the user to provide multiple arguments (i.e. question & context) to be mapped to internal `SquadExample`.

QuestionAnsweringArgumentHandler manages all the possible to create a `SquadExample` from the command-line supplied arguments.

## [](#transformers.PipelineDataFormat)Data format

### class transformers.PipelineDataFormat

[](#transformers.PipelineDataFormat)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L428)

( output\_path: typing.Optional\[str\]input\_path: typing.Optional\[str\]column: typing.Optional\[str\]overwrite: bool = False )

Parameters

-   [](#transformers.PipelineDataFormat.output_path)**output\_path** (`str`, _optional_) — Where to save the outgoing data.
-   [](#transformers.PipelineDataFormat.input_path)**input\_path** (`str`, _optional_) — Where to look for the input data.
-   [](#transformers.PipelineDataFormat.column)**column** (`str`, _optional_) — The column to read.
-   [](#transformers.PipelineDataFormat.overwrite)**overwrite** (`bool`, _optional_, defaults to `False`) — Whether or not to overwrite the `output_path`.

Base class for all the pipeline supported data format both for reading and writing. Supported data formats currently includes:

-   JSON
-   CSV
-   stdin/stdout (pipe)

`PipelineDataFormat` also includes some utilities to work with multi-columns like mapping from datasets columns to pipelines keyword arguments through the `dataset_kwarg_1=dataset_column_1` format.

#### from\_str

[](#transformers.PipelineDataFormat.from_str)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L505)

( format: stroutput\_path: typing.Optional\[str\]input\_path: typing.Optional\[str\]column: typing.Optional\[str\]overwrite = False ) → [PipelineDataFormat](/docs/transformers/v4.30.0/en/internal/pipelines_utils#transformers.PipelineDataFormat)

Parameters

-   [](#transformers.PipelineDataFormat.from_str.format)**format** (`str`) — The format of the desired pipeline. Acceptable values are `"json"`, `"csv"` or `"pipe"`.
-   [](#transformers.PipelineDataFormat.from_str.output_path)**output\_path** (`str`, _optional_) — Where to save the outgoing data.
-   [](#transformers.PipelineDataFormat.from_str.input_path)**input\_path** (`str`, _optional_) — Where to look for the input data.
-   [](#transformers.PipelineDataFormat.from_str.column)**column** (`str`, _optional_) — The column to read.
-   [](#transformers.PipelineDataFormat.from_str.overwrite)**overwrite** (`bool`, _optional_, defaults to `False`) — Whether or not to overwrite the `output_path`.

The proper data format.

Creates an instance of the right subclass of [PipelineDataFormat](/docs/transformers/v4.30.0/en/internal/pipelines_utils#transformers.PipelineDataFormat) depending on `format`.

#### save

[](#transformers.PipelineDataFormat.save)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L477)

( data: typing.Union\[dict, typing.List\[dict\]\] )

Parameters

-   [](#transformers.PipelineDataFormat.save.data)**data** (`dict` or list of `dict`) — The data to store.

Save the provided data object with the representation for the current [PipelineDataFormat](/docs/transformers/v4.30.0/en/internal/pipelines_utils#transformers.PipelineDataFormat).

#### save\_binary

[](#transformers.PipelineDataFormat.save_binary)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L487)

( data: typing.Union\[dict, typing.List\[dict\]\] ) → `str`

Parameters

-   [](#transformers.PipelineDataFormat.save_binary.data)**data** (`dict` or list of `dict`) — The data to store.

Path where the data has been saved.

Save the provided data object as a pickle-formatted binary data on the disk.

### class transformers.CsvPipelineDataFormat

[](#transformers.CsvPipelineDataFormat)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L541)

( output\_path: typing.Optional\[str\]input\_path: typing.Optional\[str\]column: typing.Optional\[str\]overwrite = False )

Parameters

-   [](#transformers.CsvPipelineDataFormat.output_path)**output\_path** (`str`, _optional_) — Where to save the outgoing data.
-   [](#transformers.CsvPipelineDataFormat.input_path)**input\_path** (`str`, _optional_) — Where to look for the input data.
-   [](#transformers.CsvPipelineDataFormat.column)**column** (`str`, _optional_) — The column to read.
-   [](#transformers.CsvPipelineDataFormat.overwrite)**overwrite** (`bool`, _optional_, defaults to `False`) — Whether or not to overwrite the `output_path`.

Support for pipelines using CSV data format.

#### save

[](#transformers.CsvPipelineDataFormat.save)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L571)

( data: typing.List\[dict\] )

Parameters

-   [](#transformers.CsvPipelineDataFormat.save.data)**data** (`List[dict]`) — The data to store.

Save the provided data object with the representation for the current [PipelineDataFormat](/docs/transformers/v4.30.0/en/internal/pipelines_utils#transformers.PipelineDataFormat).

### class transformers.JsonPipelineDataFormat

[](#transformers.JsonPipelineDataFormat)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L585)

( output\_path: typing.Optional\[str\]input\_path: typing.Optional\[str\]column: typing.Optional\[str\]overwrite = False )

Parameters

-   [](#transformers.JsonPipelineDataFormat.output_path)**output\_path** (`str`, _optional_) — Where to save the outgoing data.
-   [](#transformers.JsonPipelineDataFormat.input_path)**input\_path** (`str`, _optional_) — Where to look for the input data.
-   [](#transformers.JsonPipelineDataFormat.column)**column** (`str`, _optional_) — The column to read.
-   [](#transformers.JsonPipelineDataFormat.overwrite)**overwrite** (`bool`, _optional_, defaults to `False`) — Whether or not to overwrite the `output_path`.

Support for pipelines using JSON file format.

#### save

[](#transformers.JsonPipelineDataFormat.save)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L616)

( data: dict )

Parameters

-   [](#transformers.JsonPipelineDataFormat.save.data)**data** (`dict`) — The data to store.

Save the provided data object in a json file.

### class transformers.PipedPipelineDataFormat

[](#transformers.PipedPipelineDataFormat)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L627)

( output\_path: typing.Optional\[str\]input\_path: typing.Optional\[str\]column: typing.Optional\[str\]overwrite: bool = False )

Parameters

-   [](#transformers.PipedPipelineDataFormat.output_path)**output\_path** (`str`, _optional_) — Where to save the outgoing data.
-   [](#transformers.PipedPipelineDataFormat.input_path)**input\_path** (`str`, _optional_) — Where to look for the input data.
-   [](#transformers.PipedPipelineDataFormat.column)**column** (`str`, _optional_) — The column to read.
-   [](#transformers.PipedPipelineDataFormat.overwrite)**overwrite** (`bool`, _optional_, defaults to `False`) — Whether or not to overwrite the `output_path`.

Read data from piped input to the python process. For multi columns data, columns should separated by

If columns are provided, then the output will be a dictionary with {column\_x: value\_x}

#### save

[](#transformers.PipedPipelineDataFormat.save)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L656)

( data: dict )

Parameters

-   [](#transformers.PipedPipelineDataFormat.save.data)**data** (`dict`) — The data to store.

Print the data.

## [](#transformers.pipelines.PipelineException)Utilities

### class transformers.pipelines.PipelineException

[](#transformers.pipelines.PipelineException)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/pipelines/base.py#L401)

( task: strmodel: strreason: str )

Parameters

-   [](#transformers.pipelines.PipelineException.task)**task** (`str`) — The task of the pipeline.
-   [](#transformers.pipelines.PipelineException.model)**model** (`str`) — The model used by the pipeline.
-   [](#transformers.pipelines.PipelineException.reason)**reason** (`str`) — The error message to display.

Raised by a [Pipeline](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.Pipeline) when handling **call**.