Skip to content

Commit 62692ac

Browse files
authored
Merge pull request #9 from comfuture/types
Update function-schema to version 0.4.2
2 parents 4c5a18a + 6e18006 commit 62692ac

File tree

5 files changed

+91
-17
lines changed

5 files changed

+91
-17
lines changed

function_schema/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""
22
A small utility to generate JSON schemas for python functions.
33
"""
4-
from .core import get_function_schema
4+
from .core import get_function_schema, guess_type, Doc, Annotated
55

6-
__version__ = "0.4.1"
6+
__version__ = "0.4.2"
77
__all__ = (
8-
"get_function_schema",
98
"__version__",
9+
"get_function_schema",
10+
"guess_type",
11+
"Doc",
12+
"Annotated",
1013
)

function_schema/core.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
import inspect
33
import platform
44
import packaging.version
5-
from typing import Annotated, Optional, Union, Callable, Literal, Any, get_args, get_origin
5+
from typing import (
6+
Annotated,
7+
Optional,
8+
Union,
9+
Callable,
10+
Literal,
11+
Any,
12+
get_args,
13+
get_origin,
14+
)
15+
from .types import FunctionSchema
616

717
current_version = packaging.version.parse(platform.python_version())
818
py_310 = packaging.version.parse("3.10")
@@ -18,25 +28,35 @@
1828
try:
1929
from typing_extensions import Doc
2030
except ImportError:
31+
2132
class Doc:
2233
def __init__(self, documentation: str, /):
2334
self.documentation = documentation
2435

36+
2537
__all__ = ("get_function_schema", "guess_type", "Doc", "Annotated")
2638

2739

28-
def is_doc_meta(obj: Annotated[Any, Doc("The object to be checked.")]) -> Annotated[bool, Doc("True if the object is a documentation object, False otherwise.")]:
40+
def is_doc_meta(
41+
obj: Annotated[Any, Doc("The object to be checked.")],
42+
) -> Annotated[
43+
bool, Doc("True if the object is a documentation object, False otherwise.")
44+
]:
2945
"""
3046
Check if the given object is a documentation object.
3147
3248
Example:
3349
>>> is_doc_meta(Doc("This is a documentation object"))
3450
True
3551
"""
36-
return getattr(obj, '__class__') == Doc and hasattr(obj, 'documentation')
52+
return getattr(obj, "__class__") == Doc and hasattr(obj, "documentation")
3753

3854

39-
def unwrap_doc(obj: Annotated[Union[Doc, str], Doc("The object to get the documentation string from.")]) -> Annotated[str, Doc("The documentation string.")]:
55+
def unwrap_doc(
56+
obj: Annotated[
57+
Union[Doc, str], Doc("The object to get the documentation string from.")
58+
],
59+
) -> Annotated[str, Doc("The documentation string.")]:
4060
"""
4161
Get the documentation string from the given object.
4262
@@ -57,7 +77,7 @@ def get_function_schema(
5777
Optional[Literal["openai", "claude"]],
5878
Doc("The format of the schema to return"),
5979
] = "openai",
60-
) -> Annotated[dict[str, Any], Doc("The JSON schema for the given function")]:
80+
) -> Annotated[FunctionSchema, Doc("The JSON schema for the given function")]:
6181
"""
6282
Returns a JSON schema for the given function.
6383
@@ -122,13 +142,13 @@ def get_function_schema(
122142
# find description in param_args tuple
123143
try:
124144
description = next(
125-
unwrap_doc(arg)
126-
for arg in param_args if isinstance(arg, Doc)
145+
unwrap_doc(arg) for arg in param_args if isinstance(arg, Doc)
127146
)
128147
except StopIteration:
129148
try:
130149
description = next(
131-
arg for arg in param_args if isinstance(arg, str))
150+
arg for arg in param_args if isinstance(arg, str)
151+
)
132152
except StopIteration:
133153
description = "The {name} parameter"
134154

@@ -158,8 +178,7 @@ def get_function_schema(
158178
}
159179

160180
if enum_ is not None:
161-
schema["properties"][name]["enum"] = [
162-
t for t in enum_ if t is not None]
181+
schema["properties"][name]["enum"] = [t for t in enum_ if t is not None]
163182

164183
if default_value is not inspect._empty:
165184
schema["properties"][name]["default"] = default_value
@@ -189,8 +208,7 @@ def get_function_schema(
189208
def guess_type(
190209
T: Annotated[type, Doc("The type to guess the JSON schema type for")],
191210
) -> Annotated[
192-
Union[str, list[str]], Doc(
193-
"str | list of str that representing JSON schema type")
211+
Union[str, list[str]], Doc("str | list of str that representing JSON schema type")
194212
]:
195213
"""Guesses the JSON schema type for the given python type."""
196214

function_schema/types.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import TypedDict, Literal
2+
3+
try:
4+
from typing import NotRequired
5+
except ImportError:
6+
try:
7+
from typing_extensions import NotRequired
8+
except ImportError:
9+
from typing import Optional as NotRequired
10+
11+
12+
class ParamSchema(TypedDict):
13+
"""
14+
Represents the schema for a parameter.
15+
Attributes:
16+
type (str): The type of the parameter.
17+
description (str): The description of the parameter.
18+
enum (Optional[list[str]]): The list of allowed values for the parameter (optional).
19+
default (Optional[str]): The default value for the parameter (optional).
20+
"""
21+
22+
type: str
23+
description: str
24+
enum: NotRequired[list[str]]
25+
default: NotRequired[str]
26+
27+
28+
class RootProperty(TypedDict):
29+
"""
30+
Represents the schema for a parameter.
31+
Attributes:
32+
type (str): Root property can only be "object".
33+
properties (dict[str, ParamSchema]): The properties of the object.
34+
"""
35+
36+
type: Literal["object"]
37+
properties: dict[str, ParamSchema]
38+
39+
40+
class FunctionSchema(TypedDict):
41+
"""
42+
Represents the schema of a function.
43+
Attributes:
44+
name (str): The name of the function.
45+
description (str): The description of the function.
46+
parameters (RootProperty): The schema for the function parameters.
47+
input_schema (ParamSchema): The schema for the function parameters if format is "claude".
48+
"""
49+
50+
name: str
51+
description: str
52+
parameters: NotRequired[RootProperty]
53+
input_schema: NotRequired[RootProperty]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "function-schema"
3-
version = "0.4.1"
3+
version = "0.4.2"
44
requires-python = ">= 3.9"
55
description = "A small utility to generate JSON schemas for python functions."
66
readme = "README.md"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)