---
type: api
title: Create a trained model
description: Enable you to supply a trained model that is not created by data frame analytics.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/operation/operation-ml-put-trained-model
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/operation/operation-ml-put-trained-model
products:
  - Elasticsearch
---

# Create a trained model

Availability: 7.10+

## Paths

- `PUT` `/_ml/trained_models/{model_id}`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#bearerauth)

#### Path Parameters

- `model_id` (string Id)
  The unique identifier of the trained model.

## Description

Enable you to supply a trained model that is not created by data frame analytics.

## Required authorization

* Cluster privileges: `manage_ml`

## Query String Parameters

- `defer_definition_decompression` (boolean · default: false)
  If set to `true` and a `compressed_definition` is provided,
the request defers definition decompression and skips relevant
validations.
- `wait_for_completion` (boolean · default: false)
  Whether to wait for all child operations (e.g. model download)
to complete.

## Request Body

`application/json`

- `compressed_definition` (string) — optional
  The compressed (GZipped and Base64 encoded) inference definition of the
model. If compressed_definition is specified, then definition cannot be
specified.
- `definition` ({} Definition) — optional
  The inference definition for the model. If definition is specified, then
compressed_definition cannot be specified.
  - `preprocessors` ([] Preprocessor) — optional
    Collection of preprocessors
    - `frequency_encoding` ({} FrequencyEncodingPreprocessor) — optional
      - `field` (string) — required
      - `feature_name` (string) — required
      - `frequency_map` (map string to number) — required
    - `one_hot_encoding` ({} OneHotEncodingPreprocessor) — optional
      - `field` (string) — required
      - `hot_map` (map string to string) — required
    - `target_mean_encoding` ({} TargetMeanEncodingPreprocessor) — optional
      - `field` (string) — required
      - `feature_name` (string) — required
      - `target_map` (map string to number) — required
      - `default_value` (number) — required
  - `trained_model` ({} TrainedModel) — required
    The definition of the trained model.
    - `tree` ({} TrainedModelTree) — optional
      The definition for a binary decision tree.
      - `classification_labels` ([] string) — optional
      - `feature_names` ([] string) — required
      - `target_type` (string) — optional
      - `tree_structure` ([] TrainedModelTreeNode) — required
        - `decision_type` (string) — optional
        - `default_left` (boolean) — optional
        - `leaf_value` (number) — optional
        - `left_child` (number) — optional
        - `node_index` (number) — required
        - `right_child` (number) — optional
        - `split_feature` (number) — optional
        - `split_gain` (number) — optional
        - `threshold` (number) — optional
    - `tree_node` ({} TrainedModelTreeNode) — optional
      The definition of a node in a tree.
There are two major types of nodes: leaf nodes and not-leaf nodes.
- Leaf nodes only need node_index and leaf_value defined.
- All other nodes need split_feature, left_child, right_child, threshold, decision_type, and default_left defined.
      - `decision_type` (string) — optional
      - `default_left` (boolean) — optional
      - `leaf_value` (number) — optional
      - `left_child` (number) — optional
      - `node_index` (number) — required
      - `right_child` (number) — optional
      - `split_feature` (number) — optional
      - `split_gain` (number) — optional
      - `threshold` (number) — optional
    - `ensemble` ({} Ensemble) — optional
      The definition for an ensemble model
      - `aggregate_output` ({} AggregateOutput) — optional
        - `logistic_regression` ({} Weights) — optional
          - `weights` (number) — required
        - `weighted_sum` ({} Weights) — optional
          - `weights` (number) — required
        - `weighted_mode` ({} Weights) — optional
          - `weights` (number) — required
        - `exponent` ({} Weights) — optional
          - `weights` (number) — required
      - `classification_labels` ([] string) — optional
      - `feature_names` ([] string) — optional
      - `target_type` (string) — optional
      - `trained_models` ([] TrainedModel) — required — recursive
        Same fields as `trained_model` in `definition`
- `description` (string) — optional
  A human-readable description of the inference trained model.
- `inference_config` ({} InferenceConfigCreateContainer) — optional
  The default configuration for inference. This can be either a regression
or classification configuration. It must match the underlying
definition.trained_model's target_type. For pre-packaged models such as
ELSER the config is not required.
  - `regression` ({} RegressionInferenceOptions) — optional
    Regression configuration for inference.
    - `results_field` (string Field) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `num_top_feature_importance_values` (number · default: 0) — optional
      Specifies the maximum number of feature importance values per document.
  - `classification` ({} ClassificationInferenceOptions) — optional
    Classification configuration for inference.
    - `num_top_classes` (number) — optional
      Specifies the number of top class predictions to return. Defaults to 0.
    - `num_top_feature_importance_values` (number · default: 0) — optional
      Specifies the maximum number of feature importance values per document.
    - `prediction_field_type` (string) — optional
      Specifies the type of the predicted field to write. Acceptable values are: string, number, boolean. When boolean is provided 1.0 is transformed to true and 0.0 to false.
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `top_classes_results_field` (string) — optional
      Specifies the field to which the top classes are written. Defaults to top_classes.
  - `text_classification` ({} TextClassificationInferenceOptions) — optional
    Text classification configuration for inference.
    - `num_top_classes` (number) — optional
      Specifies the number of top class predictions to return. Defaults to 0.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options
      - `bert` ({} NlpBertTokenizationConfig) — optional
        Indicates BERT tokenization and its options
        - `do_lower_case` (boolean · default: false) — optional
          Should the tokenizer lower case the text
        - `max_sequence_length` (number · default: 512) — optional
          Maximum input sequence length for the model
        - `span` (number · default: -1) — optional
          Tokenization spanning options. Special value of -1 indicates no spanning takes place
        - `truncate` (enum TokenizationTruncate · default: first) — optional
          Should tokenization input be automatically truncated before sending to the model for inference
          Values: `first`, `second`, `none`
        - `with_special_tokens` (boolean · default: true) — optional
          Is tokenization completed with special tokens
      - `bert_ja` ({} NlpBertTokenizationConfig) — optional
        Indicates BERT Japanese tokenization and its options
        - `do_lower_case` (boolean · default: false) — optional
          Should the tokenizer lower case the text
        - `max_sequence_length` (number · default: 512) — optional
          Maximum input sequence length for the model
        - `span` (number · default: -1) — optional
          Tokenization spanning options. Special value of -1 indicates no spanning takes place
        - `truncate` (enum TokenizationTruncate · default: first) — optional
          Should tokenization input be automatically truncated before sending to the model for inference
          Values: `first`, `second`, `none`
        - `with_special_tokens` (boolean · default: true) — optional
          Is tokenization completed with special tokens
      - `mpnet` ({} NlpBertTokenizationConfig) — optional
        Indicates MPNET tokenization and its options
        - `do_lower_case` (boolean · default: false) — optional
          Should the tokenizer lower case the text
        - `max_sequence_length` (number · default: 512) — optional
          Maximum input sequence length for the model
        - `span` (number · default: -1) — optional
          Tokenization spanning options. Special value of -1 indicates no spanning takes place
        - `truncate` (enum TokenizationTruncate · default: first) — optional
          Should tokenization input be automatically truncated before sending to the model for inference
          Values: `first`, `second`, `none`
        - `with_special_tokens` (boolean · default: true) — optional
          Is tokenization completed with special tokens
      - `roberta` ({} NlpRobertaTokenizationConfig) — optional
        Indicates RoBERTa tokenization and its options
        - `do_lower_case` (boolean · default: false) — optional
          Should the tokenizer lower case the text
        - `max_sequence_length` (number · default: 512) — optional
          Maximum input sequence length for the model
        - `span` (number · default: -1) — optional
          Tokenization spanning options. Special value of -1 indicates no spanning takes place
        - `truncate` (enum TokenizationTruncate · default: first) — optional
          Should tokenization input be automatically truncated before sending to the model for inference
          Values: `first`, `second`, `none`
        - `with_special_tokens` (boolean · default: true) — optional
          Is tokenization completed with special tokens
        - `add_prefix_space` (boolean · default: false) — optional
          Should the tokenizer prefix input with a space character
      - `xlm_roberta` ({} XlmRobertaTokenizationConfig) — optional
        - `do_lower_case` (boolean · default: false) — optional
          Should the tokenizer lower case the text
        - `max_sequence_length` (number · default: 512) — optional
          Maximum input sequence length for the model
        - `span` (number · default: -1) — optional
          Tokenization spanning options. Special value of -1 indicates no spanning takes place
        - `truncate` (enum TokenizationTruncate · default: first) — optional
          Should tokenization input be automatically truncated before sending to the model for inference
          Values: `first`, `second`, `none`
        - `with_special_tokens` (boolean · default: true) — optional
          Is tokenization completed with special tokens
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `classification_labels` ([] string) — optional
      Classification labels to apply other than the stored labels. Must have the same deminsions as the default configured labels
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `zero_shot_classification` ({} ZeroShotClassificationInferenceOptions) — optional
    Zeroshot classification configuration for inference.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options to update when inferring
      Same fields as `tokenization` in `text_classification`
    - `hypothesis_template` (string · default: "This example is {}.") — optional
      Hypothesis template used when tokenizing labels for prediction
    - `classification_labels` ([] string) — required
      The zero shot classification labels indicating entailment, neutral, and contradiction
Must contain exactly and only entailment, neutral, and contradiction
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `multi_label` (boolean · default: false) — optional
      Indicates if more than one true label exists.
    - `labels` ([] string) — optional
      The labels to predict.
  - `fill_mask` ({} FillMaskInferenceOptions) — optional
    Fill mask configuration for inference.
    - `mask_token` (string) — optional
      The string/token which will be removed from incoming documents and replaced with the inference prediction(s).
In a response, this field contains the mask token for the specified model/tokenizer. Each model and tokenizer
has a predefined mask token which cannot be changed. Thus, it is recommended not to set this value in requests.
However, if this field is present in a request, its value must match the predefined value for that model/tokenizer,
otherwise the request will fail.
    - `num_top_classes` (number) — optional
      Specifies the number of top class predictions to return. Defaults to 0.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options to update when inferring
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `learning_to_rank` ({} LearningToRankConfig) — optional
    - `default_params` (map string to object) — optional
    - `feature_extractors` ([] string to FeatureExtractor) — optional
    - `num_top_feature_importance_values` (number) — required
  - `ner` ({} NerInferenceOptions) — optional
    Named entity recognition configuration for inference.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `classification_labels` ([] string) — optional
      The token classification labels. Must be IOB formatted tags
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `pass_through` ({} PassThroughInferenceOptions) — optional
    Pass through configuration for inference.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `text_embedding` ({} TextEmbeddingInferenceOptions) — optional
    Text embedding configuration for inference.
    - `embedding_size` (number) — optional
      The number of dimensions in the embedding output
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `text_expansion` ({} TextExpansionInferenceOptions) — optional
    Text expansion configuration for inference.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `vocabulary` ({} Vocabulary) — optional
      - `index` (string IndexName) — required
  - `question_answering` ({} QuestionAnsweringInferenceOptions) — optional
    Question answering configuration for inference.
    - `num_top_classes` (number) — optional
      Specifies the number of top class predictions to return. Defaults to 0.
    - `tokenization` ({} TokenizationConfigContainer) — optional
      The tokenization options to update when inferring
      Same fields as `tokenization` in `text_classification`
    - `results_field` (string) — optional
      The field that is added to incoming documents to contain the inference prediction. Defaults to predicted_value.
    - `max_answer_length` (number) — optional
      The maximum answer length to consider
- `input` ({} Input) — optional
  The input field names for the model definition.
  - `field_names` (string Names) — required
- `metadata` (object) — optional
  An object map that contains metadata about the model.
- `model_type` (enum TrainedModelType · default: tree_ensemble) — optional
  The model type.

Supported values include:
  - `tree_ensemble`: The model definition is an ensemble model of decision trees.
  - `lang_ident`: A special type reserved for language identification models.
  - `pytorch`: The stored definition is a PyTorch (specifically a TorchScript) model.
Currently only NLP models are supported.
  Values: `tree_ensemble`, `lang_ident`, `pytorch`
- `model_size_bytes` (number) — optional
  The estimated memory usage in bytes to keep the trained model in memory.
This property is supported only if defer_definition_decompression is true
or the model definition is not supplied.
- `platform_architecture` (string) — optional
  The platform architecture (if applicable) of the trained mode. If the model
only works on one platform, because it is heavily optimized for a particular
processor architecture and OS combination, then this field specifies which.
The format of the string must match the platform identifiers used by Elasticsearch,
so one of, `linux-x86_64`, `linux-aarch64`, `darwin-aarch64`,
or `windows-x86_64`. For portable models (those that work independent of processor
architecture or OS features), leave this field unset.
- `tags` ([] string) — optional
  An array of tags to organize the model.
- `prefix_strings` ({} TrainedModelPrefixStrings) — optional
  Optional prefix strings applied at inference
  - `ingest` (string) — optional
    String prepended to input at ingest
  - `search` (string) — optional
    String prepended to input at search

## Response

`application/json`

- `model_id` (string Id)
  Identifier for the trained model.
- `model_type` (enum TrainedModelType)
  The model type

Supported values include:
  - `tree_ensemble`: The model definition is an ensemble model of decision trees.
  - `lang_ident`: A special type reserved for language identification models.
  - `pytorch`: The stored definition is a PyTorch (specifically a TorchScript) model.
Currently only NLP models are supported.
  Values: `tree_ensemble`, `lang_ident`, `pytorch`
- `tags` ([] string)
  A comma delimited string of tags. A trained model can have many tags, or none.
- `version` (string)
  The Elasticsearch version number in which the trained model was created.
- `compressed_definition` (string)
- `created_by` (string)
  Information on the creator of the trained model.
- `create_time` (DateTime)
  The time when the trained model was created.
- `default_field_map` (map string to string)
  Any field map described in the inference configuration takes precedence.
- `description` (string)
  The free-text description of the trained model.
- `estimated_heap_memory_usage_bytes` (number)
  The estimated heap usage in bytes to keep the trained model in memory.
- `estimated_operations` (number)
  The estimated number of operations to use the trained model.
- `fully_defined` (boolean)
  True if the full model definition is present.
- `inference_config` ({} InferenceConfigCreateContainer)
  The default configuration for inference. This can be either a regression, classification, or one of the many NLP focused configurations. It must match the underlying definition.trained_model's target_type. For pre-packaged models such as ELSER the config is not required.
  Same fields as `inference_config`
- `input` ({} TrainedModelConfigInput)
  The input field names for the model definition.
  - `field_names` ([] string Field)
    An array of input field names for the model.
- `license_level` (string)
  The license level of the trained model.
- `metadata` ({} TrainedModelConfigMetadata)
  An object containing metadata about the trained model. For example, models created by data frame analytics contain analysis_config and input objects.
  - `model_aliases` ([] string)
  - `feature_importance_baseline` (map string to string)
    An object that contains the baseline for feature importance values. For regression analysis, it is a single value. For classification analysis, there is a value for each class.
  - `hyperparameters` ([] Hyperparameter)
    List of the available hyperparameters optimized during the fine_parameter_tuning phase as well as specified by the user.
    - `absolute_importance` (number)
      A positive number showing how much the parameter influences the variation of the loss function. For hyperparameters with values that are not specified by the user but tuned during hyperparameter optimization.
    - `name` (string Name)
      Name of the hyperparameter.
    - `relative_importance` (number)
      A number between 0 and 1 showing the proportion of influence on the variation of the loss function among all tuned hyperparameters. For hyperparameters with values that are not specified by the user but tuned during hyperparameter optimization.
    - `supplied` (boolean)
      Indicates if the hyperparameter is specified by the user (true) or optimized (false).
    - `value` (number)
      The value of the hyperparameter, either optimized or specified by the user.
  - `total_feature_importance` ([] TotalFeatureImportance)
    An array of the total feature importance for each feature used from the training data set. This array of objects is returned if data frame analytics trained the model and the request includes total_feature_importance in the include request parameter.
    - `feature_name` (string Name)
      The feature for which this importance was calculated.
    - `importance` ([] TotalFeatureImportanceStatistics)
      A collection of feature importance statistics related to the training data set for this particular feature.
      - `mean_magnitude` (number)
        The average magnitude of this feature across all the training data. This value is the average of the absolute values of the importance for this feature.
      - `max` (number)
        The maximum importance value across all the training data for this feature.
      - `min` (number)
        The minimum importance value across all the training data for this feature.
    - `classes` ([] TotalFeatureImportanceClass)
      If the trained model is a classification model, feature importance statistics are gathered per target class value.
      - `class_name` (string Name)
        The target class value. Could be a string, boolean, or number.
      - `importance` ([] TotalFeatureImportanceStatistics)
        A collection of feature importance statistics related to the training data set for this particular feature.
        - `mean_magnitude` (number)
          The average magnitude of this feature across all the training data. This value is the average of the absolute values of the importance for this feature.
        - `max` (number)
          The maximum importance value across all the training data for this feature.
        - `min` (number)
          The minimum importance value across all the training data for this feature.
- `model_size_bytes` (number | string ByteSize)
- `model_package` ({} ModelPackageConfig)
  - `create_time` (EpochTimeUnitMillis)
  - `description` (string)
  - `inference_config` (map string to object)
  - `metadata` (Metadata)
  - `minimum_version` (string)
  - `model_repository` (string)
  - `model_type` (string)
  - `packaged_model_id` (string Id)
  - `platform_architecture` (string)
  - `prefix_strings` ({} TrainedModelPrefixStrings)
    - `ingest` (string)
      String prepended to input at ingest
    - `search` (string)
      String prepended to input at search
  - `size` (number | string ByteSize)
  - `sha256` (string)
  - `tags` ([] string)
  - `vocabulary_file` (string)
- `location` ({} TrainedModelLocation)
  - `index` ({} TrainedModelLocationIndex)
    - `name` (string IndexName)
- `platform_architecture` (string)
- `prefix_strings` ({} TrainedModelPrefixStrings)
  - `ingest` (string)
    String prepended to input at ingest
  - `search` (string)
    String prepended to input at search
## Code Examples

#### Console

```console
PUT /_ml/trained_models/{model_id}
```

#### curl

```curl
curl -X PUT "/_ml/trained_models/{model_id}"
```

