Clatter LabsHelp Center

How do I use tool calling in Python?

How-toUpdated

Pass tools. When the reply has tool_calls (finish reason tool_calls), run your function and send the result back with the matching tool_call_id.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.gateway.clatterlabs.xyz/v1",
    api_key=os.environ["CLATTER_API_KEY"],
    default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]},
)
import json

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
    },
}]

def get_weather(city):
    return {"temp_c": 18, "sky": "light rain"}  # your real lookup here

msgs = [{"role": "user", "content": "What's the weather in Paris?"}]
r = client.chat.completions.create(model="supercosmos-max", messages=msgs, tools=tools)
msg = r.choices[0].message
while msg.tool_calls:
    msgs.append(msg)
    for call in msg.tool_calls:
        args = json.loads(call.function.arguments)
        msgs.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(get_weather(**args))})
    r = client.chat.completions.create(model="supercosmos-max", messages=msgs, tools=tools)
    msg = r.choices[0].message
print(msg.content)

Up to 64 tools per request. The loop handles several tool calls in one turn and follow-up calls.

The client setup above (base URL, key and the X-Clatter-Account header) is the same in every Python example; see Set up the Python SDK. Full parameter list: Create a chat completion.

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.