Custom Role Map

Point OpenAIChat at Mistral's base_url and remap the `model` role to `assistant` via role_map.

Use a custom role map with the OpenAIChat class.

The source uses mistral-medium-2505, deprecated on May 22, 2026. Mistral recommends Medium 3.5 for new integrations. Deprecation does not mean immediate removal; choose a currently available model ID for your account before running this example.

custom_role_map.py
"""This example shows how to use a custom role map with the OpenAIChat class.

This is useful when using a custom model that doesn't support the default role map.

To run this example:
- Set the MISTRAL_API_KEY environment variable.
- Run `uv pip install openai agno` to install dependencies.
"""

from os import getenv

from agno.agent import Agent
from agno.models.openai import OpenAIChat

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# Using these Mistral model and url as an example.
model_id = "mistral-medium-2505"
base_url = "https://api.mistral.ai/v1"
api_key = getenv("MISTRAL_API_KEY")
mistral_role_map = {
    "system": "system",
    "user": "user",
    "assistant": "assistant",
    "tool": "tool",
    "model": "assistant",
}

# When initializing the model, we pass our custom role map.
model = OpenAIChat(
    id=model_id,
    base_url=base_url,
    api_key=api_key,
    role_map=mistral_role_map,
)

agent = Agent(model=model, markdown=True)

# Running the agent with a custom role map.
res = agent.print_response("Hey, how are you doing?")

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai

Export your API keys

export MISTRAL_API_KEY="your_mistral_api_key_here"

Run the example

Save the code above as custom_role_map.py, then run:

python custom_role_map.py

Full source: cookbook/90_models/openai/chat/custom_role_map.py