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.

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 {"error": ...}; 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

Download files

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

Source Distribution

djangorestframework_pydantic_ai-0.19.0.tar.gz (182.2 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file djangorestframework_pydantic_ai-0.19.0.tar.gz.

File metadata

File hashes

Hashes for djangorestframework_pydantic_ai-0.19.0.tar.gz
Algorithm Hash digest
SHA256 c28629a3ecbabc1d52752bbaeb15969903c4e57807d50bb89169b88d9e97ce07
MD5 86d744397aa7dc9feee7cc801c22e90b
BLAKE2b-256 867c0317f09300a28cc38fe552aeb2b27864e83e7f480c64a52648a5b8ea3716

See more details on using hashes here.

Provenance

The following attestation bundles were made for djangorestframework_pydantic_ai-0.19.0.tar.gz:

Publisher: release.yml on Artui/djangorestframework-pydantic-ai

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

File details

Details for the file djangorestframework_pydantic_ai-0.19.0-py3-none-any.whl.

File metadata

File hashes

Hashes for djangorestframework_pydantic_ai-0.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 920b88b135c86f611807894951fdfc87a136ab04bd582f16381d6891752a1f4d
MD5 d36def5877954dc1c9bbc6fe7985df5d
BLAKE2b-256 055ca92b3404042739373b536c892ae0429aff6ea64ea0e186e98a654e4aa358

See more details on using hashes here.

Provenance

The following attestation bundles were made for djangorestframework_pydantic_ai-0.19.0-py3-none-any.whl:

Publisher: release.yml on Artui/djangorestframework-pydantic-ai

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

Release history Release notifications | RSS feed

This release

0.19.0 This release

2 files

0.18.1

2 files

0.18.0

2 files

0.17.0

2 files

0.16.0

2 files

0.15.0

2 files

0.14.0

2 files

0.13.1

2 files

0.13.0

2 files

0.12.0

2 files

0.11.1

2 files

0.11.0

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

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