How to enable functions to be called in Mistral agent using standard JSON mode format

In this tutorial, we will demonstrate how to enable function calling functions using the standard JSON pattern format. By defining the input parameters of the function with a clear architecture, you can make agents seamlessly call your custom tools for powerful dynamic interactions.
We will use the AviationStack API to retrieve real-time flight status data and show how to integrate external APIs into callable features in Mistral proxy.
Step 1: Set up the dependency
Install the Mistral library
Loading the Mistral API key
You can get an API key from it
from getpass import getpass
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
Loading the Aviation Stack API Key
You can start by registering free API keys on its dashboard.
AVIATIONSTACK_API_KEY = getpass('Enter Aviation Stack API: ')
Step 2: Define custom functions
Next, we define a python function get_flight_status() that calls the AviationStack API to retrieve the real-time state of the flight. This feature accepts optional Flight_IATA parameters and returns key details such as airline name, flight status, departure and arrival airport, and planned time. If a matching flight is not found, it will elegantly return the error message.
import requests
from typing import Dict
def get_flight_status(flight_iata=None):
"""
Retrieve flight status using optional filters: dep_iata, arr_iata, flight_iata.
"""
params = {
"access_key": AVIATIONSTACK_API_KEY,
"flight_iata": flight_iata
}
response = requests.get(" params=params)
data = response.json()
if "data" in data and data["data"]:
flight = data["data"][0]
return {
"airline": flight["airline"]["name"],
"flight_iata": flight["flight"]["iata"],
"status": flight["flight_status"],
"departure_airport": flight["departure"]["airport"],
"arrival_airport": flight["arrival"]["airport"],
"scheduled_departure": flight["departure"]["scheduled"],
"scheduled_arrival": flight["arrival"]["scheduled"],
}
else:
return {"error": "No flight found for the provided parameters."}
Step 3: Create a Mistral Client and Agent
In this step, we create a Mistral agent that uses tool names to get real-time flight information. The agent is called the Flight Status Agent, configured to use the “Mistral-Medium-2505” model and is equipped with a custom feature tool called get_flight_status. The tool is defined using a JSON pattern that accepts a single required parameter: the IATA code for the flight (e.g., “AI101”). Once deployed, the agent can automatically call this feature when detecting relevant user queries, allowing seamless integration between natural language input and structured API response.
from mistralai import Mistral
client = Mistral(MISTRAL_API_KEY)
flight_status_agent = client.beta.agents.create(
model="mistral-medium-2505",
description="Provides real-time flight status using aviationstack API.",
name="Flight Status Agent",
tools=[
{
"type": "function",
"function": {
"name": "get_flight_status",
"description": "Retrieve the current status of a flight by its IATA code (e.g. AI101).",
"parameters": {
"type": "object",
"properties": {
"flight_iata": {
"type": "string",
"description": "IATA code of the flight (e.g. AI101)"
},
},
"required": ["flight_iata"]
}
}
}
]
)
Step 4: Start the conversation and handle function calls
In this step, we talk to the Flight State Agent by asking a natural language question: “What is the current state of AI101?”. The Mistral model detects that the get_flight_status function should be called and returns the function call request. We parse the parameters, run the function locally using the AviationStack API, and then use FunctionResultry to return the result to the proxy. Finally, the model incorporates API responses and generates a natural language reply in the current flight state, which we print to the console.
from mistralai import FunctionResultEntry
import json
# User starts a conversation
response = client.beta.conversations.start(
agent_id=flight_status_agent.id,
inputs=[{"role": "user", "content": "What's the current status of AI101?"}]
)
# Check if model requested a function call
if response.outputs[-1].type == "function.call" and response.outputs[-1].name == "get_flight_status":
args = json.loads(response.outputs[-1].arguments)
# Run the function
function_result = json.dumps(get_flight_status(**args))
# Create result entry
result_entry = FunctionResultEntry(
tool_call_id=response.outputs[-1].tool_call_id,
result=function_result
)
# Return result to agent
response = client.beta.conversations.append(
conversation_id=response.conversation_id,
inputs=[result_entry]
)
print(response.outputs[-1].content)
else:
print(response.outputs[-1].content)
View notebooks on Github. All credits for this study are to the researchers on the project. Also, please stay tuned for us twitter And don’t forget to join us 95k+ ml reddit And subscribe Our newsletter.
I am a civil engineering graduate in Islamic Islam in Jamia Milia New Delhi (2022) and I am very interested in data science, especially neural networks and their applications in various fields.