HaystackHaystack 3.1 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
19 small wins to finish your pathNext lesson

Components, pipelines and a first run

A component is a class with @component and a run method that returns a dict. A pipeline adds components by name and connects an output of one to an input of another.

Example
from haystack import Pipeline, component


@component
class Cleaner:
    @component.output_types(text=str)
    def run(self, text: str):
        return {"text": " ".join(text.split()).lower()}
Example
@component
class WordCounter:
    @component.output_types(count=int, words=list[str])
    def run(self, text: str):
        words = text.split()
        return {"count": len(words), "words": words}
Example
pipeline = Pipeline()
pipeline.add_component("cleaner", Cleaner())
pipeline.add_component("counter", WordCounter())
pipeline.connect("cleaner.text", "counter.text")

result = pipeline.run({"cleaner": {"text": "  Where is   MY parcel?  "}})
print(result)

@component.output_types names the outputs and their types; run's parameters are the inputs. connect("cleaner.text", "counter.text") sends Cleaner's text output into WordCounter's text input. run takes the inputs for the first component, keyed by its name.

The result holds the outputs no other component consumed: counter's count and words. cleaner.text went into counter, so it is not in the result.

Try it yourself
  • Run the Cleaner on its own: Cleaner().run(text=" A B ").
  • Add include_outputs_from={"cleaner"} to pipeline.run.
  • Print pipeline.inputs() and pipeline.outputs().

You understood something today that you didn't yesterday.