Skip to main content

djangorestframework-pydantic-ai

CI PyPI Python versions Django versions Docs Coverage Ruff License

Expose djangorestframework-services services and selectors as a Pydantic-AI toolset, so a plain pydantic_ai.Agent can call them as tools — no MCP server and no AG-UI bridge in the path.

Every tool call routes through drf-services' transport-neutral surface (dispatch_spec plus its off-HTTP helpers), so a tool call runs the same input validation, the same permission_classes — both class-level has_permission and object-level has_object_permission on the resolved row — and the same serializer rendering your DRF views apply, just without the HTTP hop.

Install

pip install djangorestframework-pydantic-ai

It depends only on djangorestframework-services and pydantic-ai-slim. A model provider is pulled in separately, the usual Pydantic-AI way (e.g. pip install "pydantic-ai-slim[anthropic]").

Quickstart

from pydantic_ai import Agent
from rest_framework_pydantic_ai import AgentDeps, SpecToolset

toolset = SpecToolset(
    {
        "list_orders": orders_selector_spec,  # SelectorSpec -> read-only tool
        "create_order": create_order_spec,  # ServiceSpec  -> mutation tool
    }
)

agent = Agent("anthropic:claude-opus-4-8", deps_type=AgentDeps, toolsets=[toolset])

result = await agent.run(
    "create an order of 3 widgets",
    deps=AgentDeps(user=request.user),
)

The agent acts as deps.user: each call builds an off-HTTP request/view context, enforces the spec's permission_classes (class- and object-level), dispatches the spec, and renders the result through the spec's serializer. List selectors gain page / limit / order tool args, and return one page — {"items": [...], "page": 1, "totalPages": N, "hasNext": bool} — so a model reading a collection can tell whether it saw all of it.

Failures map onto the model loop so the agent self-corrects: invalid input, a bad page / limit / order value, and unexpected (hallucinated) arguments come back as ModelRetry; a business error or missing row becomes a readable ToolFailed, which the model sees as a failed call rather than a successful one carrying prose; a denied permission raises PermissionDenied, which aborts the run under a plain pydantic_ai.Agent. A host that installs a tool-failure policy — django-pydantic-agent's build_agent does by default — turns that denial into a failed-tool result instead, so the run continues; the call is still denied either way. Unexpected arguments are rejected by default — pass unknown_arguments= to SpecToolset (IGNORE / PASSTHROUGH) to change that.

See the documentation for the full reference.

License

MIT

Metadata

Release files for djangorestframework-pydantic-ai 0.25.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 djangorestframework-pydantic-ai 0.25.0
File Size Uploaded
djangorestframework_pydantic_ai-0.25.0.tar.gz 226.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djangorestframework-pydantic-ai 0.25.0
File Interpreter ABI Platform
djangorestframework_pydantic_ai-0.25.0-py3-none-any.whl Python 3 none any Details

Total release size: 270.9 kB

Release files / djangorestframework_pydantic_ai-0.25.0.tar.gz

Download URL djangorestframework_pydantic_ai-0.25.0.tar.gz
Size 226.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3d1f5c61e2b0e45a6f9fbcbb9b07d4bb679f30abcdb1c110638f35a99e02e039
BLAKE2b-256 checksum
How to use checksums
8adc5fee48f09d21c80691aaa655ed1c3e04c08b77eca96fb860ce25852fe29d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.

Transparency log

Release files / djangorestframework_pydantic_ai-0.25.0-py3-none-any.whl

Download URL djangorestframework_pydantic_ai-0.25.0-py3-none-any.whl
Size 44.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa44d9f01af032ad1f6536fede01ae17af11c4f96dd4820e296908047d5e17f3
BLAKE2b-256 checksum
How to use checksums
809b0da732d3ead32d3b5927c63b5e0bd9f40473c8c1cf8a49e5077ee5ab5307
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.

Transparency log

Release history Release notifications | RSS feed

0.35.1

2 release files

0.32.0

2 release files

0.31.0

2 release files

0.30.0

2 release files

0.29.0

2 release files

0.28.0

2 release files

This release

0.25.0 This release

2 release files

0.24.0

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.16.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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