FIM Completion
FIM (Fill in the Middle) completion lets the model generate content between a prefix and suffix — ideal for code completion, text filling, and similar scenarios.
FIM (Fill in the Middle) is a special completion mode provided by DeepSeek. Unlike conversational generation, FIM receives a prefix (prompt) and an optional suffix of a text, and the model generates the missing middle portion. This mode is particularly suited for code completion — when a user is typing in an editor, FIM can intelligently complete the intermediate code based on the context before and after the cursor.
Basic Usage
Use the fim() function for FIM completion. You need to provide a model instance and prefix text:
The model generates subsequent content based on the prefix, such as completing the Fibonacci function implementation.
Completion with Suffix
Provide suffix text via the suffix parameter, and the model generates the content between the prefix and suffix:
This is especially useful in code editor scenarios — the user's cursor is in the middle of the code, and you need to complete the current line or block based on the content before and after the cursor.
Calling via Model Instance
You can also call FIM directly through a DeepSeekModel instance:
When calling via model.fim(), the DeepSeek Beta mode API endpoint (api.deepseek.com/beta) is automatically used, which is required for FIM functionality.
Parameters
prompt — Prefix Text
The prefix text is the core input for FIM completion. The model generates subsequent content based on this text:
suffix — Suffix Text
The suffix text tells the model what comes after the generated content, helping it produce more accurate intermediate content:
echo — Echo
Setting echo: true includes the prefix text in the response, allowing you to see the full context:
maxTokens — Maximum Generation Tokens
Limits the maximum number of tokens the model can generate, controlling the length of the completion:
Return Result
The fim() function returns a simplified result object containing text and usage:
Calling via model.fim() returns the full FIMResponse object:
Code Completion Example
Here's a complete example simulating code editor completion:
The model generates the validateUser function implementation based on the surrounding context, such as using UserSchema.safeParse(input) for validation.
API Reference
fim() Parameters
falsefim() Return Value
prompt_tokens, completion_tokens, and total_tokens. model.fim() Return Value (FIMResponse)
text (generated text), finish_reason (completion reason), and logprobs (log probabilities). 
