Extract parameter metadata from function signatures, handling optional defaults and unknown annotations.
The full prompt is available without an account.
Implement to_tool_schema(fn) that returns a dict with each parameter's type,
required status, and default value (if any). Handle these cases precisely:
- Known annotations: str, int, float, bool map to their JSON type names.
- Unrecognised annotations fall back to "string".
- Parameters with a default value are NOT required.
- The return dict must have the shape:
{"name": str, "description": str, "parameters": {"type": "object", "properties": {...}, "required": [...]}}
Read-only Python 3.12 preview. Sign in to edit and execute it.
import inspect
from typing import get_type_hints, Callable
TYPE_MAP = {str: "string", int: "integer", float: "number", bool: "boolean"}
def to_tool_schema(fn: Callable) -> dict:
# TODO: extract name, description, parameters (properties + required)
return {}