Skip to content

types: relax type for tools #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __contains__(self, key: str) -> bool:
if key in self.model_fields_set:
return True

if value := self.model_fields.get(key):
if value := self.__class__.model_fields.get(key):
return value.default is not None

return False
Expand Down Expand Up @@ -313,7 +313,7 @@ class Function(SubscriptableBaseModel):


class Tool(SubscriptableBaseModel):
type: Optional[Literal['function']] = 'function'
type: Optional[str] = 'function'

class Function(SubscriptableBaseModel):
name: Optional[str] = None
Expand Down
3 changes: 2 additions & 1 deletion ollama/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ def convert_function_to_tool(func: Callable) -> Tool:
}

tool = Tool(
type='function',
function=Tool.Function(
name=func.__name__,
description=schema.get('description', ''),
parameters=Tool.Function.Parameters(**schema),
)
),
)

return Tool.model_validate(tool)
11 changes: 6 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest
from httpx import Response as httpxResponse
from pydantic import BaseModel, ValidationError
from pydantic import BaseModel
from pytest_httpserver import HTTPServer, URIPattern
from werkzeug.wrappers import Request, Response

Expand Down Expand Up @@ -1136,10 +1136,11 @@ def func2(y: str) -> int:


def test_tool_validation():
# Raises ValidationError when used as it is a generator
with pytest.raises(ValidationError):
invalid_tool = {'type': 'invalid_type', 'function': {'name': 'test'}}
list(_copy_tools([invalid_tool]))
arbitrary_tool = {'type': 'custom_type', 'function': {'name': 'test'}}
tools = list(_copy_tools([arbitrary_tool]))
assert len(tools) == 1
assert tools[0].type == 'custom_type'
assert tools[0].function.name == 'test'


def test_client_connection_error():
Expand Down
Loading