We’ll show how you can enable function calls in Mistral Agents with the JSON standard schema. By defining your function’s input parameters with a clear schema, you can make your custom tools seamlessly callable by the agent—enabling powerful, dynamic interactions.
The AviationStack API will be used to fetch real-time data on flight status. This demonstration shows how APIs from external sources can be called as functions in a Mistral Agent.
Set up Dependencies
Mistral: Install the library
Mistral API key loading
Get an API Key from https://console.mistral.ai/api-keys
From getpass import Getpass
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
How to load the Aviation Stack API keys
The API keys are free. dashboard Get started
AVIATIONSTACK_API_KEY = getpass('Enter Aviation Stack API: ')
Step 2: Definition of the Custom Function
Next, we define a Python function get_flight_status() This function calls the AviationStack API in order to get the current status of the flight. This function takes an optional parameter flight_iata and returns important details like airline name, status of flight, airports for departure and arrival, and schedule times. It gracefully displays an error message if no flight matching is found.
Import requests
Type import Dict? params=
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
}
Get response to requests.get"http://api.aviationstack.com/v1/flights", params=params)
Data = response.json()
If you want to know more about if "data" Data and information["data"]:
The flight dataReturn["data"][0]
return {
"airline"Flying - a flight["airline"]["name"],
"flight_iata"Flying - a flight["flight"]["iata"],
"status"Flying - a flight["flight_status"],
"departure_airport"Flying - a flight["departure"]["airport"],
"arrival_airport"Flying - a flight["arrival"]["airport"],
"scheduled_departure"Flying - a flight["departure"]["scheduled"],
"scheduled_arrival"Flying - a flightReturn["arrival"]["scheduled"],
}
else:
return {"error": "No flight found for the provided parameters."}
Create the Mistral Client and Agent
We create an agent that will use tool-calling in order to get real-time information about flights. This agent is named Flight Status. It uses the “mistral-medium-2505” This model is fitted with a function named get_flight_status. This tool uses a JSON-based schema and accepts only one parameter as a required: the IATA code of the flight (e.g. “AI101”). The agent will automatically activate this feature when it detects an appropriate user request, thus allowing seamless integration of natural language queries and API responses.
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"]
}
}
}
]
)
Start the conversation and handle function calling
This step involves asking the flight status agent a question in natural language. “What’s the current status of AI101?”. The Mistral Model detects the need to call get_flight_status, and it returns a Function Call Request. Then we parse and run the AviationStack API to execute the function. We then return the results back to the agent via FunctionResultEntry. The model then incorporates API responses and creates a reply in natural language with the flight status. This is printed to the console.
Import FunctionResultEntry from Mistralai
Download json
A user initiates a conversation
response = client.beta.conversations.start(
agent_id=flight_status_agent.id,
inputs=[{"role": "user", "content": "What's the current status of AI101?"}]
)
# Verify if the model has requested that a call to a specific function be made.
if response.outputs[-1].type == "function.call" Outputs and responses[-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
)
Agent: Return the result #
response = client.beta.conversations.append(
conversation_id=response.conversation_id,
inputs=[result_entry]
)
print(response.outputs[-1].content)
else:
print(response.outputs[-1].content)
Take a look at the Notebook on GitHub. The researchers are the sole owners of all credit. Also, feel free to follow us on Twitter Don’t forget about our 95k+ ML SubReddit Subscribe Now our Newsletter.

