Skip to main content

drf-llm-gateway

PyPI version Python versions License: MIT

Turn your existing Django REST Framework viewsets into tools an LLM can call — OpenAI function-calling format, MCP (Model Context Protocol) tool format, or plain JSON Schema — generated automatically from the serializers you already have.

from rest_framework import viewsets
from drf_llm_gateway import expose_as_tool


@expose_as_tool(actions=["list", "retrieve", "create"])
class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
from drf_llm_gateway import default_registry
from drf_llm_gateway.openai_tools import to_openai_tools

tools = to_openai_tools(default_registry)
# [{"type": "function", "function": {"name": "article_list", "description": "...",
#   "parameters": {"type": "object", "properties": {...}}}}, ...]

Why

Wiring an LLM agent up to call your existing API usually means hand-writing and hand-maintaining a parallel set of tool/function schemas — which drift out of sync with your serializers the moment either side changes. This package derives the schema from the serializer itself at generation time, so there is nothing to keep in sync: change a field on the serializer, regenerate the tools, done.

Features

  • Automatic JSON Schema generation from any DRF serializer — every standard field type, nested serializers, list serializers, related fields, choice fields, and more.
  • OpenAI function-calling format and MCP tool format exporters.
  • Tool registry — a decorator (@expose_as_tool) that registers a viewset's actions as callable tools without any schema duplication.
  • Runtime executor — validates arguments against the real serializer and dispatches to the real viewset action, respecting DRF permission and authentication classes.
  • Auth/permission metadata attached to every tool definition, so a gateway can decide what an agent identity may call before it calls it.
  • Schema versioning — every generated tool carries a content hash that changes whenever its schema changes, so consumers can detect drift.
  • CLI (generate_llm_tools management command) to export the full tool set as JSON for any external agent framework.
  • Fully typed, PEP 561 compatible, mypy --strict clean.

Installation

pip install drf-llm-gateway

Requires Python 3.10+, Django 4.2+, and Django REST Framework 3.14+.

Quick Start

See docs/quickstart.md for a complete walkthrough, including calling a tool end-to-end from a validated argument dict.

Documentation

Full documentation: https://mahmoudgshake.github.io/MahmoudPackages/drf-llm-gateway

Contributing

Contributions are welcome — see CONTRIBUTING.md.

License

MIT — see LICENSE.

Metadata

Release files for drf-llm-gateway 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-llm-gateway 1.0.0
File Size Uploaded
drf_llm_gateway-1.0.0.tar.gz 46.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-llm-gateway 1.0.0
File Interpreter ABI Platform
drf_llm_gateway-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 71.1 kB

Release files / drf_llm_gateway-1.0.0.tar.gz

Download URL drf_llm_gateway-1.0.0.tar.gz
Size 46.9 kB
Tags Source
SHA-256 checksum
How to use checksums
90c135b1f964aeda5daa332d17922ac03f9a9aa4e822d267f7a36745934ad0c6
BLAKE2b-256 checksum
How to use checksums
119500427afa3eae977c25459d3afdcfa5c8c1dca6f3da227ab3f8dc770d6b7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / drf_llm_gateway-1.0.0-py3-none-any.whl

Download URL drf_llm_gateway-1.0.0-py3-none-any.whl
Size 24.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
974466c23f319db34908e800352b232728f59fe675e4c01ccb1ccc007525a647
BLAKE2b-256 checksum
How to use checksums
3bc1b67dfbd01b856225a71ac33116beb6e5faa47077f4be51b4dab71183bb8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page