Skip to content
,

Ollama Tool Calling: Function Calling Guide with Python, JS and curl

8–11 minutes
Woman developer holding a card reading TOOLS, hook give your AI real tools

A plain chat model can only answer from what it already knows. Give it tools and it can check the weather, query your database, read files, run a calculator or call any API you write, all on your own PC with no cloud bill.

Ollama supports tool calling (also called function calling) through its normal chat API. This guide explains how it works, which local models handle it well, and how to build it step by step with curl, Python and JavaScript.

It covers single calls, parallel calls, a full agent loop and streaming, plus the mistakes that make models ignore your tools.

TL;DR

  • How: send a tools list (JSON schema) to /api/chat. The model replies with tool_calls. You run the function and send the result back as a "role": "tool" message.
  • The model never runs code itself. Your program runs the function. The model only picks which tool to call and with which arguments.
  • Python shortcut: pass plain Python functions with type hints and docstrings as tools=[my_func]. The SDK builds the schema for you.
  • Use a tool-capable model: look for the “tools” tag on ollama.com (Qwen3, Llama 3.1+, Mistral, gpt-oss and others).
  • Agents: loop until the model stops returning tool_calls.

New to Ollama? My Ollama guide covers install, models, commands and fixes in one place.

How Tool Calling Works

Tool calling is a short conversation between your code and the model:

  1. You describe your tools. Each tool has a name, a description and a JSON schema for its arguments.
  2. You send the user’s message plus the tools. The model decides whether a tool would help.
  3. The model returns tool_calls instead of (or alongside) a text answer, for example get_temperature(city="New York").
  4. Your code runs the real function and gets a result, such as “22°C”.
  5. You send the result back as a message with "role": "tool".
  6. The model writes the final answer using that result.

This keeps you in control. The model can only ask for tools you defined, and you decide what actually runs.

Which Models Support Tools

Not every model was trained for tool use. On the Ollama library, open a model page and look for the tools tag. Good local choices:

Model family Notes
Qwen3 (and newer Qwen) Strong tool use, supports thinking mode. Used in Ollama’s own examples.
gpt-oss Built for agentic use, reliable calls. See my gpt-oss local guide.
Llama 3.1 / 3.2 / newer Good support. Small 3B models work for simple tools.
Mistral / Mistral Small / Devstral Solid function calling.
Small agent models Tiny tool-callers like BotPsy 0.8B work for narrow tasks.

Bigger models pick the right tool and fill arguments far more reliably. For agents with several tools, a 7B-14B model or larger is a sensible minimum. Not sure what fits your GPU? See best Ollama models and my VRAM guide.

If you send tools to a model without tool support, Ollama returns an error saying the model does not support tools.

Step 1: A Single Tool Call with curl

First request: the question plus one tool definition.

curl -s http://localhost:11434/api/chat -d '{
  "model": "qwen3",
  "messages": [{"role": "user", "content": "What is the temperature in New York?"}],
  "stream": false,
  "tools": [{
    "type": "function",
    "function": {
      "name": "get_temperature",
      "description": "Get the current temperature for a city",
      "parameters": {
        "type": "object",
        "required": ["city"],
        "properties": {
          "city": {"type": "string", "description": "The name of the city"}
        }
      }
    }
  }]
}'

The reply’s message contains a tool_calls array, something like:

"tool_calls": [{
  "function": {"name": "get_temperature", "arguments": {"city": "New York"}}
}]

Note that arguments is already a JSON object in Ollama’s native API, not a string. Now run your function (pretend it returned 22°C) and send everything back:

curl -s http://localhost:11434/api/chat -d '{
  "model": "qwen3",
  "stream": false,
  "messages": [
    {"role": "user", "content": "What is the temperature in New York?"},
    {"role": "assistant", "tool_calls": [{"type": "function", "function": {"index": 0, "name": "get_temperature", "arguments": {"city": "New York"}}}]},
    {"role": "tool", "tool_name": "get_temperature", "content": "22°C"}
  ]
}'

The model now answers in plain text: “It is currently 22°C in New York.” More on the endpoints in my Ollama API guide.

Step 2: Python (Functions as Tools)

Install the official SDK:

pip install -U ollama

The Python SDK can turn normal functions into tool schemas. It reads the type hints and the Google-style docstring, so write both clearly:

from ollama import chat

def get_temperature(city: str) -> str:
    """Get the current temperature for a city

    Args:
        city: The name of the city

    Returns:
        The current temperature for the city
    """
    temps = {"New York": "22°C", "London": "15°C", "Tokyo": "18°C"}
    return temps.get(city, "Unknown")

messages = [{"role": "user", "content": "What is the temperature in New York?"}]
response = chat(model="qwen3", messages=messages, tools=[get_temperature])

messages.append(response.message)
if response.message.tool_calls:
    call = response.message.tool_calls[0]
    result = get_temperature(**call.function.arguments)
    messages.append({"role": "tool", "tool_name": call.function.name, "content": str(result)})
    final = chat(model="qwen3", messages=messages, tools=[get_temperature])
    print(final.message.content)
else:
    print(response.message.content)

Two details matter here. Append the model’s own message (with its tool_calls) before the tool result. And always convert the tool result to a string.

Step 3: Parallel Tool Calls

Ask “What’s the weather and temperature in New York and London?” with two tools, and a capable model returns several calls at once. Loop over all of them:

available = {"get_temperature": get_temperature, "get_conditions": get_conditions}

response = chat(model="qwen3", messages=messages, tools=list(available.values()))
messages.append(response.message)

for call in response.message.tool_calls or []:
    fn = available.get(call.function.name)
    result = fn(**call.function.arguments) if fn else "Unknown tool"
    messages.append({"role": "tool", "tool_name": call.function.name, "content": str(result)})

final = chat(model="qwen3", messages=messages, tools=list(available.values()))
print(final.message.content)

Send back one tool message per call, in the same order the calls arrived.

Step 4: A Full Agent Loop

Real tasks often need several rounds: look something up, then calculate, then answer. An agent loop keeps calling the model until it stops asking for tools:

from ollama import chat

def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

def multiply(a: int, b: int) -> int:
    """Multiply two numbers"""
    return a * b

tools = {"add": add, "multiply": multiply}
messages = [{"role": "user", "content": "What is (11434 + 12341) * 412? Use the tools."}]

for step in range(10):  # safety limit
    response = chat(model="qwen3", messages=messages, tools=list(tools.values()))
    messages.append(response.message)
    if not response.message.tool_calls:
        print(response.message.content)
        break
    for tc in response.message.tool_calls:
        fn = tools.get(tc.function.name)
        result = fn(**tc.function.arguments) if fn else "Unknown tool"
        messages.append({"role": "tool", "tool_name": tc.function.name, "content": str(result)})

Ollama’s docs suggest it can help to tell the model it is in a loop and can make several tool calls. Always keep a step limit so a confused model cannot loop forever.

For bigger agent projects with memory and many tools, frameworks help. See my LangChain + Ollama guide and how to create an AI agent.

Tool Calling with Streaming

With stream=True, text and tool calls arrive in chunks. Collect content, thinking and tool_calls from every chunk. Then add one assistant message with all three before you append the tool results:

content, thinking, tool_calls = "", "", []
for chunk in chat(model="qwen3", messages=messages, tools=[get_temperature], stream=True):
    if chunk.message.thinking:
        thinking += chunk.message.thinking
    if chunk.message.content:
        content += chunk.message.content
        print(chunk.message.content, end="", flush=True)
    if chunk.message.tool_calls:
        tool_calls.extend(chunk.message.tool_calls)

messages.append({"role": "assistant", "content": content, "thinking": thinking, "tool_calls": tool_calls})

Then run the tools and loop exactly as in the agent example.

JavaScript Example

import ollama from 'ollama'

const tools = [{
  type: 'function',
  function: {
    name: 'get_temperature',
    description: 'Get the current temperature for a city',
    parameters: {
      type: 'object',
      required: ['city'],
      properties: { city: { type: 'string', description: 'The name of the city' } },
    },
  },
}]

const messages = [{ role: 'user', content: 'What is the temperature in Tokyo?' }]
const res = await ollama.chat({ model: 'qwen3', messages, tools })
messages.push(res.message)

for (const call of res.message.tool_calls ?? []) {
  const result = call.function.arguments.city === 'Tokyo' ? '18°C' : 'Unknown'
  messages.push({ role: 'tool', tool_name: call.function.name, content: result })
}

const final = await ollama.chat({ model: 'qwen3', messages, tools })
console.log(final.message.content)

Install it with npm i ollama.

OpenAI-Compatible Endpoint

Ollama also serves an OpenAI-style API at http://localhost:11434/v1. Tools work there in the usual OpenAI format, so many existing agent frameworks and coding tools work by just changing the base URL:

from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
resp = client.chat.completions.create(model="qwen3", messages=messages, tools=openai_style_tools)

One difference: in the OpenAI format, arguments comes back as a JSON string, so call json.loads() on it.

Tips for Reliable Tool Use

  • Write clear descriptions. The model picks tools from the name and description. “Get the current temperature for a city” beats “temp tool”.
  • Keep the tool list short. Five well-described tools work better than twenty on a local model.
  • Use simple argument types (strings, numbers, enums) and mark required fields.
  • Validate arguments before running anything. Never pass model output straight into shell commands or SQL.
  • Raise the context. Tool schemas and results use tokens. Ollama’s default context can be as low as 4k, so see my context length guide.
  • Lower temperature (for example 0.2 to 0.5) for more consistent calls.
  • Return useful errors. If a tool fails, send the error text back as the tool result so the model can try again.

Troubleshooting

“registry.ollama.ai/library/xxx does not support tools”

That model has no tool support in its template. Switch to a model with the tools tag, or update the model with ollama pull. Older copies may lack the newer template.

The model answers in text and never calls the tool

  • The question may not obviously need the tool. Make the description match the kind of question users ask.
  • Small models often skip tools. Try a larger model.
  • Add a short system message: “Use the provided tools when they help answer the question.”

The model invents tool names or wrong arguments

Check that names are simple (letters and underscores), that schemas are valid JSON, and that required fields are listed. Handle unknown tools gracefully, as in the loop example.

KeyError or TypeError when running the function

The model sent an argument name your function does not accept. Print call.function.arguments to see what arrived, and make the schema property names match your function parameters exactly.

Slow responses with tools

Long tool results fill the context. Return only what the model needs (a summary, not a whole web page), and make sure the model runs fully on the GPU. Check with ollama ps.

FAQ

Is tool calling the same as function calling?

Yes. They are two names for the same feature.

Does the model run my code?

No. The model only returns the function name and arguments. Your program decides whether to run it.

Can I use tool calling with thinking models?

Yes. Pass think=True with a thinking model like Qwen3. The reasoning arrives in message.thinking and the calls in tool_calls.

Does it work with Ollama cloud models?

Tool calling works through the same chat API. Check each model’s page for the tools tag.

How do I get JSON answers without tools?

Use structured outputs with the format field. See my Ollama structured outputs guide.

Can LM Studio do this too?

Yes, through its OpenAI-compatible server. See my LM Studio API guide.

Sources

Free weekly email

Get new AI guides every week

One short weekly email with our newest local AI, ComfyUI and AI tool guides. No spam, no pop-ups, just the good stuff.

Free. Unsubscribe anytime in one click. We only use your email to send this newsletter. See our privacy policy.

Woman developer holding a card reading TOOLS, hook give your AI real tools