1
+ from typing import Annotated
2
+
1
3
import pytest
2
4
3
5
from coagent .agents .util import chat
6
+ from coagent .agents .aswarm .util import function_to_jsonschema
7
+ from pydantic import Field
4
8
5
9
6
10
@pytest .mark .asyncio
@@ -25,3 +29,88 @@ async def test_chat_stream(mock_model_client):
25
29
# Only one chunk.
26
30
chunk = _chunk
27
31
assert chunk and chunk .content == "hello"
32
+
33
+
34
+ def test_function_to_jsonschema_normal ():
35
+ def func (a : int , b : str = "ok" ) -> None :
36
+ """This is a test function."""
37
+ pass
38
+
39
+ schema = function_to_jsonschema (func )
40
+ assert schema == {
41
+ "function" : {
42
+ "description" : "This is a test function." ,
43
+ "name" : "func" ,
44
+ "parameters" : {
45
+ "properties" : {
46
+ "a" : {"title" : "A" , "type" : "integer" },
47
+ "b" : {"default" : "ok" , "title" : "B" , "type" : "string" },
48
+ },
49
+ "required" : ["a" ],
50
+ "title" : "func" ,
51
+ "type" : "object" ,
52
+ },
53
+ },
54
+ "type" : "function" ,
55
+ }
56
+
57
+
58
+ def test_function_to_jsonschema_annotated ():
59
+ def func (a : Annotated [int , "Param a" ], b : Annotated [str , "Param b" ] = "ok" ) -> None :
60
+ """This is a test function."""
61
+ pass
62
+
63
+ schema = function_to_jsonschema (func )
64
+ assert schema == {
65
+ "function" : {
66
+ "description" : "This is a test function." ,
67
+ "name" : "func" ,
68
+ "parameters" : {
69
+ "properties" : {
70
+ "a" : {"description" : "Param a" , "title" : "A" , "type" : "integer" },
71
+ "b" : {
72
+ "default" : "ok" ,
73
+ "description" : "Param b" ,
74
+ "title" : "B" ,
75
+ "type" : "string" ,
76
+ },
77
+ },
78
+ "required" : ["a" ],
79
+ "title" : "func" ,
80
+ "type" : "object" ,
81
+ },
82
+ },
83
+ "type" : "function" ,
84
+ }
85
+
86
+
87
+ def test_function_to_jsonschema_pydantic_field ():
88
+ def func (
89
+ a : int = Field (description = "Param a" ),
90
+ b : str = Field (default = "ok" , description = "Param b" ),
91
+ ) -> None :
92
+ """This is a test function."""
93
+ pass
94
+
95
+ schema = function_to_jsonschema (func )
96
+ assert schema == {
97
+ "function" : {
98
+ "description" : "This is a test function." ,
99
+ "name" : "func" ,
100
+ "parameters" : {
101
+ "properties" : {
102
+ "a" : {"description" : "Param a" , "title" : "A" , "type" : "integer" },
103
+ "b" : {
104
+ "default" : "ok" ,
105
+ "description" : "Param b" ,
106
+ "title" : "B" ,
107
+ "type" : "string" ,
108
+ },
109
+ },
110
+ "required" : ["a" ],
111
+ "title" : "func" ,
112
+ "type" : "object" ,
113
+ },
114
+ },
115
+ "type" : "function" ,
116
+ }
0 commit comments