API Reference¶
Generated in part from source docstrings via mkdocstrings.
Registration¶
expose_as_tool¶
Register a viewset's actions as tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
Sequence[str]
|
Which actions to expose. Standard actions
( |
('list', 'retrieve', 'create', 'update', 'partial_update', 'destroy')
|
name_prefix
|
str | None
|
Base name used to build each tool's name, as
|
None
|
descriptions
|
Mapping[str, str] | None
|
Optional per-action description overrides. Actions without an override get a generated default description. |
None
|
serializer_classes
|
Mapping[str, type[BaseSerializer[Any]]] | None
|
Optional per-action serializer class
overrides. Defaults to the viewset's |
None
|
examples
|
Mapping[str, Sequence[Mapping[str, Any]]] | None
|
Optional per-action example argument payloads, surfaced in the generated schema. |
None
|
lookup_field
|
str | None
|
URL kwarg name for detail actions. Defaults to the
viewset's |
None
|
registry
|
ToolRegistry | None
|
The :class: |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[_V], _V]
|
A class decorator that registers the tools as a side effect and |
Callable[[_V], _V]
|
returns the viewset class unchanged. |
Raises:
| Type | Description |
|---|---|
ToolRegistrationError
|
If a resulting
tool name collides with an already-registered tool, or if an
action name isn't a standard action and isn't found as an
|
Example
.. code-block:: python
@expose_as_tool(actions=["list", "retrieve"])
class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
Source code in src/drf_llm_gateway/registry.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
ToolRegistry¶
An ordered collection of :class:ToolDefinition objects.
Most projects only interact with the module-level
:data:default_registry (via :func:expose_as_tool), but a custom
registry is useful for isolating tool sets in tests or for exposing
different tool sets to different agent identities.
Source code in src/drf_llm_gateway/registry.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
clear()
¶
Remove every registered tool. Primarily useful in tests.
Source code in src/drf_llm_gateway/registry.py
211 212 213 | |
get(name)
¶
Look up a tool definition by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name. |
required |
Returns:
| Type | Description |
|---|---|
ToolDefinition | None
|
The matching :class: |
Source code in src/drf_llm_gateway/registry.py
191 192 193 194 195 196 197 198 199 200 | |
register(tool)
¶
Add a tool definition to the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolDefinition
|
The tool to register. |
required |
Raises:
| Type | Description |
|---|---|
ToolRegistrationError
|
If a tool with the same name is already registered. |
Source code in src/drf_llm_gateway/registry.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
unregister(name)
¶
Remove a tool definition by name, if present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name to remove. A no-op if not registered. |
required |
Source code in src/drf_llm_gateway/registry.py
183 184 185 186 187 188 189 | |
ToolDefinition¶
A single callable tool derived from one viewset action.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The unique tool name (see :func: |
description |
str
|
Human-readable description shown to the LLM. |
viewset_class |
type
|
The DRF viewset class this tool dispatches to. |
action |
str
|
The action name ( |
http_method |
str
|
The HTTP method used to dispatch this action. |
detail |
bool
|
Whether this action operates on a single object ( |
lookup_field |
str
|
The URL kwarg name identifying the target object for detail actions (ignored for non-detail actions). |
serializer_class |
type[BaseSerializer[Any]] | None
|
The serializer class describing this action's
request body, or |
permission_classes |
tuple[type[BasePermission], ...]
|
Permission classes enforced by
:func: |
authentication_classes |
tuple[type[BaseAuthentication], ...]
|
Authentication classes used to resolve the
calling identity in :func: |
examples |
tuple[Mapping[str, Any], ...]
|
Example argument payloads, surfaced in generated schemas
when :ref: |
version |
str
|
Developer-supplied semantic version string (defaults to
|
Source code in src/drf_llm_gateway/registry.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
schema_hash
property
¶
A content hash of :meth:input_json_schema, for drift detection.
input_json_schema()
¶
Build the JSON Schema for this tool's callable arguments.
Returns:
| Type | Description |
|---|---|
JSONSchema
|
An object schema combining the detail lookup parameter (for |
JSONSchema
|
detail actions) with the serializer's writable fields (for |
JSONSchema
|
actions with a request body). |
JSONSchema
|
fields as required, matching HTTP |
Source code in src/drf_llm_gateway/registry.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
default_registry¶
The module-level ToolRegistry instance expose_as_tool registers into
unless a different registry= is passed explicitly.
STANDARD_ACTIONS¶
Mapping of standard ModelViewSet action names to (http_method, detail)
tuples, used internally to resolve HTTP method and detail-ness for actions
that aren't custom @action-decorated methods.
Schema generation¶
serializer_to_json_schema¶
Convert a DRF serializer to a JSON Schema object schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serializer
|
type[BaseSerializer[Any]] | BaseSerializer[Any]
|
A serializer class or instance. Classes are instantiated internally (with no arguments) purely for field introspection; no validation or I/O occurs. |
required |
mode
|
str
|
|
'input'
|
Returns:
| Type | Description |
|---|---|
JSONSchema
|
A JSON-Schema-compatible object schema: a top-level type of |
JSONSchema
|
object, a mapping of field name to field schema, and (in |
JSONSchema
|
|
Raises:
| Type | Description |
|---|---|
SchemaGenerationError
|
If |
Example
A serializer with a required name field and an optional
email field produces a schema whose required list contains
only "name".
Source code in src/drf_llm_gateway/schema.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
Export formats¶
to_openai_tool¶
Convert a single :class:ToolDefinition to an OpenAI tool object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolDefinition
|
The tool definition to convert. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict shaped like the following, ready to pass directly in an |
dict[str, Any]
|
OpenAI API call's |
dict[str, Any]
|
```python |
dict[str, Any]
|
{"type": "function", "function": {"name": ..., "description": ..., "parameters": ...}} |
dict[str, Any]
|
``` |
Source code in src/drf_llm_gateway/openai_tools.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
to_openai_tools¶
Convert every tool in a registry to OpenAI tool objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry | None
|
The registry to export. Defaults to
:data: |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of OpenAI tool objects, in registration order. |
Source code in src/drf_llm_gateway/openai_tools.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
to_mcp_tool¶
Convert a single :class:ToolDefinition to an MCP tool object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolDefinition
|
The tool definition to convert. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A |
dict[str, Any]
|
"inputSchema": ...}``, matching the shape an MCP server returns |
dict[str, Any]
|
from |
dict[str, Any]
|
|
Source code in src/drf_llm_gateway/mcp.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
to_mcp_tools¶
Convert every tool in a registry to MCP tool objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry | None
|
The registry to export. Defaults to
:data: |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of MCP tool objects, in registration order — directly |
list[dict[str, Any]]
|
usable as the |
Source code in src/drf_llm_gateway/mcp.py
34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Execution¶
execute_tool¶
Execute a registered tool with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name, as produced by
:func: |
required |
arguments
|
dict[str, Any]
|
The tool call arguments. For detail actions, must
include the key named by the tool's |
required |
user
|
AbstractBaseUser | AnonymousUser | None
|
The identity to dispatch as. Defaults to
:class: |
None
|
registry
|
ToolRegistry | None
|
The registry to look |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ToolResult
|
class: |
Raises:
| Type | Description |
|---|---|
ToolNotFoundError
|
If |
ToolValidationError
|
If the underlying
view returned |
ToolPermissionDeniedError
|
If the
underlying view returned |
Source code in src/drf_llm_gateway/executor.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
ToolResult¶
The outcome of successfully executing a tool.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
int
|
The HTTP status code the underlying view produced. |
data |
Any
|
The response body as plain Python data (what
|
Source code in src/drf_llm_gateway/executor.py
35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Versioning¶
compute_schema_hash¶
Compute a stable, deterministic hash of a JSON Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON-Schema-compatible |
required |
Returns:
| Type | Description |
|---|---|
str
|
A 16-character hexadecimal digest prefix. Dict key order does not |
str
|
affect the result ( |
str
|
before hashing), so semantically identical schemas always hash |
str
|
identically. |
Example
compute_schema_hash({"type": "object", "properties": {}}) '...' # doctest: +SKIP
Source code in src/drf_llm_gateway/versioning.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Exceptions¶
LLMGatewayError¶
Bases: Exception
Base class for all errors raised by this package.
Source code in src/drf_llm_gateway/exceptions.py
18 19 | |
SchemaGenerationError¶
Bases: LLMGatewayError, ValueError
Raised when a serializer cannot be converted to JSON Schema.
The exception message describes what could not be converted and why
(e.g. an invalid mode, or nesting beyond MAX_SCHEMA_DEPTH).
Source code in src/drf_llm_gateway/exceptions.py
22 23 24 25 26 27 | |
ToolRegistrationError¶
Bases: LLMGatewayError, ValueError
Raised when :func:~drf_llm_gateway.registry.expose_as_tool or
:meth:~drf_llm_gateway.registry.ToolRegistry.register is used
incorrectly — e.g. duplicate tool names, or an unsupported action.
The exception message names the specific registration problem encountered.
Source code in src/drf_llm_gateway/exceptions.py
30 31 32 33 34 35 36 37 | |
ToolExecutionError¶
Bases: LLMGatewayError, APIException
Base class for errors raised while executing a registered tool.
Source code in src/drf_llm_gateway/exceptions.py
40 41 42 43 | |
ToolNotFoundError¶
Bases: ToolExecutionError
Raised when :func:~drf_llm_gateway.executor.execute_tool is asked
to run a tool name that isn't registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The unrecognized tool name. |
required |
Source code in src/drf_llm_gateway/exceptions.py
46 47 48 49 50 51 52 53 54 55 56 57 58 | |
ToolValidationError¶
Bases: ToolExecutionError, ValidationError
Raised when the arguments passed to a tool fail serializer validation.
The underlying :attr:detail mirrors the structure DRF's own
:class:~rest_framework.exceptions.ValidationError produces, so
field-level error messages are preserved.
Source code in src/drf_llm_gateway/exceptions.py
61 62 63 64 65 66 67 68 69 | |
ToolPermissionDeniedError¶
Bases: ToolExecutionError, PermissionDenied
Raised when the calling identity fails a tool's permission checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name that was denied. |
required |
Source code in src/drf_llm_gateway/exceptions.py
72 73 74 75 76 77 78 79 80 81 82 | |
Settings¶
get_setting¶
Return the effective value of a single package setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
One of the keys documented in :data: |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The user-configured value if present in the |
Any
|
setting, otherwise the default. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ImproperlyConfigured
|
If the |
Source code in src/drf_llm_gateway/settings.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
See Settings for the full list of recognized keys.
Management command¶
generate_llm_tools¶
python manage.py generate_llm_tools [--format {openai,mcp,jsonschema}] [--indent N] [--urlconf PATH]
See Quick Start.