Extra Handler#

Extra handlers are functions that are executed before or after a PipelineComponent.

class ComponentExtraHandler(**data)[source]#

Bases: BaseModel

Class, representing an extra handler for pipeline components.

A component extra handler is a set of functions, attached to pipeline component (before or after it). Extra handlers should execute supportive tasks (like time or resources measurement, minor data transformations). Extra handlers should NOT edit context or pipeline, use services for that purpose instead.

functions: List[ExtraHandlerFunction]#

A list or instance of ExtraHandlerFunction.

stage: ClassVar[ExtraHandlerType] = 'UNDEFINED'#

An ExtraHandlerType, specifying whether this handler will be executed before or after pipeline component.

timeout: Optional[float]#

(for asynchronous only!) Maximum component execution time (in seconds), if it exceeds this time, it is interrupted.

requested_async_flag: Optional[bool]#

Requested asynchronous property.

classmethod functions_validator(data)[source]#

Add support for initializing from a Callable or List[Callable]. Casts functions to list if it’s not already.

property calculated_async_flag: bool#
property asynchronous: bool#

Property, that indicates, whether this component extra handler is synchronous or asynchronous. It is calculated according to the following rules:

async _run_function(func, ctx, pipeline, component_info)[source]#
async _run(ctx, pipeline, component_info)[source]#

Method for executing one of the extra handler functions (before or after). If the function is not set, nothing happens.

Parameters:
  • ctx (Context) – current dialog context.

  • pipeline (Pipeline) – the current pipeline.

  • component_info (ServiceRuntimeInfo) – associated component’s info dictionary.

Returns:

None

async __call__(ctx, pipeline, component_info)[source]#

A method for calling pipeline components. It sets up timeout if this component is asynchronous and executes it using _run method.

Parameters:
  • ctx (Context) – (required) Current dialog Context.

  • pipeline (Pipeline) – This Pipeline.

Returns:

Context if this is a synchronous service or Awaitable if this is an asynchronous component or None.

property info_dict: dict#

Property for retrieving info dictionary about this extra handler.

Returns:

Info dict, containing its fields as well as its type. All not set fields there are replaced with None.

class BeforeHandler(**data)[source]#

Bases: ComponentExtraHandler

A handler for extra functions that are executed before the component’s main function.

Parameters:
  • functions (List[ExtraHandlerFunction]) – A list of callables that will be executed before the component’s main function.

  • timeout – Optional timeout for the execution of the extra functions, in seconds.

  • requested_async_flag – Optional flag that indicates whether the extra functions should be executed asynchronously. The default value of the flag is True if all the functions in this handler are asynchronous.

stage: ClassVar[ExtraHandlerType] = 'BEFORE'#

An ExtraHandlerType, specifying whether this handler will be executed before or after pipeline component.

class AfterHandler(**data)[source]#

Bases: ComponentExtraHandler

A handler for extra functions that are executed after the component’s main function.

Parameters:
  • functions (List[ExtraHandlerFunction]) – A list of callables that will be executed after the component’s main function.

  • timeout – Optional timeout for the execution of the extra functions, in seconds.

  • requested_async_flag – Optional flag that indicates whether the extra functions should be executed asynchronously. The default value of the flag is True if all the functions in this handler are asynchronous.

stage: ClassVar[ExtraHandlerType] = 'AFTER'#

An ExtraHandlerType, specifying whether this handler will be executed before or after pipeline component.

ComponentExtraHandlerInitTypes#

Types that ComponentExtraHandler can be validated from.

alias of Union[ComponentExtraHandler, typing.Annotated[dict, ‘dict following the ComponentExtraHandler data model’], Callable[Callable], List[Callable][List[Callable]]]