DeepSeek FIM: fill the middle of code and check the prefix and suffix
Use the Beta completions endpoint with prompt, suffix and max_tokens to get middle text, then check it against the surrounding code yourself.
Task and deliverable · Prepare the input and environment · Mechanism and data flow
At a glance
FIM asks the model to write the middle of a piece of code while the code before and after stays fixed. The official page calls it FIM (Fill In the Middle) completion: the user supplies a prefix and an optional suffix, and the model completes what lies between. This article relies only on that page and explains how the request is built, what each variable in the sample does, and what a reader should check in the output. Manual joining, checks and key management are editorial recommendations.
Task workflow
Prefix and suffix bound the middle
The source says that in FIM the user provides
aprefix and an optional suffix, and the model completes the content in between. In the sample,promptholds the prefixdef fib(a):andsuffixholds the final recursive return line. The model generates the text between them. The source marks the suffix as optional, but this article covers the case where the later code must be kept, so both fields are supplied. The source does not say the three parts will always join into valid syntax, so that check falls to the reader.Beta base URL and completions call
The page labels FIM a Beta feature and says to set
base_url=https://api.deepseek.com/betato enable it. The sample creates an OpenAI client withbase_urlset tohttps://api.deepseek.com/betaand then callsclient.completions.create, not a chat endpoint. The documentation navigation also lists a separate Chat Prefix Completion (Beta) page next to FIM, so the two should not be mixed: this request usespromptandsuffixand has nomessages.Model, length parameter and output location
The sample sets
model="deepseek-flash", the model name the source page currently uses; confirm it against the official docs before sending.max_tokens=128limits the length of this generation. A separate Notice on the page says the max tokens of FIM completion is 4K, and this article adds no interpretation of that number. The result is read fromresponse.choices[0].text, and the sample only prints it, with no joining, parsing or syntax check.
Task and deliverable
FIM asks the model to write the middle of a piece of code while the code before and after stays fixed. The official page calls it FIM (Fill In the Middle) completion: the user supplies a prefix and an optional suffix, and the model completes what lies between. This article relies only on that page and explains how the request is built, what each variable in the sample does, and what a reader should check in the output. Manual joining, checks and key management are editorial recommendations.
Prepare the input and environment
- You need a working DeepSeek API account and an API key. In the source sample,
api_keyis only the placeholder<your api key>. The page does not explain how to obtain or manage keys, so follow the official platform instructions. - You need a Python environment where the
openaipackage is already installed, because the sample begins withfrom openai import OpenAI. The source gives no install command or version requirement, and this article assumes none. - You need to know which code comes before and after the gap. The source sample uses the function opening
def fib(a):and the closing linereturn fib(a-1) + fib(a-2), andareader has to substitute their own code.
Mechanism and data flow
Prefix and suffix bound the middle: The source says that in FIM the user provides a prefix and an optional suffix, and the model completes the content in between. In the sample, prompt holds the prefix def fib(a): and suffix holds the final recursive return line. The model generates the text between them. The source marks the suffix as optional, but this article covers the case where the later code must be kept, so both fields are supplied. The source does not say the three parts will always join into valid syntax, so that check falls to the reader.
Beta base URL and completions call: The page labels FIM a Beta feature and says to set base_url=https://api.deepseek.com/beta to enable it. The sample creates an OpenAI client with base_url set to https://api.deepseek.com/beta and then calls client.completions.create, not a chat endpoint. The documentation navigation also lists a separate Chat Prefix Completion (Beta) page next to FIM, so the two should not be mixed: this request uses prompt and suffix and has no messages.
Model, length parameter and output location: The sample sets model="deepseek-flash", the model name the source page currently uses; confirm it against the official docs before sending. max_tokens=128 limits the length of this generation. A separate Notice on the page says the max tokens of FIM completion is 4K, and this article adds no interpretation of that number. The result is read from response.choices[0].text, and the sample only prints it, with no joining, parsing or syntax check.
Workflow map
| Stage | What happens |
|---|---|
| Prefix and suffix bound the middle | The source says that in FIM the user provides a prefix and an optional suffix, and the model completes the content in between. |
| Beta base URL and completions call | The page labels FIM a Beta feature and says to set base_url=https://api.deepseek.com/beta to enable it. |
| Model, length parameter and output location | The sample sets model="deepseek-flash", the model name the source page currently uses; confirm it against the official docs before sending. |
Complete the task
Confirm the account, key and runtime: First confirm that you can use the DeepSeek API and that you have an API key and a place to keep it. The sample writes the string straight into api_key, which suits reading the code but not real use; handle the key the way your team normally does and keep real keys out of shared files. Then confirm that the openai package is present. The source does not describe how to meet these conditions, and this batch installed and called nothing.
Point the client at the Beta address: When you create the OpenAI client, set base_url to https://api.deepseek.com/beta. The source states this setting is needed to enable the Beta feature. When you review the line, make sure the full /beta suffix is still there, since without it the request no longer matches what the documentation requires for FIM. Replace api_key with your own key.
Prepare the prefix and suffix: Put the opening you want to keep in prompt and the ending you want to keep in suffix. The official sample opens with def fib(a): and ends with return fib(a-1) + fib(a-2), so the model fills the middle of a Fibonacci function. The suffix string starts with four spaces, which are part of the Python indentation, so keep indentation consistent with the surrounding code when you adapt it.
Set the model and max_tokens, then send: Call client.completions.create with model="deepseek-flash", prompt, suffix and max_tokens=128. These four arguments are the entire request in the source sample. The value 128 is the sample's choice, not a recommendation, and the source does not say whether it suffices; if your middle section is longer you must adjust it yourself, keeping the page's 4K note in mind. Check the model name against the docs again before sending.
Read the returned text: The sample outputs the result with print(response.choices[0].text). The expectation is that it prints the generated middle text rather than a whole function, because the code never joins the prefix, completion and suffix. That is a reader expectation, not an observation from this batch, which ran nothing. If the call fails or returns no text, check base_url, the key and the parameters against the official docs first.
Join the pieces and check the meaning: Join the prefix, the completion and the suffix in order, then check each point: indentation is continuous, variable names match, the completion does not repeat the prefix or suffix, and the logic fits your intent. The sample code does not do this for you, so printing text does not mean the whole function is correct. When you finish, remove any real key from temporary files.
Worked example
The source example completes a Fibonacci function. The prefix prompt is def fib(a):, the suffix suffix is return fib(a-1) + fib(a-2), max_tokens is 128, the model is deepseek-flash, and the endpoint is https://api.deepseek.com/beta. After calling client.completions.create, the code only prints response.choices[0].text. A reader should expect text that sits between those two lines, then join the three parts and inspect them. The source gives no actual output for the example, and this article does not invent one.
from openai import OpenAI
client = OpenAI(
api_key="<your api key>",
base_url="https://api.deepseek.com/beta",
)
response = client.completions.create(
model="deepseek-flash",
prompt="def fib(a):",
suffix=" return fib(a-1) + fib(a-2)",
max_tokens=128,
)
print(response.choices[0].text)
Check the result and failure paths
- Static check:
base_urlkeeps/beta, the request usescompletions.createwithprompt,suffixandmax_tokens, and there is nomessagesfield. - Semantic check: after joining prefix, completion and suffix, indentation, variables and logic match your intent, and the completion does not repeat the prefix or suffix.
- Failure check: if the request errors, comes back empty or the text is unsuitable, verify the key, address, model name and parameters against the official docs before changing numbers by guesswork.
- Completion criteria (editorial recommendation): use the completion only when it is nonempty and the joined prefix, completion and suffix pass static and semantic checks. Printed text alone does not complete the task.
- Failure or voluntary stop (editorial recommendation): if the request errors, returns empty text or fails either check, end this attempt and discard the completion. Correct the problem before deciding whether to send a new request. If you choose to stop, send no further requests. Remove real keys from temporary files after success, failure or stopping.
Scope and limits
- The source only says FIM is a Beta feature with a 4K max tokens figure; it does not cover pricing, rate limits or support in other models, and this article infers none of that.
- The sample prints only the completion text. It does not assemble or verify the full function, so it cannot show the completion is semantically correct.
- The page also mentions Continue, a VSCode plugin that supports code completion, and tells readers to consult its document for configuration. This article gives no configuration steps because the source provides none.
API parameters and the example come from official documentation. Joining code, static and semantic checks, failure or cancellation handling and key management are editorial recommendations. The product was not run in this batch. No dependency was installed and no real key was used.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.