> For the complete documentation index, see [llms.txt](https://gilad-rubin.gitbook.io/hypster/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gilad-rubin.gitbook.io/hypster/integrations/haystack.md).

# Haystack

Haystack pipelines often have swappable retrievers, rankers, prompts, and generation models. Hypster can select those pieces before you build or run the pipeline.

## Shape

{% code overflow="wrap" %}

```python
from hypster import HP, instantiate_with_params

def retrieval_config(hp: HP):
    retrieval = hp.select(
        {
            "keyword": {"type": "bm25", "index": "docs"},
            "vector": {"type": "embedding", "index": "docs-embeddings"},
            "hybrid": {"type": "hybrid", "keyword_weight": 0.35},
        },
        name="kind",
        default="hybrid",
        options_only=True,
    )
    return retrieval

def haystack_pipeline_config(hp: HP):
    retrieval = hp.nest(retrieval_config, name="retrieval")
    top_k = hp.int(8, name="top_k", min=1, max=50)
    rerank = hp.bool(True, name="rerank")
    answer_style = hp.select(
        ["brief", "sourced"],
        name="answer_style",
        default="sourced",
        options_only=True,
    )

    return {
        "retrieval": retrieval,
        "top_k": top_k,
        "rerank": rerank,
        "answer_style": answer_style,
    }

run = instantiate_with_params(
    haystack_pipeline_config,
    values={"retrieval.kind": "vector", "top_k": 12},
)

# pipeline = build_haystack_pipeline(run.value)
# tracker.log_params(run.params)
```

{% endcode %}

## Build The Pipeline After Instantiation

Keep Hypster configs focused on replayable settings. Build Haystack components after `instantiate_with_params()` so exploration and UI generation do not open indexes, clients, or network connections.

{% code overflow="wrap" %}

```python
def build_haystack_pipeline(settings):
    retrieval = settings["retrieval"]

    if retrieval["type"] == "bm25":
        retriever = build_bm25_retriever(index=retrieval["index"], top_k=settings["top_k"])
    elif retrieval["type"] == "embedding":
        retriever = build_embedding_retriever(index=retrieval["index"], top_k=settings["top_k"])
    else:
        retriever = build_hybrid_retriever(
            keyword_weight=retrieval["keyword_weight"],
            top_k=settings["top_k"],
        )

    pipeline = make_pipeline(
        retriever=retriever,
        rerank=settings["rerank"],
        answer_style=settings["answer_style"],
    )
    return pipeline


run = instantiate_with_params(
    haystack_pipeline_config,
    values={"retrieval.kind": "hybrid", "answer_style": "sourced"},
)

pipeline = build_haystack_pipeline(run.value)
tracker.log_params(run.params)
```

{% endcode %}

If your components are cheap pure-Python objects, a config can return factories or initialized components. For retrievers, indexes, remote LLM clients, and pipelines that allocate resources, prefer returning lightweight settings and building the Haystack pipeline outside the config function.

Hypster does not ship a Haystack-specific adapter today. Use this pattern when you want replayable parameter selection around an existing Haystack builder.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gilad-rubin.gitbook.io/hypster/integrations/haystack.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
