Skip to content
Open
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
3 changes: 3 additions & 0 deletions ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ class Function(SubscriptableBaseModel):
arguments: Mapping[str, Any]
'Arguments of the function.'

id: Optional[str] = None
'Identifier of the tool call, when provided by the server.'

function: Function
'Function to be called.'

Expand Down
18 changes: 17 additions & 1 deletion tests/test_type_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from ollama._types import CreateRequest, Image
from ollama._types import CreateRequest, Image, Message


def test_image_serialization_bytes():
Expand All @@ -31,6 +31,22 @@ def test_image_serialization_plain_string():
assert img.model_dump() == 'not a path or base64' # Should return as-is


def test_tool_call_id_round_trips():
# The server sends an `id` on each tool call (e.g. "call_p7o2gz50"); it must survive parsing.
message = Message(
role='assistant',
tool_calls=[{'id': 'call_p7o2gz50', 'function': {'name': 'test_function', 'arguments': {'param1': 'test1'}}}],
)
assert message.tool_calls[0].id == 'call_p7o2gz50'
assert message.tool_calls[0].function.name == 'test_function'
assert message.model_dump()['tool_calls'][0]['id'] == 'call_p7o2gz50'


def test_tool_call_id_defaults_to_none():
message = Message(role='assistant', tool_calls=[{'function': {'name': 'f', 'arguments': {}}}])
assert message.tool_calls[0].id is None


def test_image_serialization_path():
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(b'test file content')
Expand Down