functionschema
Class info¶
Re-exported classes¶
| Class | Module | Description |
|---|---|---|
| FunctionSchema | schemez.functionschema.functionschema | Schema representing an OpenAI function definition and metadata. |
| OpenAIFunctionDefinition | schemez.functionschema.typedefs | Schema for the function definition part of an OpenAI tool. |
| OpenAIFunctionTool | schemez.functionschema.typedefs | Complete OpenAI tool definition for function calling. |
| SimpleProperty | schemez.functionschema.typedefs | Schema property for primitive types. |
| ToolParameters | schemez.functionschema.typedefs |
🛈 DocStrings¶
FunctionSchema.
ToolParameters
module-attribute
¶
ToolParameters = TypedDict(
"ToolParameters",
{
"type": Literal["object"],
"properties": dict[str, Any],
"required": NotRequired[list[str]],
"$defs": NotRequired[dict[str, Any]],
},
)
Schema for tool parameters (JSON Schema object type).
FunctionSchema
¶
Bases: BaseModel
Schema representing an OpenAI function definition and metadata.
This class encapsulates all the necessary information to describe a function to the OpenAI API, including its name, description, parameters, return type, and execution characteristics. It follows the OpenAI function calling format while adding additional metadata useful for Python function handling.
Source code in src/schemez/functionschema/functionschema.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
description
class-attribute
instance-attribute
¶
Optional description of what the function does.
name
instance-attribute
¶
The name of the function as it will be presented to the OpenAI API.
parameters
class-attribute
instance-attribute
¶
parameters: ToolParameters = pydantic.Field(
default_factory=lambda: ToolParameters(type="object", properties={}, required=[])
)
JSON Schema object describing the function's parameters.
required
class-attribute
instance-attribute
¶
List of parameter names that are required (do not have default values).
returns
class-attribute
instance-attribute
¶
JSON Schema object describing the function's return type.
create_parameter_model
¶
Create a Pydantic model from the schema parameters.
Source code in src/schemez/functionschema/functionschema.py
from_dict
classmethod
¶
from_dict(schema: Mapping[str, Any], output_schema: dict[str, Any] | None = None) -> FunctionSchema
Create a FunctionSchema from a raw schema dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Mapping[str, Any]
|
OpenAI function schema dictionary (also an |
required |
output_schema
|
dict[str, Any] | None
|
Optional dictionary specifying the return type. If not provided, an object type will be set. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema format is invalid or missing required fields |
Source code in src/schemez/functionschema/functionschema.py
get_annotations
¶
Get a dictionary of parameter names to their Python types.
This can be used directly for annotations assignment.
Returns:
| Type | Description |
|---|---|
dict[str, type[Any]]
|
Dictionary mapping parameter names to their Python types. |
Source code in src/schemez/functionschema/functionschema.py
model_dump_openai
¶
model_dump_openai() -> OpenAIFunctionTool
Convert the schema to OpenAI's function calling format.
Returns:
| Type | Description |
|---|---|
OpenAIFunctionTool
|
A dictionary matching OpenAI's complete function tool definition format. |
OpenAIFunctionTool
|
{"type": "function", "function": {"name": ..., "description": ..., "parameters": ...}} |
Source code in src/schemez/functionschema/functionschema.py
to_parameter_model_code
¶
Generate Pydantic model code for parameters using datamodel-codegen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str | None
|
Name for the generated class (default: {name}Params) |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Generated Python code string |
Source code in src/schemez/functionschema/functionschema.py
to_python_signature
¶
Convert the schema back to a Python function signature.
This method creates a Python function signature from the OpenAI schema, mapping JSON schema types back to their Python equivalents.
Source code in src/schemez/functionschema/functionschema.py
to_return_model_code
¶
Generate Pydantic model code for return type using datamodel-codegen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str | None
|
Name for the generated class (default: {name}Response) |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Generated Python code string |
Source code in src/schemez/functionschema/functionschema.py
OpenAIFunctionDefinition
¶
Bases: TypedDict
Schema for the function definition part of an OpenAI tool.
This represents the inner "function" object that contains the actual function metadata and parameters.
Source code in src/schemez/functionschema/typedefs.py
OpenAIFunctionTool
¶
Bases: TypedDict
Complete OpenAI tool definition for function calling.
This represents the top-level tool object that wraps a function definition and identifies it as a function tool type.
Source code in src/schemez/functionschema/typedefs.py
SimpleProperty
¶
create_constructor_schema
¶
create_constructor_schema(cls: type) -> FunctionSchema
Create OpenAI function schema from class constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
Class to create schema for |
required |
Returns:
| Type | Description |
|---|---|
FunctionSchema
|
OpenAI function schema for class constructor |
Source code in src/schemez/functionschema/schema_generators.py
create_schema
¶
create_schema(
func: Callable[..., Any],
name_override: str | None = None,
description_override: str | None = None,
exclude_types: list[type] | None = None,
mode: SchemaType = "simple",
) -> FunctionSchema
Create an OpenAI function schema from a Python function.
If an iterator is passed, the schema return type is a list of the iterator's element type. Variable arguments (*args) and keyword arguments (**kwargs) are not supported in OpenAI function schemas and will be ignored with a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Function to create schema for |
required |
name_override
|
str | None
|
Optional name override (otherwise the function name) |
None
|
description_override
|
str | None
|
Optional description override (otherwise the function docstring) |
None
|
exclude_types
|
list[type] | None
|
Types to exclude from parameters (e.g., context types) |
None
|
mode
|
SchemaType
|
Schema generation mode - simple" (default) uses custom simple implementation, - "openai" for OpenAI function calling via Pydantic, - "jsonschema" for standard JSON schema via Pydantic |
'simple'
|
Source code in src/schemez/functionschema/functionschema.py
create_schemas_from_callables
¶
create_schemas_from_callables(
callables: dict[str, Callable[..., Any]],
prefix: str | Literal[False] | None = None,
exclude_private: bool = True,
) -> dict[str, FunctionSchema]
Generate OpenAI function schemas from a dictionary of callables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callables
|
dict[str, Callable[..., Any]]
|
Dictionary mapping names to callable objects |
required |
prefix
|
str | Literal[False] | None
|
Schema name prefix to prepend to function names. If None, no prefix. If False, use raw name. If string, uses that prefix. |
None
|
exclude_private
|
bool
|
Whether to exclude callables starting with underscore |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, FunctionSchema]
|
Dictionary mapping qualified names to FunctionSchema objects |
Example
def foo(x: int) -> str: ... def bar(y: float) -> int: ... callables = {'foo': foo, 'bar': bar} schemas = create_schemas_from_callables(callables, prefix='math') print(schemas['math.foo'])
Source code in src/schemez/functionschema/schema_generators.py
create_schemas_from_class
¶
create_schemas_from_class(
cls: type, prefix: str | Literal[False] | None = None
) -> dict[str, FunctionSchema]
Generate OpenAI function schemas for all public methods in a class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to generate schemas from |
required |
prefix
|
str | Literal[False] | None
|
Schema name prefix. If None, uses class name. If False, no prefix. If string, uses that prefix. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, FunctionSchema]
|
Dictionary mapping qualified method names to FunctionSchema objects |
Example
class MyClass: ... def my_method(self, x: int) -> str: ... return str(x) schemas = create_schemas_from_class(MyClass) print(schemas['MyClass.my_method'])
Source code in src/schemez/functionschema/schema_generators.py
create_schemas_from_module
¶
create_schemas_from_module(
module: ModuleType | str,
include_functions: list[str] | None = None,
prefix: str | Literal[False] | None = None,
) -> dict[str, FunctionSchema]
Generate OpenAI function schemas from a Python module's functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType | str
|
Either a ModuleType object or string name of module to analyze |
required |
include_functions
|
list[str] | None
|
Optional list of function names to specifically include |
None
|
prefix
|
str | Literal[False] | None
|
Schema name prefix. If None, uses module name. If False, no prefix. If string, uses that prefix. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, FunctionSchema]
|
Dictionary mapping function names to FunctionSchema objects |
Raises:
| Type | Description |
|---|---|
ImportError
|
If module string name cannot be imported |
Example
import math schemas = create_schemas_from_module(math, ['sin', 'cos']) print(schemas['math.sin'])
Source code in src/schemez/functionschema/schema_generators.py
determine_function_type
¶
Determine the type of the function.
Source code in src/schemez/functionschema/helpers.py
resolve_type_annotation
¶
resolve_type_annotation(
typ: Any,
description: str | None = None,
default: Any = empty,
is_parameter: bool = True,
defs: dict[str, Any] | None = None,
) -> Property
Resolve a type annotation into an OpenAI schema type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typ
|
Any
|
Type to resolve |
required |
description
|
str | None
|
Optional description |
None
|
default
|
Any
|
Default value if any |
empty
|
is_parameter
|
bool
|
Whether this is for a parameter (affects dict schema) |
True
|
defs
|
dict[str, Any] | None
|
Collects the definitions of recursive types, referenced as
|
None
|