Skip to main content

Generate function-calling tool schemas from DRF serializers. Compatible with OpenAI, Anthropic Claude, OpenRouter, Groq, Mistral, and any OpenAI-compatible LLM provider.

Project description

drf-tool-schema

Generate function-calling tool schemas from Django REST Framework serializers. Compatible with any LLM provider that follows the OpenAI tool calling standard — including OpenAI, Anthropic Claude, OpenRouter, Groq, Mistral, and LiteLLM.

Instead of writing tool schemas by hand and keeping them in sync with your serializers manually, drf-tool-schema introspects the serializer and builds the schema automatically. Change a field in your serializer and the tool schema updates with it.

Installation

pip install drf-tool-schema

Usage

from drf_tool_schema import schema_from_serializer
from partners.serializers import PartnerWriteSerializer

SCHEMAS = [
    schema_from_serializer(
        name="create_customer",
        description="Creates a new customer in the system. Returns the created customer ID.",
        serializer=PartnerWriteSerializer(),
        required_override=["legal_name", "tax_id"],
    ),
]

This produces:

{
  "type": "function",
  "function": {
    "name": "create_customer",
    "description": "Creates a new customer in the system. Returns the created customer ID.",
    "parameters": {
      "type": "object",
      "properties": {
        "legal_name": { "type": "string", "description": "Official registered company name." },
        "tax_id":     { "type": "string", "description": "NIF / CIF / VAT number." },
        "country":    { "type": "string", "description": "ISO 3166-1 alpha-2 country code.", "default": "ES" }
      },
      "required": ["legal_name", "tax_id"]
    }
  }
}

Pass the schema directly to your LLM client:

# OpenAI
client.chat.completions.create(model="gpt-4o", messages=[...], tools=SCHEMAS)

# Anthropic Claude
client.messages.create(model="claude-sonnet-4-20250514", messages=[...], tools=SCHEMAS)

# OpenRouter / Groq / Mistral / any OpenAI-compatible provider
client.chat.completions.create(model="...", messages=[...], tools=SCHEMAS)

Parameters

Parameter Type Description
name str Tool name in snake_case
description str Description sent to the LLM
serializer Serializer Instantiated DRF serializer
required_override list[str] | None Override required fields. If None, uses serializer's required flags
exclude list[str] | None Field names to exclude (read-only fields are always excluded)

Field type mapping

DRF field JSON Schema type
CharField, EmailField, URLField, DateField, DateTimeField, UUIDField string
IntegerField integer (with minimum / maximum if set)
DecimalField, FloatField number
BooleanField boolean
ChoiceField string + enum
ListField array
Nested Serializer object (inlined)

Fields with read_only=True are always excluded. Field descriptions come from help_text, falling back to label.

Development

pip install -e ".[dev]"
pytest
# or without pytest:
python tests/test_builder.py

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_tool_schema-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_tool_schema-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file drf_tool_schema-0.1.0.tar.gz.

File metadata

  • Download URL: drf_tool_schema-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for drf_tool_schema-0.1.0.tar.gz
Algorithm Hash digest
SHA256 48c4748986de5610c83c6c4e831848e89e1da6a98a969baadee941e0ca7b44c3
MD5 4dc99fd91a271b8702b959e55f2bc11e
BLAKE2b-256 2fbe790ac8e76034d315ea7b65bbf42f10b373373e78362bd75e61eee9f164b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_tool_schema-0.1.0.tar.gz:

Publisher: publish.yml on SebassContreras/drf-tool-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file drf_tool_schema-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_tool_schema-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f1a0f630c6330525857d30693638bd7e2eaa897483534fc139088dcc80ce9cb
MD5 ea068ee511100edba1f229e991cc7a8f
BLAKE2b-256 eba62f40195c10efe5a6e9207321df283c69a1418e42bb4ed74cde76c1116578

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_tool_schema-0.1.0-py3-none-any.whl:

Publisher: publish.yml on SebassContreras/drf-tool-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page