|
1 |
| -from __future__ import annotations |
| 1 | +# from __future__ import annotations |
2 | 2 |
|
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 |
5 | 4 | from ollama._types import Tool
|
6 | 5 |
|
| 6 | +# Map both the type and the type reference to the same JSON type |
7 | 7 | PYTHON_TO_JSON_TYPES = {
|
8 |
| - str: 'string', |
9 | 8 | int: 'integer',
|
| 9 | + 'int': 'integer', |
| 10 | + str: 'string', |
| 11 | + 'str': 'string', |
10 | 12 | float: 'number',
|
| 13 | + 'float': 'number', |
11 | 14 | bool: 'boolean',
|
| 15 | + 'bool': 'boolean', |
12 | 16 | list: 'array',
|
| 17 | + 'list': 'array', |
13 | 18 | dict: 'object',
|
14 |
| - List: 'array', |
15 |
| - Dict: 'object', |
16 |
| - None: 'null', |
| 19 | + 'dict': 'object', |
| 20 | + type(None): 'null', |
| 21 | + 'None': 'null', |
17 | 22 | }
|
18 | 23 |
|
19 | 24 | """
|
|
26 | 31 |
|
27 | 32 |
|
28 | 33 | def _get_json_type(python_type: Any) -> str | List[str]:
|
| 34 | + # Print for debugging |
| 35 | + |
29 | 36 | # Handle Optional types (Union[type, None] and type | None)
|
30 | 37 | 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: |
32 | 39 | args = get_args(python_type)
|
33 | 40 | # Filter out None/NoneType from union args
|
34 | 41 | 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] |
36 | 42 | if non_none_args:
|
37 | 43 | if len(non_none_args) == 1:
|
38 | 44 | return _get_json_type(non_none_args[0])
|
39 | 45 | else:
|
40 |
| - return non_none_args_types |
41 |
| - |
| 46 | + return [_get_json_type(arg) for arg in non_none_args] |
42 | 47 | return 'null'
|
43 | 48 |
|
44 |
| - # Get basic type mapping |
| 49 | + # Direct type check first (like int, str, etc.) |
45 | 50 | if python_type in PYTHON_TO_JSON_TYPES:
|
46 | 51 | return PYTHON_TO_JSON_TYPES[python_type]
|
47 | 52 |
|
48 | 53 | # 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 | + |
51 | 60 |
|
52 |
| - # TODO: Default to string if type is unknown - define beahvior |
53 |
| - return 'string' |
| 61 | +# return 'string' |
54 | 62 |
|
55 | 63 |
|
56 | 64 | def _is_optional_type(python_type: Any) -> bool:
|
57 | 65 | 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: |
59 | 67 | args = get_args(python_type)
|
60 | 68 | return any(arg in (None, type(None)) for arg in args)
|
61 | 69 | return False
|
|
0 commit comments