All guides

DeepSeek prefix completion: code continuation and stop boundaries

Use the Beta endpoint and an assistant prefix, separating returned text, stop markers and code correctness.

AINavWritten / Updated Sources checked
On this page

At a glance

Chat Prefix Completion continues an assistant message prefix supplied by the caller. This task follows the official Python quicksort example, using a Python fence as the prefix and a closing fence as the stop sequence. Its deliverable is a correctly formed request and the raw returned content. Parsing and sorting correctness are separate checks; a successful API response does not establish either.

Task flow

  1. Last message

    The final message is assistant with prefix: True.

  2. Beta request

    Use the documented /beta endpoint and model example.

  3. Inspect the return

    Keep the actual content, check its boundaries, then review the code.

The arrows show data and checks for this task; branches are explained in the text.

Prepare the client and credentials

You need an installed OpenAI Python client, a usable DeepSeek API credential and permitted usage allowance. Supply the key through DEEPSEEK_API_KEY in the environment. The code uses deepseek-flash from the current documentation example. Check your available models and Beta access before running it. No account was accessed, purchase made or model request sent for this article.

Put the prefix in the right message

Place the user request first and the assistant prefix last. That final message must have role assistant and Python Boolean prefix: True. Its content is a Python opening fence followed by a newline. This asks the model to continue a specified reply beginning. Putting the same text in a user prompt is not the documented prefix-completion contract.

Save and send an explicit request

Example: the documented quicksort prefix request
python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com/beta",
)

messages = [
    {"role": "user", "content": "Please write quick sort code"},
    {"role": "assistant", "content": "```python\n", "prefix": True},
]
response = client.chat.completions.create(
    model="deepseek-flash",
    messages=messages,
    stop=["```"],
)
print(response.choices[0].message.content)

Save the script as prefix_completion.py and run python prefix_completion.py from a terminal with the client and environment variable already configured. The /beta part of base_url enables the feature as described in the guide. stop=["```"] asks generation to end at a closing fence so that explanations do not follow. It does not review the generated code.

Keep the raw content before judging it

The script prints response.choices[0].message.content. Save that actual value before checking whether it is empty, prematurely cut off or mixed with explanations. Do not invent a closing fence, missing function body or sorting result. Review syntax and logic before deciding to execute any returned code. This task did not execute generated code.

Keep three checks separate

Prefix request completion checks
LayerWhat to check
Request contractBeta address, last assistant message and prefix: True
Return boundaryActual content and the stop sequence’s effect on truncation
Code reviewSyntax, sorting logic and input boundaries independently of API success

Stop at the request stage if authentication, model access or the Beta address fails. If required code is absent, retain the response and refine the task. Do not fill gaps by hand and claim a complete model delivery. The example requests quicksort; the guide does not guarantee identical or correct implementations on every call.

Verification scope

The official Chat Prefix Completion page was read to check message position, the prefix parameter, Beta address, model example and stop sequence. No client was installed, account opened, API called or returned code executed. This Beta feature and example do not imply identical behavior across every model or Chat Completion endpoint.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. DeepSeek · Official sources
    View details