> 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_integration_test.md).

# @tensorleap\_integration\_test

code\_loader.inner\_leap\_binder.leapbinder\_decorators.tensorleap\_integration\_test

The `tensorleap_integration_test` decorator registers the [integration test](https://docs.tensorleap.ai/tensorleap-integration/integration-test.md). The integration test does two jobs:

* **Locally**, it runs your integration functions on real samples, so errors show up before you push.
* **For the platform**, it describes how the functions connect: which encoder feeds which model input, and which tensors go into each loss, metric and visualizer.

```python
@tensorleap_integration_test()
def integration_test(idx: int, preprocess: PreprocessResponse) -> None:
    pass
```

#### Function inputs

|              |                                                                                                                                                                                                                   |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `idx`        | A sample id from `preprocess.sample_ids` (a list of ids for a [grouped](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/preprocessresponse.md#grouped-samples) response). |
| `preprocess` | A [PreprocessResponse](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/preprocessresponse.md) returned by the preprocess function.                                        |

The parameters can have any names.

#### What the function body may contain

Each call to the decorated function runs the body twice: once on the real sample, and once more with placeholders instead of data, to record how the functions connect. In the second run, decorated functions return placeholders, not arrays. So the body may only:

* call Tensorleap-decorated functions,
* call the model returned by [`@tensorleap_load_model`](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_load_model.md),
* index the model's predictions by position (`preds[0]`),
* call `visualize(...)` on a visualizer's result.

Arithmetic, NumPy calls, other libraries and other Python logic on the tensors fail with `Integration test is only allowed to call Tensorleap decorators`. Move that code into a decorated function.

Functions that take a [`SamplePreprocessResponse`](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/samplepreprocessresponse.md) argument must receive it explicitly inside the integration test, e.g. `my_loss(gt, preds, SamplePreprocessResponse(np.array([idx]), preprocess))`. On the platform it is filled in automatically.

After the body runs, the whole integration is checked (preprocess, every encoder and metadata function, simulations, and the [analysis configuration](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/leapanalysisconfiguration.md)).

### Examples

#### Basic Usage

```python
from code_loader.plot_functions.visualize import visualize
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_integration_test

# Your decorated integration functions
from leap_integration import (
    preprocess_func, input_encoder, gt_encoder,
    image_visualizer, my_metric, my_loss, load_model
)


@tensorleap_integration_test()
def integration_test(idx, preprocess):
    image = input_encoder(idx, preprocess)
    gt = gt_encoder(idx, preprocess)

    model = load_model()
    y_pred = model([image])

    img_vis = image_visualizer(image)
    visualize(img_vis)

    my_metric(gt, y_pred)
    my_loss(gt, y_pred)


if __name__ == '__main__':
    train, val, *_ = preprocess_func()
    for i in train.sample_ids[:3]:
        integration_test(i, train)
    for i in val.sample_ids[:3]:
        integration_test(i, val)
```


---

# 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_integration_test.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.
