Skip to main content
Maitai supports OpenAI-style tool calling (tools + tool_choice). In Python, you can define tools either as raw OpenAI tool definitions, or using Maitai’s @tool decorator.

Option A: Raw OpenAI tool definitions

import json
import maitai

def get_current_weather(location: str, unit: str = "fahrenheit") -> str:
    return json.dumps({"location": location, "unit": unit, "temperature": "72"})

weather_tool = {
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "The city and state"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["location"],
        },
    },
}

available_functions = {
    "get_current_weather": get_current_weather,
}

client = maitai.MaitaiAsync()

messages = [{"role": "user", "content": "What's the weather in San Francisco?"}]

response = await client.chat.completions.create(
    messages=messages,
    application="demo_app",
    intent="WEATHER",
    tool_choice="auto",
    tools=[weather_tool],
)

for call in response.choices[0].message.tool_calls or []:
    fn = available_functions[call.function.name]
    args = json.loads(call.function.arguments)
    tool_result = fn(**args)
import maitai
from maitai.tools import Tools, tool

@tool(strict=True)
def get_current_weather(location: str, unit: str = "fahrenheit") -> str:
    """
    Get the current weather in a given location.

    Args:
        location: The city and state, e.g. San Francisco, CA
        unit: The unit of measurement (default is "fahrenheit")
    """
    return f"{location}: 72° ({unit})"

tools = Tools(get_current_weather)

client = maitai.MaitaiAsync()

messages = [{"role": "user", "content": "What's the weather in San Francisco?"}]

response = await client.chat.completions.create(
    messages=messages,
    application="demo_app",
    intent="WEATHER",
    tool_choice="auto",
    tools=tools,
)

for call in response.choices[0].message.tool_calls or []:
    tool_result = tools.invoke(call)