An agent framework lets a language model call ordinary Python functions. Each function must be described to the model as a JSON-style tool definition, and writing those by hand drifts out of date. Generate the definition from the function itself.
Write tool_schema(func) that returns a dictionary:
{"name": ..., "description": ..., "parameters": {"type": "object", "properties": {...}, "required": [...]}}
name: the function's __name__.description: the docstring's summary, meaning its first paragraph (lines up to the first blank line, after removing common indentation), joined with single spaces. Empty string if there is no docstring.properties: one entry per parameter, in signature order. Skip self, cls, *args and **kwargs. Each entry is a type schema (below), plus "description" if the docstring documents the parameter, plus "default" if the parameter has a default value.required: names of the parameters (in signature order) that have no default.Type schemas from annotations: int gives {"type": "integer"}, float gives "number", str gives "string", bool gives "boolean", dict or dict[...] gives "object". list gives {"type": "array"}, and list[X] adds "items" with the schema of X. Literal["a", "b"] gives {"type": "string", "enum": ["a", "b"]}. Optional[X] and X | None give the schema of X. A missing annotation, or any other type, gives {"type": "string"}.
Parameter descriptions come from a Google-style Args: section: after a line Args:, each line name: text or name (type): text documents name; more-indented continuation lines are appended with a single space. The section ends at a blank line or at the next line that ends with : at the section's own indentation (such as Returns:).