Skip to content
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
15 changes: 14 additions & 1 deletion ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,19 @@ class Function(SubscriptableBaseModel):
description: Optional[str] = None

class Parameters(SubscriptableBaseModel):
model_config = ConfigDict(populate_by_name=True)
type: Optional[Literal['object']] = 'object'
defs: Optional[Any] = Field(None, alias='$defs')
items: Optional[Any] = None
required: Optional[Sequence[str]] = None

class Property(SubscriptableBaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

type: Optional[Union[str, Sequence[str]]] = None
items: Optional[Any] = None
description: Optional[str] = None
enum: Optional[Sequence] = None
enum: Optional[Sequence[Any]] = None

properties: Optional[Mapping[str, Property]] = None

Expand All @@ -325,6 +329,15 @@ class Property(SubscriptableBaseModel):


class ChatRequest(BaseGenerateRequest):
@model_serializer(mode='wrap')
def serialize_model(self, nxt):
output = nxt(self)
if 'tools' in output and output['tools']:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same pattern as the create request

for tool in output['tools']:
if 'function' in tool and 'parameters' in tool['function'] and 'defs' in tool['function']['parameters']:
tool['function']['parameters']['$defs'] = tool['function']['parameters'].pop('defs')
return output

messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None
'Messages to chat with.'

Expand Down
8 changes: 5 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def only_description():

tool = convert_function_to_tool(only_description).model_dump()
assert tool['function']['description'] == 'A function with only a description.'
assert tool['function']['parameters'] == {'type': 'object', 'properties': {}, 'required': None}
assert tool['function']['parameters'] == {'type': 'object', 'defs': None, 'items': None, 'required': None, 'properties': {}}

def only_description_with_args(x: int, y: int):
"""
Expand All @@ -199,9 +199,11 @@ def only_description_with_args(x: int, y: int):
assert tool['function']['description'] == 'A function with only a description.'
assert tool['function']['parameters'] == {
'type': 'object',
'defs': None,
'items': None,
'properties': {
'x': {'type': 'integer', 'description': '', 'enum': None},
'y': {'type': 'integer', 'description': '', 'enum': None},
'x': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
'y': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
},
'required': ['x', 'y'],
}
Expand Down