What is Chat Prefix Completion
Chat Prefix Completion allows developers to preset an "opening" in the assistant message, letting the model continue from this opening. This is very useful in the following scenarios:
- Force the model to start replies in a specific format (e.g., JSON, Markdown tables)
- Fill in missing parts of conversation templates
- Let the model start replies with a specific tone or persona
Basic Usage
Pre-fill an assistant message in the messages and set the prefix parameter to true:
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url='https://api.deepseek.com'
)
response = client.chat.completions.create(
model='deepseek-v4-flash',
messages=[
{"role": "system", "content": "You are a professional email writing assistant."},
{"role": "user", "content": "Write a leave request email, subject: Feeling unwell, request one day off."},
# Preset opening - the model will continue from here
{"role": "assistant", "content": "Dear Manager:\n\nHello!"}
],
# Enable prefix completion
extra_body={"chat_prefix_completion": True}
)
print(response.choices[0].message.content)
# Output:
# Dear Manager:
#
# Hello!
# Due to feeling unwell, I would like to request one day off... (continuation)Force JSON Output Opening
Solve the problem of the model occasionally not outputting pure JSON:
response = client.chat.completions.create(
model='deepseek-v4-pro',
messages=[
{"role": "system", "content": "You are a data analyst, output reports in JSON format."},
{"role": "user", "content": "Analyze Q2 sales data: total revenue 5 million, 20% year-over-year growth"},
# Force start with JSON
{"role": "assistant", "content": "{\n \"report\": "}
],
extra_body={"chat_prefix_completion": True}
)
# Output guaranteed to start with {"report":Markdown Table Continuation
response = client.chat.completions.create(
model='deepseek-v4-flash',
messages=[
{"role": "user", "content": "List the three major Python web frameworks and their features"},
# Preset table header - the model fills in the table content
{"role": "assistant", "content": "| Framework | Features | Use Cases |\n|-----------|----------|-----------|\n"}
],
extra_body={"chat_prefix_completion": True}
)
# Output guaranteed to start with Markdown table formatChat Prefix vs FIM Comparison
| Chat Prefix Completion | FIM Completion | |
|---|---|---|
| API | /v1/chat/completions | /v1/completions |
| Control Method | assistant message prefix | prompt + suffix parameters |
| Thinking Mode | Supports thinking mode | Non-thinking mode only |
| Use Cases | Format control, template filling, tone setting | Code completion, infilling |
| Stability | Beta | Beta |
Notes
- Currently a Beta feature; API and behavior may change
- Only effective when the last message in the list is an assistant message
- Prefix length counts towards output token consumption
- It is recommended to keep the prefix short (within 100 tokens) to leave room for the model's creativity
- Can be combined with JSON Mode: set the prefix as a JSON opening + response_format constraint