Skip to content
Search

Ollama:Python

lastmod 2025-04-11created 2025-03-295 edits

Ollama Python Library

Examples

REST API Spec

Passing Python functions as tools

First, define a regular Python function. For better results, annotate the parameter and return value types, and optionally add a Google-style docstring .

def add_two_numbers(a: int, b: int) -> int:
  """
  Add two numbers

  Args:
    a: The first integer number
    b: The second integer number

  Returns:
    int: The sum of the two numbers
  """
  return a + b

Next, pass the function as a tool to Ollama using the tools field.

import ollama

response = ollama.chat(
  'llama3.1',
  messages=[{'role': 'user', 'content': 'What is 10 + 10?'}],
  tools=[add_two_numbers], # Actual function reference
)

Use the returned tool call and arguments provided by the model to call the corresponding function.

available_functions = {
  'add_two_numbers': add_two_numbers,
}

for tool in response.message.tool_calls or []:
  function_to_call = available_functions.get(tool.function.name)
  if function_to_call:
    print('Function output:', function_to_call(**tool.function.arguments))
  else:
    print('Function not found:', tool.function.name)

Troubleshooting

pydantic_core._pydantic_core.ValidationError: 1 validation error for ShowResponse

pydantic_core._pydantic_core.ValidationError: 1 validation error for ShowResponse
model_info
  Field required [type=missing, input_value={'modelfile': '# Modelfil...ization_level': 'Q8_0'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing

See also

Favorite site

author Zer0 sitemap