> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fastrun.si/llms.txt
> Use this file to discover all available pages before exploring further.

# Tool calling

> Let the model call functions in your code.

Describe your functions in `tools`. When the model decides to use one, it answers with `tool_calls` instead of text.
Your code runs the function and sends the result back in a `tool` message, and the model continues from there.

```python Python theme={null}
import json
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.fastrun.si/v1", api_key=os.environ["FASTRUN_API_KEY"])

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Current weather in a city.",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"],
            },
        },
    }
]

def get_weather(city: str) -> dict:
    return {"city": city, "temperature_c": 18, "sky": "clear"}  # your real implementation

messages = [{"role": "user", "content": "What's the weather in Ljubljana?"}]
res = client.chat.completions.create(model="google/gemma-4-31b-it", messages=messages, tools=tools)
msg = res.choices[0].message

if msg.tool_calls:
    messages.append(msg)
    for call in msg.tool_calls:
        args = json.loads(call.function.arguments)
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": json.dumps(get_weather(**args)),
        })
    res = client.chat.completions.create(model="google/gemma-4-31b-it", messages=messages, tools=tools)

print(res.choices[0].message.content)
```

When the model calls a tool, `finish_reason` is `tool_calls` and `message.tool_calls` holds the calls:

```json theme={null}
{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "chatcmpl-tool-...",
      "type": "function",
      "function": { "name": "get_weather", "arguments": "{\"city\": \"Ljubljana\"}" }
    }
  ]
}
```

`arguments` is a JSON string. Parse it, and validate it before you act on it: the model can make mistakes.

## Choosing when tools are used

| `tool_choice` | Effect |
| - | - |
| `"auto"` (default) | The model decides whether to call a tool. |
| `"none"` | The model answers with text. |
| `"required"` | The model must call at least one tool. |
| `{"type": "function", "function": {"name": "get_weather"}}` | The model must call this tool. |

## Streaming

Tool calls also work with `stream: true`. They arrive in pieces in `delta.tool_calls`; OpenAI SDKs offer helpers that
put them together for you.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.