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:

import { createModel, fim } from 'deepseek-kit'

const model = createModel({ model: 'deepseek-v4-flash' })

const result = await fim({
  model,
  prompt: 'function fibonacci(n) {',
})

console.log(result.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:

const result = await fim({
  model,
  prompt: 'function add(a, b) {\n  ',
  suffix: '\n  return result\n}',
})

console.log(result.text)
// "const result = a + b;"

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:

import { createModel } from 'deepseek-kit'

const model = createModel({ model: 'deepseek-v4-flash' })

const result = await model.fim({
  prompt: 'const greeting = "Hello, ',
  suffix: '!"',
  maxTokens: 100,
})

console.log(result.choices[0].text)

Parameters

prompt — Prefix Text

The prefix text is the core input for FIM completion. The model generates subsequent content based on this text:

const result = await fim({
  model,
  prompt: 'import { useState } from "react"\n\nfunction Counter() {',
})

suffix — Suffix Text

The suffix text tells the model what comes after the generated content, helping it produce more accurate intermediate content:

const result = await fim({
  model,
  prompt: 'function formatDate(date) {\n  ',
  suffix: '\n  return formatted\n}',
})

echo — Echo

Setting echo: true includes the prefix text in the response, allowing you to see the full context:

const result = await fim({
  model,
  prompt: 'function hello() {',
  echo: true,
})

console.log(result.choices[0].text)
// "function hello() { console.log('Hello!') }"

maxTokens — Maximum Generation Tokens

Limits the maximum number of tokens the model can generate, controlling the length of the completion:

const result = await fim({
  model,
  prompt: 'function quickSort(arr) {',
  maxTokens: 256,
})

Return Result

The fim() function returns a simplified result object containing text and usage:

const result = await fim({
  model,
  prompt: 'const sum = ',
})

console.log(result.text)
console.log(result.usage)

Calling via model.fim() returns the full FIMResponse object:

const response = await model.fim({
  prompt: 'const sum = ',
})

console.log(response.id)
console.log(response.choices[0].text)
console.log(response.choices[0].finish_reason)
console.log(response.usage)

Code Completion Example

Here's a complete example simulating code editor completion:

import { createModel, fim } from 'deepseek-kit'

const model = createModel({ model: 'deepseek-v4-flash' })

const prefix = `
import { z } from 'zod'

export const UserSchema = z.object({
  name: z.string(),
  email: z.string().email(),
  age: z.number().int().positive(),
})

export function validateUser(input: unknown) {
  `

const suffix = `
}

const result = validateUser({ name: 'John', email: 'test@example.com', age: 25 })
console.log(result)
`

const result = await fim({
  model,
  prompt: prefix,
  suffix,
  maxTokens: 200,
})

console.log(result.text)

The model generates the validateUser function implementation based on the surrounding context, such as using UserSchema.safeParse(input) for validation.

API Reference

fim() Parameters

modelrequiredDeepSeekModel
Model instance. The reasoning engine used for FIM completion.
promptrequiredstring
Prefix text. The model generates subsequent content based on this text.
suffixstring
Suffix text. Provides context after the generated content, helping the model produce more accurate intermediate content.
echoboolean
false
Whether to include the prefix text in the response.
maxTokensnumber
Maximum number of tokens to generate.

fim() Return Value

textstring
The completion text generated by the model.
usageFIMUsage
Token usage statistics, including prompt_tokens, completion_tokens, and total_tokens.

model.fim() Return Value (FIMResponse)

idstring
Unique identifier for the completion request.
choicesFIMChoice[]
List of completion choices. Each item contains text (generated text), finish_reason (completion reason), and logprobs (log probabilities).
modelstring
Model identifier used.
usageFIMUsage
Token usage statistics.