BETA

Docs
Documentation

Python Function Calling

The tools / tool_choice parameters are forwarded exactly as sent — Apily doesn't rewrite them, so behavior matches the upstream provider's own function-calling support.

main.py
import json
from openai import OpenAI

client = OpenAI(base_url="https://apily.pro/v1", api_key="apily_your-api-key")

def get_current_weather(location, unit="celsius"):
    return json.dumps({"location": location, "temperature": "18", "unit": unit})

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

messages = [{"role": "user", "content": "What's the weather in Paris?"}]
response = client.chat.completions.create(model="gpt-5.6-sol", messages=messages, tools=tools, tool_choice="auto")

tool_calls = response.choices[0].message.tool_calls
if tool_calls:
    messages.append(response.choices[0].message)
    for call in tool_calls:
        args = json.loads(call.function.arguments)
        result = get_current_weather(**args)
        messages.append({"role": "tool", "tool_call_id": call.id, "content": result})

    final = client.chat.completions.create(model="gpt-5.6-sol", messages=messages, tools=tools)
    print(final.choices[0].message.content)
Not every catalog model supports tool calling — check the model's provider docs if tool_calls keeps coming back empty.