TImon Harz
December 14, 2024
Ollama Python library 0.4 with function calling improvements
Discover how to pass Python functions as tools to Ollama and how the library automatically generates JSON schema from function definitions. Explore the new updates in the Ollama Python library for improved functionality and typing support.

Get started
To begin, install or upgrade the Ollama Python library:
Passing Python functions as tools
Define a Python function
Start by defining a regular Python function. For better results, annotate the parameter and return types, and optionally add a Google-style docstring:
Pass the function as a tool to Ollama
Next, pass the function as a tool to Ollama using the tools
field:
Call the function from the model response
Use the tool call and arguments provided by the model to call the respective function:
Pass existing functions as tools
You can now pass functions from existing Python libraries, SDKs, and other sources as tools. For instance, the following code demonstrates passing the request
function from the requests
library as a tool to fetch the contents of the Ollama website:
How it works: Generating JSON Schema from Functions
The Ollama Python library utilizes Pydantic and docstring parsing to automatically generate the JSON schema. For example, consider the add_two_numbers
function defined earlier in this post. The following JSON schema is generated (which was previously required to be provided manually as a tool):
Additional Improvements to the Ollama Python Library
The 0.4 release of the Ollama Python library introduces several enhancements:
Examples have been updated on the Ollama Python GitHub.
Full typing support across the library, enabling direct object access while preserving existing functionality.
Press contact
Timon Harz
oneboardhq@outlook.com
Other posts
Company
About
Blog
Careers
Press
Legal
Privacy
Terms
Security