Schema
Schema utilities and local tool description building for ToolNode.
This module provides the SchemaMixin class which handles automatic schema generation for local Python functions, converting their type annotations and signatures into OpenAI-compatible function schemas. It supports various Python types including primitives, Optional types, List types, and Literal enums.
The schema generation process inspects function signatures and converts them to JSON Schema format suitable for use with language models and function calling APIs.
Classes:
Name | Description |
---|---|
SchemaMixin |
Mixin providing schema generation and local tool description building. |
Attributes¶
Classes¶
SchemaMixin
¶
Mixin providing schema generation and local tool description building.
This mixin provides functionality to automatically generate JSON Schema definitions from Python function signatures. It handles type annotation conversion, parameter analysis, and OpenAI-compatible function schema generation for local tools.
The mixin is designed to be used with ToolNode to automatically generate tool schemas without requiring manual schema definition for Python functions.
Attributes:
Name | Type | Description |
---|---|---|
_funcs |
dict[str, Callable]
|
Dictionary mapping function names to callable functions. This attribute is expected to be provided by the mixing class. |
Methods:
Name | Description |
---|---|
get_local_tool |
Generate OpenAI-compatible tool definitions for all registered local functions. |
Source code in pyagenity/graph/tool_node/schema.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
|
Functions¶
get_local_tool
¶
get_local_tool()
Generate OpenAI-compatible tool definitions for all registered local functions.
Inspects all registered functions in _funcs and automatically generates tool schemas by analyzing function signatures, type annotations, and docstrings. Excludes injectable parameters that are provided by the framework.
Returns:
Type | Description |
---|---|
list[dict]
|
List of tool definitions in OpenAI function calling format. Each |
list[dict]
|
definition includes the function name, description (from docstring), |
list[dict]
|
and complete parameter schema with types and required fields. |
Example
For a function:
def calculate(a: int, b: int, operation: str = "add") -> int:
'''Perform arithmetic calculation.'''
return a + b if operation == "add" else a - b
Returns:
[
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform arithmetic calculation.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "integer"},
"b": {"type": "integer"},
"operation": {"type": "string", "default": "add"},
},
"required": ["a", "b"],
},
},
}
]
Note
Parameters listed in INJECTABLE_PARAMS (like 'state', 'config', 'tool_call_id') are automatically excluded from the generated schema as they are provided by the framework during execution.
Source code in pyagenity/graph/tool_node/schema.py
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 |
|