This is a small utility to generate JSON schemas for python functions. With power of type annotations, it is possible to generate a schema for a function without describing it twice.
At this moment, extracting schema from a function is useful for OpenAI Assistant Tool Calling, OpenAI API function-call, and Anthropic Claude Tool calling feature. And it can be used for other purposes for example to generate documentation in the future.
pip install function-schemaSee the examples directory for comprehensive usage examples with different AI platforms:
- Basic Usage - Core features and function definition patterns
- OpenAI Integration - Assistant API and Chat Completion examples
- Claude Integration - Anthropic Claude tool calling examples
- MCP Integration - Model Context Protocol examples
- CLI Usage - Command-line interface examples
from typing import Annotated
from function_schema import Doc, get_function_schema
def get_weather(city: Annotated[str, Doc("The city to get the weather for")]) -> str:
"""Returns the weather for the given city."""
return f"Weather for {city} is 20°C"
# Generate schema
schema = get_function_schema(get_weather)- Type Annotations: Use
typing.AnnotatedwithDocmetadata for parameter descriptions - Multiple Formats: Support for OpenAI (
"openai") and Claude ("claude") schema formats - Enum Support: Use
enum.Enumortyping.Literalfor parameter constraints - CLI Tool: Generate schemas from command line using
function_schema
For detailed examples and advanced usage patterns, see the examples directory.
For detailed OpenAI integration examples including Assistant API and Chat Completion, see examples/openai_example.py.
For Claude tool calling examples and multi-turn conversations, see examples/claude_example.py.
For MCP server and tool integration examples, see examples/mcp_example.py.
Generate schemas from command line:
function_schema examples/cli_example.py get_weather | jqFor more CLI examples, see examples/cli_example.py.
MIT License