> For the complete documentation index, see [llms.txt](https://docs.tensorleap.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_metadata.md).

# @tensorleap\_metadata

code\_loader.inner\_leap\_binder.leapbinder\_decorators.tensorleap\_metadata

The `tensorleap_metadata` decorator registers a [**Metadata Function**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/metadata-function.md): a function that returns one value, or a dictionary of named values, describing a sample.

```python
@tensorleap_metadata(name='sample_info', metadata_type={'label': DatasetMetadataType.int, 'is_circle': DatasetMetadataType.boolean})
def sample_info(idx: int, preprocess: PreprocessResponse) -> Union[int, str, bool, float, Dict[str, Union[int, str, bool, float]]]:
    pass
```

| Args            |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`          | *(str)* The name of the metadata, e.g. `label`. Must be unique among metadata functions. When the function returns a dictionary, each key becomes its own metadata field.                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `metadata_type` | *(*[*DatasetMetadataType*](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/enums/datasetmetadatatype.md) *or Dict\[str,* [*DatasetMetadataType*](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/enums/datasetmetadatatype.md)*], optional)* The type of the value. For a function that returns a dictionary, a dictionary from key to type. When it is left out, the type is taken from the returned values. **Required** for any value that can be `None`, NaN or infinite: such a value has no type to infer, and the check fails with `Metadata <name> is None and no metadata type is provided`. |

#### Function inputs

Same as the [input encoder](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_input_encoder.md#function-inputs): a sample id (or a list of ids for a grouped response) and its `PreprocessResponse`.

#### Function output

One of `int`, `float`, `str`, `bool`, `None`, a NumPy scalar of those kinds, or a `dict` from `str` to any of these. For a grouped response, a list with one such value per sample id.

`None`, NaN and infinite values are treated as missing values.

### Examples

#### Basic Usage

```python
from typing import Dict, Union
from code_loader.contract.datasetclasses import PreprocessResponse
from code_loader.contract.enums import DatasetMetadataType
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_metadata


@tensorleap_metadata(name='sample_info',
                     metadata_type={'label': DatasetMetadataType.int, 'is_circle': DatasetMetadataType.boolean})
def sample_info(idx: str, preprocess: PreprocessResponse) -> Dict[str, Union[int, bool]]:
    row = preprocess.data.loc[idx]
    return {
        'label': int(row['label']),
        'is_circle': bool(row['is_circle']),
    }
```

Usage within the full script can be found at [**Dataset Script**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code.md#dataset-script).

#### Guides

Full examples can be found at the **Dataset Integration** section of the following guides:

* [**MNIST Guide**](https://docs.tensorleap.ai/guides/full-guides/mnist-guide.md)
* [**IMDB Guide**](https://docs.tensorleap.ai/guides/full-guides/imdb-guide.md)


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_metadata.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
