Module 2 of 3 · 45 min
Mistral Function Calling & Codestral FIM
Build tool-enabled workflows with native Mistral function calling and implement Fill-in-the-Middle code generation.
Core concept
By the end
You will be able to
- Declare and execute typed tool schemas with Mistral function calling.
- Implement Fill-in-the-Middle (FIM) prompt formatting with `prompt` and `suffix`.
- Enforce strict JSON mode responses with response format constraints.
01
Fill-in-the-Middle (FIM) Completion
Code generation in IDEs rarely happens strictly from top to bottom. Codestral takes both prefix code (before cursor) and suffix code (after cursor), generating the exact bridge snippet needed.
Codestral FIM Completion Request
python
from mistralai import Mistral
client = Mistral(api_key="your-api-key")
response = client.fim.complete(
model="codestral-latest",
prompt="def calculate_discount(price: float, discount_pct: float) -> float:\n",
suffix="\n return round(final_price, 2)"
)
print(response.choices[0].message.content)Practice activity
Implement an IDE Code Completion Hook with Codestral
- Create a script simulating IDE cursor insertion.
- Send prefix and suffix parameters to Codestral FIM endpoint.
- Verify that returned code merges seamlessly into the target function.
What to produce
- Completed FIM test run logs and merged file output.
Reflect before continuing
Why does FIM completion produce cleaner insertions than standard prefix-only generation?
Evidence
Sources and verification
- Codestral: Dedicated Code Model by Mistral AIMistral AI · verified 2026-08-22
Knowledge check
Make it stick.
Choose the strongest answer for each question. Your attempts become part of your account transcript.