Tool calling: support more types (#35776)

* Tool calling: support NoneType for function return type
This commit is contained in:
Aymeric Roucher 2025-01-20 19:15:34 +01:00 committed by GitHub
parent f19135afc7
commit 44393df089
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import types
from contextlib import contextmanager
from datetime import datetime
from functools import lru_cache
from types import NoneType
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, get_args, get_origin, get_type_hints
from packaging import version
@ -77,6 +78,7 @@ def _get_json_schema_type(param_type: str) -> Dict[str, str]:
float: {"type": "number"},
str: {"type": "string"},
bool: {"type": "boolean"},
NoneType: {"type": "null"},
Any: {},
}
if is_vision_available():

View File

@ -419,6 +419,31 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
self.assertEqual(schema["function"], expected_schema)
def test_return_none(self):
def fn(x: int) -> None:
"""
Test function
Args:
x: The first input
"""
pass
schema = get_json_schema(fn)
expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {
"x": {"type": "integer", "description": "The first input"},
},
"required": ["x"],
},
"return": {"type": "null"},
}
self.assertEqual(schema["function"], expected_schema)
def test_everything_all_at_once(self):
def fn(
x: str, y: Optional[List[Union[str, int]]], z: Tuple[Union[str, int], str] = (42, "hello")