@@ -98,14 +98,14 @@ def test_process_tools():
98
98
def func1 (x : int ) -> str :
99
99
"""Simple function 1.
100
100
Args:
101
- x: A number
101
+ x (integer) : A number
102
102
"""
103
103
pass
104
104
105
105
def func2 (y : str ) -> int :
106
106
"""Simple function 2.
107
107
Args:
108
- y: A string
108
+ y (string) : A string
109
109
"""
110
110
pass
111
111
@@ -126,3 +126,81 @@ def func2(y: str) -> int:
126
126
assert len (tools ) == 2
127
127
assert tools [0 ].function .name == 'func1'
128
128
assert tools [1 ].function .name == 'test'
129
+
130
+
131
+ def test_advanced_json_type_conversion ():
132
+ from typing import Optional , Union , List , Dict , Sequence , Mapping , Set , Tuple , Any
133
+
134
+ # Test nested collections
135
+ assert _get_json_type (List [List [int ]]) == 'array'
136
+ assert _get_json_type (Dict [str , List [int ]]) == 'object'
137
+
138
+ # Test multiple unions
139
+ assert set (_get_json_type (Union [int , str , float ])) == {'integer' , 'string' , 'number' }
140
+
141
+ # Test collections.abc types
142
+ assert _get_json_type (Sequence [int ]) == 'array'
143
+ assert _get_json_type (Mapping [str , int ]) == 'object'
144
+ assert _get_json_type (Set [int ]) == 'array'
145
+ assert _get_json_type (Tuple [int , str ]) == 'array'
146
+
147
+ # Test nested optionals
148
+ assert _get_json_type (Optional [List [Optional [int ]]]) == 'array'
149
+
150
+ # Test edge cases
151
+ assert _get_json_type (Any ) == 'string' # or however you want to handle Any
152
+ assert _get_json_type (None ) == 'null'
153
+ assert _get_json_type (type (None )) == 'null'
154
+
155
+ # Test complex nested types
156
+ complex_type = Dict [str , Union [List [int ], Optional [str ], Dict [str , bool ]]]
157
+ assert _get_json_type (complex_type ) == 'object'
158
+
159
+
160
+ def test_invalid_types ():
161
+ # Test that invalid types raise appropriate errors
162
+ with pytest .raises (ValueError ):
163
+ _get_json_type (lambda x : x ) # Function type
164
+
165
+ with pytest .raises (ValueError ):
166
+ _get_json_type (type ) # metaclass
167
+
168
+
169
+ def test_function_docstring_parsing ():
170
+ from typing import List , Dict , Any
171
+
172
+ def func_with_complex_docs (x : int , y : List [str ]) -> Dict [str , Any ]:
173
+ """
174
+ Test function with complex docstring.
175
+
176
+ Args:
177
+ x (integer): A number
178
+ with multiple lines
179
+ y (array of string): A list
180
+ with multiple lines
181
+
182
+ Returns:
183
+ object: A dictionary
184
+ with multiple lines
185
+ """
186
+ pass
187
+
188
+ tool = convert_function_to_tool (func_with_complex_docs )
189
+ assert tool ['function' ]['description' ] == 'Test function with complex docstring.'
190
+ assert tool ['function' ]['parameters' ]['properties' ]['x' ]['description' ] == 'A number with multiple lines'
191
+ assert tool ['function' ]['parameters' ]['properties' ]['y' ]['description' ] == 'A list with multiple lines'
192
+
193
+
194
+ def test_tool_validation ():
195
+ # Test that malformed tool dictionaries are rejected
196
+ invalid_tool = {'type' : 'invalid_type' , 'function' : {'name' : 'test' }}
197
+ with pytest .raises (ValueError ):
198
+ process_tools ([invalid_tool ])
199
+
200
+ # Test missing required fields
201
+ incomplete_tool = {
202
+ 'type' : 'function' ,
203
+ 'function' : {'name' : 'test' }, # missing description and parameters
204
+ }
205
+ with pytest .raises (ValueError ):
206
+ process_tools ([incomplete_tool ])
0 commit comments