Skip to content

comfuture/function-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Function schema

CI Release PyPI version

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.

Installation

pip install function-schema

Usage

See the examples directory for comprehensive usage examples with different AI platforms:

Quick Start

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)

Key Features

  • Type Annotations: Use typing.Annotated with Doc metadata for parameter descriptions
  • Multiple Formats: Support for OpenAI ("openai") and Claude ("claude") schema formats
  • Enum Support: Use enum.Enum or typing.Literal for parameter constraints
  • CLI Tool: Generate schemas from command line using function_schema

For detailed examples and advanced usage patterns, see the examples directory.

Platform Integration

OpenAI API

For detailed OpenAI integration examples including Assistant API and Chat Completion, see examples/openai_example.py.

Anthropic Claude

For Claude tool calling examples and multi-turn conversations, see examples/claude_example.py.

Model Context Protocol (MCP)

For MCP server and tool integration examples, see examples/mcp_example.py.

CLI Usage

Generate schemas from command line:

function_schema examples/cli_example.py get_weather | jq

For more CLI examples, see examples/cli_example.py.

License

MIT License

About

A small utility to generate JSON schemas for python functions.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages