Tool Schema Builder
Prerequisites & Dependencies
- Python 3.10+ with
inspect / Node 18+ for runtime introspection (JSDoc, TypeScript reflect-metadata)
- Optional converters:
pip install pydantic docstring-parser or npm i zod zod-to-json-schema
- No API keys required; optionally a model endpoint for a smoke-test call
Execution Steps
- Extract the function name, signature (parameter names, types, defaults), and docstring summary/description.
- Map language types to JSON Schema:
str→string, int/float→number|integer, bool→boolean, list→array, dict/TypedDict→object, Optional[X] excluded from required.
- Encode constraints: enums, min/max values, patterns; add a one-line action-oriented description with units and expected format for each parameter.
- Wrap in the provider's tool-calling envelope:
"type": "function", "function": { "name": "...", "description": "...", "parameters": { ... } } with $schema: https://json-schema.org/draft/07/schema and additionalProperties: false.
- Validate the generated schema against JSON Schema meta-schema; round-trip a sample payload to verify completeness.
- Wire the dispatcher: parse model tool call, validate arguments against the schema, and invoke the underlying function with typed validation errors on mismatch.
from pydantic import BaseModel, Field, TypeAdapter
def get_weather(city: str, units: str = "celsius") -> dict:
"""Return the current weather for a city."""
class GetWeather(BaseModel):
"""Return the current weather for a city."""
city: str = Field(description="City name, e.g. 'Berlin'")
units: str = Field(default="celsius", pattern="^(celsius|fahrenheit)$")
tool = {
"type": "function",
"function": {
"name": "get_weather",
"description": GetWeather.__doc__,
"parameters": TypeAdapter(GetWeather).json_schema(),
},
}
1---2name: tool-schema-builder3description: Convert standard code functions into JSON schema format for AI function calling.4---56# Tool Schema Builder78## Prerequisites & Dependencies9- Python 3.10+ with `inspect` / Node 18+ for runtime introspection (JSDoc, TypeScript `reflect-metadata`)10- Optional converters: `pip install pydantic docstring-parser` or `npm i zod zod-to-json-schema`11- No API keys required; optionally a model endpoint for a smoke-test call1213## Execution Steps141. Extract the function name, signature (parameter names, types, defaults), and docstring summary/description.152. Map language types to JSON Schema: `str→string`, `int/float→number|integer`, `bool→boolean`, `list→array`, `dict/TypedDict→object`, `Optional[X]` excluded from `required`.163. Encode constraints: enums, min/max values, patterns; add a one-line action-oriented description with units and expected format for each parameter.174. Wrap in the provider's tool-calling envelope: `"type": "function"`, `"function": { "name": "...", "description": "...", "parameters": { ... } }` with `$schema: https://json-schema.org/draft/07/schema` and `additionalProperties: false`.185. Validate the generated schema against JSON Schema meta-schema; round-trip a sample payload to verify completeness.196. Wire the dispatcher: parse model tool call, validate arguments against the schema, and invoke the underlying function with typed validation errors on mismatch.2021```python22from pydantic import BaseModel, Field, TypeAdapter2324def get_weather(city: str, units: str = "celsius") -> dict:25 """Return the current weather for a city."""2627class GetWeather(BaseModel):28 """Return the current weather for a city."""29 city: str = Field(description="City name, e.g. 'Berlin'")30 units: str = Field(default="celsius", pattern="^(celsius|fahrenheit)$")3132tool = {33 "type": "function",34 "function": {35 "name": "get_weather",36 "description": GetWeather.__doc__,37 "parameters": TypeAdapter(GetWeather).json_schema(),38 },39}40```