Skip to content

Commit 93c7a63

Browse files
committed
wip
1 parent 1ef75a7 commit 93c7a63

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

ollama/_utils.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
from __future__ import annotations
1+
# from __future__ import annotations
22

3-
from types import UnionType
4-
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Union, get_args, get_origin
3+
from typing import Any, Callable, List, Mapping, Optional, Sequence, Union, get_args, get_origin
54
from ollama._types import Tool
65

6+
# Map both the type and the type reference to the same JSON type
77
PYTHON_TO_JSON_TYPES = {
8-
str: 'string',
98
int: 'integer',
9+
'int': 'integer',
10+
str: 'string',
11+
'str': 'string',
1012
float: 'number',
13+
'float': 'number',
1114
bool: 'boolean',
15+
'bool': 'boolean',
1216
list: 'array',
17+
'list': 'array',
1318
dict: 'object',
14-
List: 'array',
15-
Dict: 'object',
16-
None: 'null',
19+
'dict': 'object',
20+
type(None): 'null',
21+
'None': 'null',
1722
}
1823

1924
"""
@@ -26,36 +31,39 @@
2631

2732

2833
def _get_json_type(python_type: Any) -> str | List[str]:
34+
# Print for debugging
35+
2936
# Handle Optional types (Union[type, None] and type | None)
3037
origin = get_origin(python_type)
31-
if origin is UnionType or origin is Union:
38+
if origin is type(int | str) or origin is Union:
3239
args = get_args(python_type)
3340
# Filter out None/NoneType from union args
3441
non_none_args = [arg for arg in args if arg not in (None, type(None))]
35-
non_none_args_types = [PYTHON_TO_JSON_TYPES[arg] for arg in non_none_args]
3642
if non_none_args:
3743
if len(non_none_args) == 1:
3844
return _get_json_type(non_none_args[0])
3945
else:
40-
return non_none_args_types
41-
46+
return [_get_json_type(arg) for arg in non_none_args]
4247
return 'null'
4348

44-
# Get basic type mapping
49+
# Direct type check first (like int, str, etc.)
4550
if python_type in PYTHON_TO_JSON_TYPES:
4651
return PYTHON_TO_JSON_TYPES[python_type]
4752

4853
# Handle typing.List, typing.Dict etc.
49-
if origin in PYTHON_TO_JSON_TYPES:
50-
return PYTHON_TO_JSON_TYPES[origin]
54+
if origin is not None:
55+
return PYTHON_TO_JSON_TYPES.get(origin, 'string')
56+
57+
# Default to string if type is unknown
58+
raise ValueError(f'Unknown type: {python_type}')
59+
5160

52-
# TODO: Default to string if type is unknown - define beahvior
53-
return 'string'
61+
# return 'string'
5462

5563

5664
def _is_optional_type(python_type: Any) -> bool:
5765
origin = get_origin(python_type)
58-
if origin is UnionType or origin is Union:
66+
if origin is type(int | str) or origin is Union:
5967
args = get_args(python_type)
6068
return any(arg in (None, type(None)) for arg in args)
6169
return False

0 commit comments

Comments
 (0)