Reference · Snippet · python

Function call handler

toolspythonopenai

def get_weather(city: str) -> str:
    return f"Sunny, 32C in {city}"

TOOLS = {"get_weather": get_weather}

# model returns tool_call = {"name": "get_weather", "arguments": {"city": "Dubai"}}
def run_tool_call(tool_call: dict) -> str:
    name = tool_call["name"]
    args = tool_call["arguments"]
    if name not in TOOLS:
        raise ValueError("Tool not allowed")
    return TOOLS[name](**args)