Creating Custom Components

Create your own components and use them standalone or in Pipelines.

With Haystack 2.0, you can easily create any custom components for various tasks, from filtering results to integrating with external software. You can then insert, reuse, and share these components within Haystack or even with an external audience by packaging them and submitting them to Haystack Integrations!

Here are the requirements for all custom components:

  • @component decorator. This will define that you are, in fact, creating a component.
  • run() method. This expects a set of input arguments and returns a dict object. You can pass values to the run() method of a component either when you run() a pipeline, or by using connect() to pass the outputs of one component to the inputs of another. An example of this is in the Extended Example below.
  • @component.output_types decorator. This will define the type of data your custom component will output and the name of the outputting edge. The names and types defined here should match correctly with the dict object that run() method returns.

Here is an example of the minimal component setup:

from haystack import component

@component
class WelcomeTextGenerator:
  """
  A component generating personal welcome message and making it upper case
  """
  @component.output_types(welcome_text=str, note=str)
  def run(self, name:str):
    return {"welcome_text": ('Hello {name}, welcome to Haystack!'.format(name=name)).upper(), "note": "welcome message is ready"}

Here, the custom component WelcomeTextGenerator accepts one input: name string and returns two outputs: welcome_note and note.

Extended Example

Click on the Recipe below to see how to create two custom components and connect them in a Haystack pipeline.