# Semantic conventions for generative AI retrieval **Status**: [Development][DocumentStatus] - [Span: `gen_ai.retrieval`](#span-gen_airetrieval) - [Metrics](#metrics) - [Metric: `gen_ai.retrieval.duration`](#metric-gen_airetrievalduration) This document defines semantic conventions for retrieval operations performed by GenAI frameworks or applications, including spans and metrics. Examples of retrieval operations include: - [LangChain retrievers](https://docs.langchain.com/oss/python/integrations/retrievers) - [LlamaIndex retrievers](https://developers.llamaindex.ai/python/framework/module_guides/querying/retriever/) - [Haystack retrievers](https://docs.haystack.deepset.ai/docs/retrievers) ## Span: `gen_ai.retrieval` **Status:** ![Development](https://img.shields.io/badge/-development-blue) Describes a retrieval operation - an execution of a framework retrieval pipeline that returns documents relevant to a query. **Span name** SHOULD be `{gen_ai.operation.name}`. **Span kind** SHOULD be `INTERNAL`. **Span status** SHOULD follow the [Recording Errors](https://github.com/open-telemetry/semantic-conventions/blob/v1.45.0/docs/general/recording-errors.md) document. **Entity Associations** The span SHOULD be associated with one of the following entities: - [`gen_ai.main_agent`](/docs/registry/entities/gen-ai.md#gen-ai-main-agent) **Requirement level:** [Recommended](https://github.com/open-telemetry/semantic-conventions/blob/v1.45.0/docs/general/signal-requirement-level.md). **Attributes:** | Key | Stability | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Value Type | Description | Example Values | | --- | --- | --- | --- | --- | --- | | [`gen_ai.operation.name`](/docs/registry/attributes/gen-ai.md) | ![Development](https://img.shields.io/badge/-development-blue) | `Required` | string | The name of the operation being performed. [1] | `retrieval` | | [`error.type`](https://github.com/open-telemetry/semantic-conventions/blob/v1.45.0/docs/registry/attributes/error.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If the operation ended in an error. | string | Describes a class of error the operation ended with. [2] | `timeout`; `java.net.UnknownHostException`; `server_certificate_invalid`; `500` | | [`gen_ai.retrieval.top_k`](/docs/registry/attributes/gen-ai.md) | ![Development](https://img.shields.io/badge/-development-blue) | `Recommended` | int | The maximum number of documents the retriever was asked to return for the query (also known as `k`, `limit`, or `max_num_results`). | `5` | | [`gen_ai.retrieval.documents`](/docs/registry/attributes/gen-ai.md) | ![Development](https://img.shields.io/badge/-development-blue) | `Opt-In` | any | The documents retrieved. [3] | [
  {
    "id": "doc_123",
    "score": 0.95
  },
  {
    "id": "doc_456",
    "score": 0.87
  },
  {
    "id": "doc_789",
    "score": 0.82
  }
] | | [`gen_ai.retrieval.query.text`](/docs/registry/attributes/gen-ai.md) | ![Development](https://img.shields.io/badge/-development-blue) | `Opt-In` | string | The query text used for retrieval. [4] | `What is the capital of France?`; `weather in Paris` | **[1] `gen_ai.operation.name`:** SHOULD be `retrieval`. **[2] `error.type`:** The `error.type` SHOULD match the error code returned by the Generative AI provider or the client library, the canonical name of exception that occurred, or another low-cardinality error identifier. Instrumentations SHOULD document the list of errors they report. **[3] `gen_ai.retrieval.documents`:** Each document object SHOULD contain the following properties when available: `id` (string): A unique identifier for the document, `score` (double): The relevance score of the document This attribute MUST be recorded as an [AnyValue](https://opentelemetry.io/docs/specs/otel/common/#anyvalue) conforming to its [JSON schema](/model/gen-ai/gen-ai-retrieval-documents.json). **[4] `gen_ai.retrieval.query.text`:** > [!Warning] > This attribute may contain sensitive information. The following attributes can be important for making sampling decisions and SHOULD be provided **at span creation time** (if provided at all): * [`gen_ai.operation.name`](/docs/registry/attributes/gen-ai.md) --- `error.type` has the following list of well-known values. If one of them applies, then the respective value MUST be used; otherwise, a custom value MAY be used. | Value | Description | Stability | | --- | --- | --- | | `_OTHER` | A fallback error value to be used when the instrumentation doesn't define a custom value. | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | ## Metrics ### Metric: `gen_ai.retrieval.duration` This metric SHOULD be specified with [ExplicitBucketBoundaries] of [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60]. | Name | Instrument Type | Unit (UCUM) | Description | Stability | Entity Associations | | -------- | --------------- | ----------- | -------------- | --------- | ------ | | `gen_ai.retrieval.duration` | Histogram | `s` | The duration of a retrieval operation - an execution of a framework retrieval pipeline that returns documents relevant to a query. [1] | ![Development](https://img.shields.io/badge/-development-blue) | | **[1]:** Measured from when the retriever receives the query to when it returns documents or ends with an error. When this metric is reported alongside a `gen_ai.retrieval` span, the metric value SHOULD be the same as the span duration. **Requirement level:** [Recommended](https://github.com/open-telemetry/semantic-conventions/blob/v1.45.0/docs/general/signal-requirement-level.md). **Attributes:** | Key | Stability | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Value Type | Description | Example Values | | --- | --- | --- | --- | --- | --- | | [`gen_ai.operation.name`](/docs/registry/attributes/gen-ai.md) | ![Development](https://img.shields.io/badge/-development-blue) | `Required` | string | The name of the operation being performed. [1] | `retrieval` | | [`error.type`](https://github.com/open-telemetry/semantic-conventions/blob/v1.45.0/docs/registry/attributes/error.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If the operation ended in an error. | string | Describes a class of error the operation ended with. [2] | `timeout`; `java.net.UnknownHostException`; `server_certificate_invalid`; `500` | **[1] `gen_ai.operation.name`:** SHOULD be `retrieval`. **[2] `error.type`:** The `error.type` SHOULD match the error code returned by the Generative AI provider or the client library, the canonical name of exception that occurred, or another low-cardinality error identifier. Instrumentations SHOULD document the list of errors they report. --- `error.type` has the following list of well-known values. If one of them applies, then the respective value MUST be used; otherwise, a custom value MAY be used. | Value | Description | Stability | | --- | --- | --- | | `_OTHER` | A fallback error value to be used when the instrumentation doesn't define a custom value. | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | [DocumentStatus]: https://opentelemetry.io/docs/specs/otel/document-status [ExplicitBucketBoundaries]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.55.0/specification/metrics/api.md#instrument-advisory-parameters