Tool calling lets an Ollama model request a function call instead of guessing information it does not have. The application executes the requested tool, appends the tool result to the conversation, and asks the model for the final answer.

A tool definition is a JSON schema for function arguments. The first model response may contain tool_calls with no final content, which is the signal for application code to run the tool and continue the chat.

Use a small deterministic tool while validating the message flow. Once the request and response shapes are correct, replace the stub with the real service lookup or calculation.

Steps to use tool calling with the Ollama API:

  1. Send a chat request with a single function tool definition.
    $ curl -s http://localhost:11434/api/chat -d '{
      "model":"gpt-oss:20b",
      "messages":[{"role":"user","content":"What is the temperature in Paris?"}],
      "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"}}}}}]
    }'
    {"message":{"content":"","tool_calls":[{"function":{"name":"get_temperature","arguments":{"city":"Paris"}}}]},"done":true}
  2. Run the requested tool in application code.
    $ python3 - <<'PY'
    def get_temperature(city):
        return {'city': city, 'temperature': '22 C'}
    print(get_temperature('Paris'))
    PY
    {'city': 'Paris', 'temperature': '22 C'}
  3. Append the tool result and request the final answer.
    $ curl -s http://localhost:11434/api/chat -d '{
      "model":"gpt-oss:20b",
      "messages":[
        {"role":"user","content":"What is the temperature in Paris?"},
        {"role":"assistant","tool_calls":[{"type":"function","function":{"name":"get_temperature","arguments":{"city":"Paris"}}}]},
        {"role":"tool","tool_name":"get_temperature","content":"22 C"}
      ],
      "stream":false
    }'
    {"message":{"content":"The temperature in Paris is 22 C."},"done":true}
  4. Reject unknown tool names before executing anything.
    $ python3 - <<'PY'
    allowed = {'get_temperature'}
    print('delete_file' in allowed)
    PY
    False
  5. Verify the tool name and sanitized arguments are safe to log.
    $ printf '%s\n' 'tool=get_temperature city=Paris'
    tool=get_temperature city=Paris