@@ -20,13 +20,14 @@ pip install function-schema
2020
2121``` python
2222from typing import Annotated, Optional
23+ from function_schema import Doc
2324import enum
2425
2526def get_weather (
26- city : Annotated[str , " The city to get the weather for" ],
27+ city : Annotated[str , Doc( " The city to get the weather for" ) ],
2728 unit : Annotated[
2829 Optional[str ],
29- " The unit to return the temperature in" ,
30+ Doc( " The unit to return the temperature in" ) ,
3031 enum.Enum(" Unit" , " celcius fahrenheit" )
3132 ] = " celcius" ,
3233) -> str :
@@ -37,10 +38,7 @@ def get_weather(
3738Function description is taken from the docstring.
3839Type hinting with ` typing.Annotated ` for annotate additional information about the parameters and return type.
3940
40- - type can be ` typing.Union ` , ` typing.Optional ` . (` T | None ` for python 3.10+)
41- - string value of ` Annotated ` is used as a description
42- - enum value of ` Annotated ` is used as an enum schema
43-
41+ Then you can generate a schema for this function:
4442``` python
4543import json
4644from function_schema import get_function_schema
@@ -89,20 +87,55 @@ schema = get_function_schema(get_weather, "claude")
8987
9088Please refer to the [ Claude tool use] ( https://docs.anthropic.com/claude/docs/tool-use ) documentation for more information.
9189
92- ### Literal types can be used as Enum
90+ You can use any type hinting supported by python for the first argument of ` Annotated ` . including:
91+ ` typing.Literal ` , ` typing.Optional ` , ` typing.Union ` , and ` T | None ` for python 3.10+.
92+ ` Doc ` class or plain string in ` Annotated ` is used for describe the parameter.
93+ ` Doc ` metadata is the [ PEP propose] ( https://peps.python.org/pep-0727/ ) for standardizing the metadata in type hints.
94+ currently, implemented in ` typing-extensions ` module. Also ` function_schema.Doc ` is provided for compatibility.
95+
96+ Enumeratable candidates can be defined with ` enum.Enum ` in the argument of ` Annotated ` .
97+
98+ ``` python
99+ import enum
100+
101+ class AnimalType (enum .Enum ):
102+ dog = enum.auto()
103+ cat = enum.auto()
104+
105+ def get_animal (
106+ animal : Annotated[str , Doc(" The animal to get" ), AnimalType],
107+ ) -> str :
108+ """ Returns the animal."""
109+ return f " Animal is { animal.value} "
110+ ```
111+ In this example, each name of ` AnimalType ` enums(` dog ` , ` cat ` ) is used as an enum schema.
112+ In shorthand, you can use ` typing.Literal ` as the type will do the same thing.
113+
114+ ``` python
115+ def get_animal (
116+ animal : Annotated[Literal[" dog" , " cat" ], Doc(" The animal to get" )],
117+ ) -> str :
118+ """ Returns the animal."""
119+ return f " Animal is { animal} "
120+ ```
121+
122+
123+ ### Plain String in Annotated
124+
125+ The string value of ` Annotated ` is used as a description for convenience.
93126
94127``` python
95128def get_weather (
96- city : Annotated[str , " The city to get the weather for" ],
97- unit : Annotated[
98- Optional[Literal[" celcius" , " fahrenheit" ]], # <- Literal type represents Enum
99- " The unit to return the temperature in" ,
100- ] = " celcius" ,
129+ city : Annotated[str , " The city to get the weather for" ], # <- string value of Annotated is used as a description
130+ unit : Annotated[Optional[str ], " The unit to return the temperature in" ] = " celcius" ,
101131) -> str :
102132 """ Returns the weather for the given city."""
103133 return f " Weather for { city} is 20°C "
104134```
105- The schema will be generated as the same as the previous example.
135+
136+ But this would create a predefined meaning for any plain string inside of ` Annotated ` ,
137+ and any tool that was using plain strings in them for any other purpose, which is currently allowed, would now be invalid.
138+ Please refer to the [ PEP 0727, Plain String in Annotated] ( https://peps.python.org/pep-0727/#plain-string-in-annotated ) for more information.
106139
107140### Usage with OpenAI API
108141
@@ -163,7 +196,7 @@ response = client.beta.tools.messages.create(
163196### CLI usage
164197
165198``` sh
166- function_schema mymodule.py my_function
199+ function_schema mymodule.py my_function | jq
167200```
168201
169202## License
0 commit comments