---
title: "Completions"
method: POST
path: "/v1/completions"
tags: ["Container.Completions"]
---

# Completions

`POST /v1/completions`

## Request

- Base URL: `http://localhost:8000`
- URL: `http://localhost:8000/v1/completions`
- Auth: none declared
- TypeScript: <https://skmtc.dev/friendli/apis/friendli-suite-api-reference/docs/v1/completions/post.ts> (a function with typed inputs; its response is checked against the schema)

Generate text based on the given text prompt.

## Request body

- union
  - CompletionsBodyWithPrompt
    - `model` string, nullable: Routes the request to a specific adapter.
    - `bad_word_tokens` TokenSequence[], nullable: Same as the above `bad_words` field, but receives token sequences instead of text phrases. This is similar to Hugging Face's [`bad_word_ids`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.bad_words_ids) argument.
      - `tokens` integer[], nullable: A List of token IDs.
    - `bad_words` string[], nullable: Text phrases that should not be generated. For a bad word phrase that contains N tokens, if the first N-1 tokens appears at the last of the generated result, the logit for the last token of the phrase is set to -inf. Before checking whether a bard word is included in the result, the word is converted into tokens. We recommend using `bad_word_tokens` because it is clearer. For example, after tokenization, phrases "clear" and " clear" can result in different token sequences due to the prepended space character. Defaults to empty list.
    - `embedding_to_replace` number[], nullable: A list of flattened embedding vectors used for replacing the tokens at the specified indices provided via `token_index_to_replace`.
    - `encoder_no_repeat_ngram` integer, nullable: If this exceeds 1, every ngram of that size occurring in the input token sequence cannot appear in the generated result. 1 means that this mechanism is disabled (i.e., you cannot prevent 1-gram from being generated repeatedly). Only allowed for encoder-decoder models. Defaults to 1. This is similar to Hugging Face's [`encoder_no_repeat_ngram_size`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.encoder_no_repeat_ngram_size) argument.
    - `encoder_repetition_penalty` number, nullable: Penalizes tokens that have already appeared in the input tokens. Should be positive value. 1.0 means no penalty. Only allowed for encoder-decoder models. See [Keskar et al., 2019](https://arxiv.org/abs/1909.05858) for more details. This is similar to Hugging Face's [`encoder_repetition_penalty`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.encoder_repetition_penalty) argument.
    - `eos_token` integer[], nullable: A list of endpoint sentence tokens.
    - `forced_output_tokens` integer[], nullable: A token sequence that is enforced as a generation output. This option can be used when evaluating the model for the datasets with multi-choice problems (e.g., [HellaSwag](https://huggingface.co/datasets/hellaswag), [MMLU](https://huggingface.co/datasets/cais/mmlu)). Use this option with `logprobs` to get logprobs for the evaluation.
    - `frequency_penalty` number, nullable: Number between -2.0 and 2.0. Positive values penalizes tokens that have been sampled, taking into account their frequency in the preceding text. This penalization diminishes the model's tendency to reproduce identical lines verbatim.
    - `logprobs` integer, nullable: Include the log probabilities on the logprobs most likely output tokens, as well the chosen tokens.
    - `max_tokens` integer, nullable: The maximum number of tokens to generate. For decoder-only models like GPT, the length of your input tokens plus `max_tokens` should not exceed the model's maximum length (e.g., 2048 for OpenAI GPT-3). For encoder-decoder models like T5 or BlenderBot, `max_tokens` should not exceed the model's maximum output length. This is similar to Hugging Face's [`max_new_tokens`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.max_new_tokens) argument.
    - `max_total_tokens` integer, nullable: The maximum number of tokens including both the generated result and the input tokens. Only allowed for decoder-only models. Only one argument between `max_tokens` and `max_total_tokens` is allowed. Default value is the model's maximum length. This is similar to Hugging Face's [`max_length`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.max_length) argument.
    - `min_p` number, nullable: A scaling factor used to determine the minimum token probability threshold. This threshold is calculated as `min_p` multiplied by the probability of the most likely token. Tokens with probabilities below this scaled threshold are excluded from sampling. Values range from 0.0 (inclusive) to 1.0 (inclusive). Higher values result in stricter filtering, while lower values allow for greater diversity. The default value of 0.0 disables filtering, allowing all tokens to be considered for sampling.
    - `min_tokens` integer, nullable: The minimum number of tokens to generate. Default value is 0. This is similar to Hugging Face's [`min_new_tokens`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.min_new_tokens) argument. **This field is unsupported when `response_format` is specified.**
    - `min_total_tokens` integer, nullable: The minimum number of tokens including both the generated result and the input tokens. Only allowed for decoder-only models. Only one argument between `min_tokens` and `min_total_tokens` is allowed. This is similar to Hugging Face's [`min_length`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.min_length) argument.
    - `n` integer, nullable: The number of independently generated results for the prompt. Defaults to 1. This is similar to Hugging Face's [`num_return_sequences`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.num_return_sequences) argument.
    - `no_repeat_ngram` integer, nullable: If this exceeds 1, every ngram of that size can only occur once among the generated result (plus the input tokens for decoder-only models). 1 means that this mechanism is disabled (i.e., you cannot prevent 1-gram from being generated repeatedly). Defaults to 1. This is similar to Hugging Face's [`no_repeat_ngram_size`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.no_repeat_ngram_size) argument.
    - `presence_penalty` number, nullable: Number between -2.0 and 2.0. Positive values penalizes tokens that have been sampled at least once in the existing text.
    - `repetition_penalty` number, nullable: Penalizes tokens that have already appeared in the generated result (plus the input tokens for decoder-only models). Should be positive value (1.0 means no penalty). See [keskar et al., 2019](https://arxiv.org/abs/1909.05858) for more details. This is similar to Hugging Face's [`repetition_penalty`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.repetition_penalty) argument.
    - `response_format` union: The enforced format of the model's output. Note that the content of the output message may be truncated if it exceeds the `max_tokens`. You can check this by verifying that the `finish_reason` of the output message is `length`. For more detailed information, please refer [here](https://friendli.ai/docs/guides/structured-outputs). ***Important*** You must explicitly instruct the model to produce the desired output format using a system prompt or user message (e.g., `You are an API generating a valid JSON as output.`). Otherwise, the model may result in an unending stream of whitespace or other characters. **When `response_format` is specified, `min_tokens` field is unsupported.**
      - ResponseFormatJsonSchema
        - `type` 'json_schema', required: The type of the response format: `json_schema`
        - `json_schema` ResponseFormatJsonSchemaSchema, required
          - `schema` object, required: The schema for the response format, described as a JSON Schema object.
      - ResponseFormatJsonObject
        - `type` 'json_object', required: The type of the response format: `json_object`
      - ResponseFormatRegex
        - `type` 'regex', required: The type of the response format: `regex`
        - `schema` string, required: The schema of the output. Lookaheads or lookbehinds (e.g., `\a`, `\z`, `^`, `$`, `(?=)`, `(?!)`, `(?<=...)`, `(?<!...)`) are not supported. Group specials (e.g., `\w`, `\W`, `\d`, `\D`, `\s`, `\S`) do not support non-ASCII characters. Unicode escape patterns (e.g., `\N`, `\p`, `\P`) are not supported. Additionally, conditional matching (`(?(`) and back-references can cause inefficiency.
      - ResponseFormatText
        - `type` 'text', required: The type of the response format: `text`
    - `seed` union: Seed to control random procedure. If nothing is given, the API generate the seed randomly, use it for sampling, and return the seed along with the generated result. When using the `n` argument, you can pass a list of seed values to control all of the independent generations.
      - integer[]
      - integer
    - `stop` string[], nullable: When one of the stop phrases appears in the generation result, the API will stop generation. The stop phrases are excluded from the result. Defaults to empty list.
    - `stop_tokens` TokenSequence[], nullable: Stop generating further tokens when generated token corresponds to any of the tokens in the sequence.
      - `tokens` integer[], nullable: A List of token IDs.
    - `stream` boolean, nullable: Whether to stream the generation result. When set to `true`, each token will be sent as [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format) once generated.
    - `stream_options` StreamOptions
      - `include_usage` boolean, nullable: When set to `true`, the number of tokens used will be included at the end of the stream result in the form of `"usage": {"completion_tokens": number, "prompt_tokens": number, "total_tokens": number}`.
    - `temperature` number, nullable: Sampling temperature. Smaller temperature makes the generation result closer to greedy, argmax (i.e., `top_k = 1`) sampling. Defaults to 1.0. This is similar to Hugging Face's [`temperature`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.temperature) argument.
    - `token_index_to_replace` integer[], nullable: A list of token indices where to replace the embeddings of input tokens provided via either `tokens` or `prompt`.
    - `top_k` integer, nullable: Limits sampling to the top k tokens with the highest probabilities. Values range from 0 (no filtering) to the model's vocabulary size (inclusive). The default value of 0 applies no filtering, allowing all tokens.
    - `top_p` number, nullable: Keeps only the smallest set of tokens whose cumulative probabilities reach `top_p` or higher. Values range from 0.0 (exclusive) to 1.0 (inclusive). The default value of 1.0 includes all tokens, allowing maximum diversity.
    - `xtc_threshold` number, nullable: A probability threshold used to identify “top choice” tokens for exclusion in XTC (Exclude Top Choices) sampling. Tokens with probabilities at or above this threshold are considered viable candidates, and all but the least likely viable token are excluded from sampling. This option reduces the dominance of highly probable tokens while preserving some diversity by keeping the least confident “top choice.” Values range from 0.0 (inclusive) to 1.0 (inclusive). Higher values make the filtering more selective by requiring higher probabilities to trigger exclusion, while lower values apply filtering more broadly. The default value of 0.0 disables XTC filtering entirely.
    - `xtc_probability` number, nullable: The probability that XTC (Exclude Top Choices) filtering will be applied for each sampling decision. When XTC is triggered, high-probability tokens above the `xtc_threshold` are excluded except for the least likely viable token. This stochastic activation allows for a balance between standard sampling and creativity-boosting exclusion filtering. Values range from 0.0 (inclusive) to 1.0 (inclusive), where 0.0 means XTC is never applied, 1.0 means XTC is always applied when viable tokens exist, and intermediate values provide probabilistic activation. The default value of 0.0 disables XTC filtering.
    - `prompt` union, required: The prompt (i.e., input text) to generate completions for. Either `prompt` or `tokens` field is required.
      - string
      - string[]
  - CompletionsBodyWithTokens
    - `model` string, nullable: Routes the request to a specific adapter.
    - `bad_word_tokens` TokenSequence[], nullable: Same as the above `bad_words` field, but receives token sequences instead of text phrases. This is similar to Hugging Face's [`bad_word_ids`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.bad_words_ids) argument.
      - `tokens` integer[], nullable: A List of token IDs.
    - `bad_words` string[], nullable: Text phrases that should not be generated. For a bad word phrase that contains N tokens, if the first N-1 tokens appears at the last of the generated result, the logit for the last token of the phrase is set to -inf. Before checking whether a bard word is included in the result, the word is converted into tokens. We recommend using `bad_word_tokens` because it is clearer. For example, after tokenization, phrases "clear" and " clear" can result in different token sequences due to the prepended space character. Defaults to empty list.
    - `embedding_to_replace` number[], nullable: A list of flattened embedding vectors used for replacing the tokens at the specified indices provided via `token_index_to_replace`.
    - `encoder_no_repeat_ngram` integer, nullable: If this exceeds 1, every ngram of that size occurring in the input token sequence cannot appear in the generated result. 1 means that this mechanism is disabled (i.e., you cannot prevent 1-gram from being generated repeatedly). Only allowed for encoder-decoder models. Defaults to 1. This is similar to Hugging Face's [`encoder_no_repeat_ngram_size`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.encoder_no_repeat_ngram_size) argument.
    - `encoder_repetition_penalty` number, nullable: Penalizes tokens that have already appeared in the input tokens. Should be positive value. 1.0 means no penalty. Only allowed for encoder-decoder models. See [Keskar et al., 2019](https://arxiv.org/abs/1909.05858) for more details. This is similar to Hugging Face's [`encoder_repetition_penalty`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.encoder_repetition_penalty) argument.
    - `eos_token` integer[], nullable: A list of endpoint sentence tokens.
    - `forced_output_tokens` integer[], nullable: A token sequence that is enforced as a generation output. This option can be used when evaluating the model for the datasets with multi-choice problems (e.g., [HellaSwag](https://huggingface.co/datasets/hellaswag), [MMLU](https://huggingface.co/datasets/cais/mmlu)). Use this option with `logprobs` to get logprobs for the evaluation.
    - `frequency_penalty` number, nullable: Number between -2.0 and 2.0. Positive values penalizes tokens that have been sampled, taking into account their frequency in the preceding text. This penalization diminishes the model's tendency to reproduce identical lines verbatim.
    - `logprobs` integer, nullable: Include the log probabilities on the logprobs most likely output tokens, as well the chosen tokens.
    - `max_tokens` integer, nullable: The maximum number of tokens to generate. For decoder-only models like GPT, the length of your input tokens plus `max_tokens` should not exceed the model's maximum length (e.g., 2048 for OpenAI GPT-3). For encoder-decoder models like T5 or BlenderBot, `max_tokens` should not exceed the model's maximum output length. This is similar to Hugging Face's [`max_new_tokens`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.max_new_tokens) argument.
    - `max_total_tokens` integer, nullable: The maximum number of tokens including both the generated result and the input tokens. Only allowed for decoder-only models. Only one argument between `max_tokens` and `max_total_tokens` is allowed. Default value is the model's maximum length. This is similar to Hugging Face's [`max_length`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.max_length) argument.
    - `min_p` number, nullable: A scaling factor used to determine the minimum token probability threshold. This threshold is calculated as `min_p` multiplied by the probability of the most likely token. Tokens with probabilities below this scaled threshold are excluded from sampling. Values range from 0.0 (inclusive) to 1.0 (inclusive). Higher values result in stricter filtering, while lower values allow for greater diversity. The default value of 0.0 disables filtering, allowing all tokens to be considered for sampling.
    - `min_tokens` integer, nullable: The minimum number of tokens to generate. Default value is 0. This is similar to Hugging Face's [`min_new_tokens`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.min_new_tokens) argument. **This field is unsupported when `response_format` is specified.**
    - `min_total_tokens` integer, nullable: The minimum number of tokens including both the generated result and the input tokens. Only allowed for decoder-only models. Only one argument between `min_tokens` and `min_total_tokens` is allowed. This is similar to Hugging Face's [`min_length`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.min_length) argument.
    - `n` integer, nullable: The number of independently generated results for the prompt. Defaults to 1. This is similar to Hugging Face's [`num_return_sequences`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.num_return_sequences) argument.
    - `no_repeat_ngram` integer, nullable: If this exceeds 1, every ngram of that size can only occur once among the generated result (plus the input tokens for decoder-only models). 1 means that this mechanism is disabled (i.e., you cannot prevent 1-gram from being generated repeatedly). Defaults to 1. This is similar to Hugging Face's [`no_repeat_ngram_size`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.GenerationConfig.no_repeat_ngram_size) argument.
    - `presence_penalty` number, nullable: Number between -2.0 and 2.0. Positive values penalizes tokens that have been sampled at least once in the existing text.
    - `repetition_penalty` number, nullable: Penalizes tokens that have already appeared in the generated result (plus the input tokens for decoder-only models). Should be positive value (1.0 means no penalty). See [keskar et al., 2019](https://arxiv.org/abs/1909.05858) for more details. This is similar to Hugging Face's [`repetition_penalty`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.repetition_penalty) argument.
    - `response_format` union: The enforced format of the model's output. Note that the content of the output message may be truncated if it exceeds the `max_tokens`. You can check this by verifying that the `finish_reason` of the output message is `length`. For more detailed information, please refer [here](https://friendli.ai/docs/guides/structured-outputs). ***Important*** You must explicitly instruct the model to produce the desired output format using a system prompt or user message (e.g., `You are an API generating a valid JSON as output.`). Otherwise, the model may result in an unending stream of whitespace or other characters. **When `response_format` is specified, `min_tokens` field is unsupported.**
      - ResponseFormatJsonSchema
        - `type` 'json_schema', required: The type of the response format: `json_schema`
        - `json_schema` ResponseFormatJsonSchemaSchema, required
          - `schema` object, required: The schema for the response format, described as a JSON Schema object.
      - ResponseFormatJsonObject
        - `type` 'json_object', required: The type of the response format: `json_object`
      - ResponseFormatRegex
        - `type` 'regex', required: The type of the response format: `regex`
        - `schema` string, required: The schema of the output. Lookaheads or lookbehinds (e.g., `\a`, `\z`, `^`, `$`, `(?=)`, `(?!)`, `(?<=...)`, `(?<!...)`) are not supported. Group specials (e.g., `\w`, `\W`, `\d`, `\D`, `\s`, `\S`) do not support non-ASCII characters. Unicode escape patterns (e.g., `\N`, `\p`, `\P`) are not supported. Additionally, conditional matching (`(?(`) and back-references can cause inefficiency.
      - ResponseFormatText
        - `type` 'text', required: The type of the response format: `text`
    - `seed` union: Seed to control random procedure. If nothing is given, the API generate the seed randomly, use it for sampling, and return the seed along with the generated result. When using the `n` argument, you can pass a list of seed values to control all of the independent generations.
      - integer[]
      - integer
    - `stop` string[], nullable: When one of the stop phrases appears in the generation result, the API will stop generation. The stop phrases are excluded from the result. Defaults to empty list.
    - `stop_tokens` TokenSequence[], nullable: Stop generating further tokens when generated token corresponds to any of the tokens in the sequence.
      - `tokens` integer[], nullable: A List of token IDs.
    - `stream` boolean, nullable: Whether to stream the generation result. When set to `true`, each token will be sent as [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format) once generated.
    - `stream_options` StreamOptions
      - `include_usage` boolean, nullable: When set to `true`, the number of tokens used will be included at the end of the stream result in the form of `"usage": {"completion_tokens": number, "prompt_tokens": number, "total_tokens": number}`.
    - `temperature` number, nullable: Sampling temperature. Smaller temperature makes the generation result closer to greedy, argmax (i.e., `top_k = 1`) sampling. Defaults to 1.0. This is similar to Hugging Face's [`temperature`](https://huggingface.co/docs/transformers/v4.26.0/en/main_classes/text_generation#transformers.generationconfig.temperature) argument.
    - `token_index_to_replace` integer[], nullable: A list of token indices where to replace the embeddings of input tokens provided via either `tokens` or `prompt`.
    - `top_k` integer, nullable: Limits sampling to the top k tokens with the highest probabilities. Values range from 0 (no filtering) to the model's vocabulary size (inclusive). The default value of 0 applies no filtering, allowing all tokens.
    - `top_p` number, nullable: Keeps only the smallest set of tokens whose cumulative probabilities reach `top_p` or higher. Values range from 0.0 (exclusive) to 1.0 (inclusive). The default value of 1.0 includes all tokens, allowing maximum diversity.
    - `xtc_threshold` number, nullable: A probability threshold used to identify “top choice” tokens for exclusion in XTC (Exclude Top Choices) sampling. Tokens with probabilities at or above this threshold are considered viable candidates, and all but the least likely viable token are excluded from sampling. This option reduces the dominance of highly probable tokens while preserving some diversity by keeping the least confident “top choice.” Values range from 0.0 (inclusive) to 1.0 (inclusive). Higher values make the filtering more selective by requiring higher probabilities to trigger exclusion, while lower values apply filtering more broadly. The default value of 0.0 disables XTC filtering entirely.
    - `xtc_probability` number, nullable: The probability that XTC (Exclude Top Choices) filtering will be applied for each sampling decision. When XTC is triggered, high-probability tokens above the `xtc_threshold` are excluded except for the least likely viable token. This stochastic activation allows for a balance between standard sampling and creativity-boosting exclusion filtering. Values range from 0.0 (inclusive) to 1.0 (inclusive), where 0.0 means XTC is never applied, 1.0 means XTC is always applied when viable tokens exist, and intermediate values provide probabilistic activation. The default value of 0.0 disables XTC filtering.
    - `tokens` integer[], required: The tokenized prompt (i.e., input tokens). Either `prompt` or `tokens` field is required.

## Response `200`

Successfully generated completions.

- ContainerCompletionsSuccess
  - `id` string, required: A unique ID of the completion.
  - `object` 'text_completion', required: The object type, which is always set to `text_completion`.
  - `usage` TextUsage, required
    - `prompt_tokens` integer, required: Number of tokens in the prompt.
    - `completion_tokens` integer, required: Number of tokens in the generated completions.
    - `total_tokens` integer, required: Total number of tokens used in the request (`prompt_tokens` + `completion_tokens`).
    - `prompt_tokens_details` PromptTokensDetails
      - `cached_tokens` integer, nullable: Cached tokens present in the prompt.
  - `choices` CompletionsChoice[], required
    - `index` integer, required: The index of the choice in the list of generated choices.
    - `text` string, required: Generated text output.
    - `logprobs` CompletionsLogprobs
      - `text_offset` integer[], required: The starting character position of each token in the generated text, useful for mapping tokens back to their exact location for detailed analysis.
      - `token_logprobs` number[], required: The log probabilities of each generated token, indicating the model's confidence in selecting each token.
      - `tokens` string[], required: A list of individual tokens generated in the completion, representing segments of text such as words or pieces of words.
      - `top_logprobs` object[], required: A list of dictionaries, where each dictionary represents the top alternative tokens considered by the model at a specific position in the generated text, along with their log probabilities. The number of items in each dictionary matches the value of `logprobs`.
    - `seed` integer, required: Random seed used for the generation.
    - `finish_reason` 'stop' | 'length', required: Termination condition of the generation. `stop` means the API returned the full completions generated by the model without running into any limits. `length` means the generation exceeded `max_tokens` or the conversation exceeded the max context length.
    - `tokens` integer[], required: Generated output tokens.
  - `model` string, nullable: The model to generate the completion. For dedicated endpoints, it returns the endpoint id.

## Other responses

- `422`: Unprocessable Entity

---

[API](https://skmtc.dev/friendli/apis/friendli-suite-api-reference.md) - [All operations](https://skmtc.dev/friendli/apis/friendli-suite-api-reference/llms.txt) - [OpenAPI document](https://skmtc.dev/friendli/apis/friendli-suite-api-reference/revisions/38fb43993a11?raw)
