---
title: "FunctionModel"
sidebar_label: "FunctionModel"
description: "Kotlin binding reference: FunctionModel."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

//[clika-runtime](../../../index.md)/[io.clika.runtime](../index.md)/[FunctionModel](index.md)

# FunctionModel

[common]\
class [FunctionModel](index.md)

A serving model whose phases are Kotlin bodies: declare the contract, bind each phase (and the state factory of a stateful model), then hand it to an [Executor](../Executor/index.md) or a [Pipeline](../Pipeline/index.md). A phase bound after an executor or a pipeline was created over the model is not one it runs. `AutoCloseable`; [close](close.md) refuses while an executor or a pipeline holds the model.

```kotlin
val model = FunctionModel(ModelSchema(inputs = listOf(TensorSpec("x")), outputs = listOf(TensorSpec("y"))))
model.onRunOnce("run") { ctx -> ctx.input("x").use { x -> Ops.mul(x, 2.0).use { y -> ctx.setOutput("y", y) } } }
```

## Constructors

| | |
|---|---|
| [FunctionModel](FunctionModel.md) | [common]<br>constructor(schema: [ModelSchema](../ModelSchema/index.md))<br>A model carrying schema whose phases are bound as bodies. |

## Types

| Name | Summary |
|---|---|
| [Companion](Companion/index.md) | [common]<br>object [Companion](Companion/index.md) |

## Properties

| Name | Summary |
|---|---|
| [schema](schema.md) | [common]<br>val [schema](schema.md): [ModelSchema](../ModelSchema/index.md) |

## Functions

| Name | Summary |
|---|---|
| [close](close.md) | [common]<br>open fun [close](close.md)() |
| [onIterate](onIterate.md) | [common]<br>fun [onIterate](onIterate.md)(name: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), body: [IterateBody](../IterateBody/index.md)): [FunctionModel](index.md)<br>Bind the body of the iterate phase named [name](onIterate.md) (stepped over the active batch until each session finishes). Returns this for chaining. |
| [onRunOnce](onRunOnce.md) | [common]<br>fun [onRunOnce](onRunOnce.md)(name: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), body: [RunOnceBody](../RunOnceBody/index.md)): [FunctionModel](index.md)<br>Bind the body of the run-once phase named [name](onRunOnce.md) (a single pass per session). Returns this for chaining. |
| [withState](withState.md) | [common]<br>fun [withState](withState.md)(factory: [StateFactory](../StateFactory/index.md)): [FunctionModel](index.md)<br>Set the per-session state factory (a stateful model only). Returns this for chaining. |