Updated on 2026-06-18 GMT+08:00

Prefix Completion

In daily text processing tasks, such as writing articles or generating responses for chatbots, there may be a need to generate subsequent content based on existing text fragments. However, manually completing these tasks is not only time-consuming but also inefficient. To ensure that the generated follow-up content is both semantically coherent and efficient, MaaS supports model prefix completion capabilities. You can provide a prefix (the beginning part) of a text, and the LLM will generate semantically coherent and reasonable subsequent content according to your requirements.

Use Cases

Prefix completion can be applied to various scenarios, for example:

  • Code completion: In a programming environment, predict and generate subsequent code based on existing code snippets.
  • Automated writing: Assists authors in completing the creation of articles, stories, or poems.
  • Chatbot: Generates appropriate responses based on user input.
  • Email auto-reply: Automatically generates reply content based on the beginning of the email.

Usage Instructions

MaaS APIs V2 and OpenAI-compatible APIs support prefix completion. To achieve this, set the role of the last message in the messages array to assistant and provide the prefix in its content. If you are using a V2 API, set the parameter prefix: true in the message. If you are using an OpenAI-compatible API, add two parameters to the request body: continue_final_message: true; add_generation_prompt: false. The request body format is as follows:

API V2:

{
  "model": "deepseek-v3.2",
  "messages": [
    {
      "role": "user",
    "content": "Implement quick sort for an array using Python. Do not add any additional content."
    },
    {
      "role": "assistant",
      "content": "def quick_sort(array)",
      "prefix": true
    }
  ],
  "temperature": 0.6,
  "stream": false
}

OpenAI-compatible API:

{ 
    "model": "deepseek-v3.2",
    "continue_final_message": true,
    "add_generation_prompt": false,
    "messages": [
        {"role": "user", "content": "You are a calculator, please calculate: 1 + 1"}, 
        {"role": "assistant", "content": "="}
    ],
    "temperature": 0.6,
    "stream": false
}

The model will start generating from the prefix content.

Supported Models

For models that currently support prefix completion, see the Capability row in Text Generation.

API Description

For API parameter descriptions, see MaaS Standard API V2 or OpenAI-compatible APIs.

Mode Comparison

DeepSeek-V3.2 is used as an example.

Table 1 Comparison between common mode and prefix completion mode

Comparison Item

Common

Prefix Completion

Request example

{
  "model": "deepseek-v3.2",
  "messages": [
    {
      "content": " You are a chat assistant", 
      "role": "system"
    },
    {
      "role": "user",
      "content": " Write a quick sort function in Python without adding any additional content" 
    }
  ],
  "temperature": 0.6,
  "stream": false
}
{
  "model": "deepseek-v3.2",
  "messages": [
    {
      "role": "user",
      "content": "Implement quick sort for an array using Python. Do not add any additional content."      
    },
    {
      "role": "assistant",
      "content": "def quick_sort(array)",
      "prefix": true
    }
  ],
  "temperature": 0.6,
  "stream": false
}

Response example

The model will generate a complete natural language response based on the query.

def quick_sort(arr):
    if len(arr) <= 1:
        return arr

    pivot = arr[len(arr) // 2]

    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]

    return quick_sort(left) + middle + quick_sort(right)

The model will start generating from the prefix content def quick_sort(array).

-> list:
    if len(array) <= 1:
        return array

    pivot = array[len(array) // 2]
    left = [x for x in array if x < pivot]
    middle = [x for x in array if x == pivot]
    right = [x for x in array if x > pivot]

    return quick_sort(left) + middle + quick_sort(right)

Prerequisites

  • You have subscribed to the built-in service on the Model Inference > Real-Time Inference > Built-in Services tab page. For details, see Subscribing to a Built-in Service.

Getting Started

Code completion is a core application of prefix completion. The following example demonstrates how to use the DeepSeek-V3.2 model to complete a Python function.

import requests
import json

if __name__ == '__main__':
    url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions"  # API address
    api_key = "MAAS_API_KEY"  # Replace MAAS_API_KEY with the obtained API key.

    # Send request.
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }
    data = {
		"model": "deepseek-v3.2",
		"messages" : [{
				"role" : "user",
				"content": "Implement quick sort for an array using Python. Do not add any additional content."
			}, {
				"role" : "assistant",
				"content" : "def quick_sort(array)",
				"prefix" : True
			}
		],
		"temperature": 0.6,
		"stream": False
	}
    response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)

    with open('response.txt', 'w', encoding='utf-8') as f:
        f.write(f"Status Code: {response.status_code}\n")
        f.write("Response Content:\n")
        f.write(response.text)
    # Print result.
    print(response.status_code)
    print(response.text)

Return result:

if len(array) <= 1:
    return array
pivot = array[len(array) // 2]
left = [x for x in array if x < pivot]
middle = [x for x in array if x == pivot]
right = [x for x in array if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
curl -X POST "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $MAAS_API_KEY" \
  -d '{
	"model": "deepseek-v3.2",
	"messages":  [{
      "role": "user", "content": "Implement quick sort for an array using Python. Do not add any additional content." 
    },
    {
      "role": "assistant","content": "def quick_sort(array)","prefix": true
    }],
    "temperature": 0.6,
	"stream": false
  }'

Return result:

if len(array) <= 1:
	return array
pivot = array[len(array) // 2]
left = [x for x in array if x < pivot]
middle = [x for x in array if x == pivot]
right = [x for x in array if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
JDK 15 or later is recommended.
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
/**
* Prefix continuation
 * */
public class ChatCompletionsClient {
    public static void main(String[] args) {
        // API URL
        String url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions";
        // Replace it with the actual API key.
        String apiKey = "MAAS_API_KEY";
        // Replace it with a model that supports prefix continuation.
        String modelName = "deepseek-v3.2"; 
        String jsonBody = String.format(
                """
                        {
                            "model": "%s",
                            "messages": [
                                {"role": "user", "content": "Implement quick sort for an array using Python. Do not add any additional content."},
                                {"role": "assistant", "content": "def quick_sort(array)", "prefix" : true}
                            ],
                            "temperature": 0.6,
                            "stream": false
                        }""", modelName);
        try {
            HttpClient client = HttpClient.newBuilder()
                    .connectTimeout(Duration.ofSeconds(10))
                    .build();
            // Construct an HTTP request.
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Content-Type", "application/json")
                    .header("Authorization", "Bearer " + apiKey)
                    .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                    .build();
    // Send the request and obtain a response.
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            int statusCode = response.statusCode();
            String responseBody = response.body();
            // Write the response to a file.
            String fileContent = "Status Code: " + statusCode + "\nResponse Content:\n" + responseBody;
            Files.writeString(Paths.get("response.txt"), fileContent);
            // Console output
            System.out.println(statusCode);
            System.out.println(responseBody);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Return result:

if len(array) <= 1:
	return array
pivot = array[len(array) // 2]
left = [x for x in array if x < pivot]
middle = [x for x in array if x == pivot]
right = [x for x in array if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

Scenario One: Role-Playing

In role-playing scenarios, users desire generated dialogue that adheres to the specific character persona requirements. This can be achieved through the use of prefix completion models to generate dialogues for characters, ensuring they conform to their respective personas. The following is an example of Python code. You can replace the model by modifying the model parameter. For details about the model parameter, see Text Generation.

#Python
import requests
import json

if __name__ == '__main__':
    url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions"  # API address
    api_key = "MAAS_API_KEY"  # Replace MAAS_API_KEY with the obtained API key.

    # Send request.
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }
    data = {
        "model": "deepseek-v3.2",
        "messages" : [{
                "role" : "user",
                "content": "Below is a scene from Dream of the Red Chamber. Please conduct a dialogue according to the assigned roles. For each round of dialogue, only provide the response for the current character and do not generate any additional content. After leaving her village cottage and traveling into the city, Granny Liu soon arrived before a grand estate. She beheld vermilion gates and high walls, with exquisitely carved beams and painted rafters. Majestic stone lions stood before the entrance, reflecting the strict dignity and status of the household."
            }, {
                "role" : "assistant",
                "content": "Granny Liu said:,"
                "prefix" : True
            }
        ],
        "temperature": 0.6,
        "stream": False
    }
    response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)

    with open('response.txt', 'w', encoding='utf-8') as f:
        f.write(f"Status Code: {response.status_code}\n")
        f.write("Response Content:\n")
        f.write(response.text)
    # Print result.
    print(response.status_code)
    print(response.text)

Output:

"Amitabha! This is far more magnificent than our countryside. I must first inquire—is this the Jia estate? (To a passing pedestrian) Excuse me, brother, might this be the Rongguo Mansion?"
#Python
import requests
import json

if __name__ == '__main__':
    url = "https://api-ap-southeast-1.modelarts-maas.com/openai/v1/chat/completions"  # API address
    api_key = "MAAS_API_KEY"  # Replace MAAS_API_KEY with the obtained API key.

    # Send request.
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }
    data = {
        "model": "deepseek-v3",
        "continue_final_message":True,
        "add_generation_prompt":False,
        "messages" : [{
                "role" : "user",
                "content": "Below is a scene from Dream of the Red Chamber. Please conduct a dialogue according to the assigned roles. For each round of dialogue, only provide the response for the current character and do not generate any additional content. After leaving her village cottage and traveling into the city, Granny Liu soon arrived before a grand estate. She beheld vermilion gates and high walls, with exquisitely carved beams and painted rafters. Majestic stone lions stood before the entrance, reflecting the strict dignity and status of the household."
            }, {
                "role" : "assistant",
                "content": "Granny Liu said:,"
                "prefix" : True
            }
        ],
        "temperature": 0.6,
        "stream": False
    }
    response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)

    with open('response.txt', 'w', encoding='utf-8') as f:
        f.write(f"Status Code: {response.status_code}\n")
        f.write("Response Content:\n")
        f.write(response.text)
    # Print result.
    print(response.status_code)
    print(response.text)

Output:

"Amitabha! This is far more magnificent than our countryside. I must first inquire—is this the Jia estate? (To a passing pedestrian) Excuse me, brother, might this be the Rongguo Mansion?"

Scenario Two: Output Content Exceeding the Model's Output Limit

Due to the limitation on the length of single outputs from LLMs, long content may not be fully generated. Prefix completion can append content based on previous requests, allowing multiple segments to be concatenated into an ultra-long output. The following is an example of Python code. You can replace the model by modifying the model parameter. For details about the model parameter, see Text Generation.

#Python
import requests
import json
import os

def continuous_generate():
    url = "https://api-ap-southeast-1.modelarts-maas.com/v2/chat/completions"
    api_key = "MAAS_API_KEY"

    # Initial message
    messages = [
        {"role": "user", "content":"Please continue the following story with a conversation theme: In a lush green forest, there lives a rabbit named Little White."}, 
        {"role": "assistant", "content": "", "prefix": True}
    ]

    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }

    full_response_content = messages[-1]["content"]  # Record completed content
    loop_count = 0
    max_loops = 30  # Maximum loop count to prevent infinite loops

    while True:
        data = {
            "model": "deepseek-v3.2",
            "messages": messages,
            "temperature": 0.6,
            "stream": False
        }

        response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)

        if response.status_code != 200:
            print("Error:", response.text)
            break

        result = response.json()
        choice = result['choices'][0]
        finish_reason = choice.get('finish_reason')
        content = choice['message']['content']

        # Concatenate new content to historical responses
        full_response_content += content

        # Update assistant's content to the current complete cumulative content
        messages[-1]["content"] = full_response_content

        loop_count += 1
		 # Stop if the maximum number of loops is reached or finish_reason is not length
        if finish_reason != "length" or loop_count >= max_loops:
            if loop_count >= max_loops:
                print(f" Reached max loop limit {max_loops}, stopped.")
            break

    # Output the final complete content
    print(" Final Complete Content:")
    print(full_response_content)

    # (Optional) Save the output to a file
    with open("final_output.txt", "w", encoding="utf-8") as f:
        f.write(full_response_content)

if __name__ == "__main__":
    continuous_generate()

Model output example:

Final Complete Content:
Alright, let's continue with this beginning and weave a fairy tale:
---
In a lush green forest, there lived a rabbit named Little White. Unlike other rabbits, Little White had eyes like rubies that always gazed longingly at the tallest and oldest oak tree in the depths of the forest. Grandma Rabbit once told him that inside the hollow of that oak tree lay an ancient **"Moonlight Phone"**. It was said that on the night when the moon was fullest each year, the phone would briefly connect, allowing you to call anyone you missed, no matter how far away they were, even if they were … in another world.
Little White missed his grandfather. Last year, during the time when the leaves turned their most golden yellow, Grandpa went to a distant place called "Star Prairie" and never returned.
So, Little White waited and waited, from spring until summer. Finally, the full moon of Mid-Autumn Festival rose into the night sky like a silver platter. Gathering his courage, Little White crossed the dew-covered grass, skirted around the snoring hedgehog, and arrived at the base of the old oak tree. The hollow was deep, and inside indeed lay a wondrous phone wrapped in vines and with a shell for a receiver. It glowed softly with a pearly light.     
Little White's heart raced as he tiptoed and carefully picked up the receiver. A "rustling" sound came through, like the wind blowing across the stars. He took a deep breath and spoke softly into the shell receiver: "Hello... Could you please connect me to Star Prairie? I want to find my grandpa."
The line was silent for a moment, then the "rustling" sound turned into a melodious sound like flowing water. A warm, familiar voice, carrying the scent of sunshine and dry hay, responded: "Hey, Little White, is that you? Have your long ears grown a bit longer?"
It was Grandpa! Tears welled up in Little White's eyes, and he nodded vigorously as if Grandpa could see him: "Grandpa! It's me! Your hearing is still so sharp! I... I'm doing well. I run faster than before and found new carrot fields, but... but I miss you. What does Star Prairie look like?"
Grandpa's voice was filled with laughter:"This place is incredibly soft, like a carpet made of clouds, covered in luminous berries, and it's always a comfortable twilight. I've made many new friends here, including a joke-telling meteor badger and a quiet but kind giant-horned star deer. Every day, I sit on the highest hill where I can see our forest."
"Really? Can you see me?" Little White asked eagerly.
"Of course," Grandpa's voice was gentle like moonlight, "I see you bravely jumping over streams, helping lost birds find their way home, and sometimes sitting by the entrance of your burrow, staring at the sky. My Little White has grown up, becoming both kind and brave."
Little White felt warmth and a tinge of sadness in his heart. "Grandpa, sometimes I get scared, afraid of the dark, afraid of thunder..."
"Remember, child," Grandpa's voice was firm and powerful, "fear is like shadows in the forest—seemingly large, but if you walk through them bravely, they will retreat behind you. Your courage lies within your heart, just like seeds in the soil, always growing. Listen to your heartbeat; it is the loudest drumbeat, guiding you forward."
They talked for a long time, about news from the forest, Little White's newly learned jumping skills, and Grandpa's amusing stories from Star Prairie. As the moon gradually tilted westward, the "rustling" sound in the phone grew louder again.
"Little White," Grandpa's voice became softer but remained clear, "the power of the Moonlight Phone comes from the gift of moonlight, and it's almost depleted. Remember what Grandpa said: love is a call that never disconnects. When you smell the fragrance of flowers in the wind, it's me greeting you; when you see twinkling stars, it's me blinking at you. I have never left; I am just accompanying you in a different way."
"Grandpa, I..." Little White choked up.
"Run happily and grow freely. Eat more sweet carrots for me. Goodbye, my dear." Grandpa's voice, tinged with laughter, gradually merged into the "rustling" wind of the stars.
The glow of the phone dimmed and returned to calmness. Little White gently placed the receiver back, tears still on his face, but his heart felt bright and reassured, as if washed by moonlight. He stepped out of the hollow and looked up at the starry sky. One star, particularly warm and bright, seemed to be gently twinkling at him.
Little White knew it must be Grandpa. From that day on, Little White became more cheerful and brave. He continued to love running but also often stopped to smell the flowers and watch the stars, then smiled knowingly at the night sky.
Because he knew that no matter how far apart they were, the signal of love and longing was always full strength. That ancient Moonlight Phone was actually always connected in his heart.
---
This is the story of Little White and the Moonlight Phone, a fairy tale about longing, courage, and eternal connection. I hope you enjoyed this warm ending.
The process has ended. The exit code is 0.