> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Haystack Instrumentation

> Instrument Haystack pipelines with LangWatch using community OpenTelemetry instrumentors.

LangWatch integrates with Haystack through OpenInference instrumentation to capture traces from your Haystack pipelines and components.

## Installation

```bash pip theme={null}
pip install langwatch openinference-instrumentation-haystack haystack-ai
```

```bash uv theme={null}
uv add langwatch openinference-instrumentation-haystack haystack-ai
```

## Usage

> [!NOTE]
>
> The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.

Use the OpenInference instrumentation for Haystack by passing `HaystackInstrumentor` to `langwatch.setup()`.

```python theme={null}
import os
import langwatch

from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from openinference.instrumentation.haystack import HaystackInstrumentor

langwatch.setup(instrumentors=[HaystackInstrumentor()])

basic_agent = Agent(
    chat_generator=OpenAIChatGenerator(model="gpt-6-luna"),
    system_prompt="You are a helpful web agent.",
    tools=[],
)

result = basic_agent.run(messages=[ChatMessage.from_user("Tell me a joke")])

print(result["last_message"].text)
```

The `HaystackInstrumentor` automatically captures Haystack pipeline operations, component executions, and model interactions. Use `@langwatch.trace()` to create a parent trace under which Haystack operations will be nested.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds. Open it to see the Haystack pipeline, its components and the model calls as spans.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the script runs in.

## Related

* [Capturing RAG](https://langwatch.ai/docs/integration/python/tutorials/capturing-rag.md) - Learn how to capture RAG data from retrievers and tools
* [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata.md) - Add custom metadata and attributes to your traces and spans
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your Haystack applications


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.