Metadata-Version: 2.4
Name: django-ai-sdk
Version: 0.1.2
Summary: Build AI agents in Django. Batteries included.
Author: terminalkitten
License-Expression: BSD-3-Clause
Project-URL: Homepage, https://github.com/django-ai-sdk/django-ai-sdk
Project-URL: Repository, https://github.com/django-ai-sdk/django-ai-sdk
Project-URL: Issues, https://github.com/django-ai-sdk/django-ai-sdk/issues
Project-URL: Changelog, https://github.com/django-ai-sdk/django-ai-sdk/releases
Keywords: django,ai-agents,agentic-framework,multi-agent,llm-agents,workflow-automation,rag,llm,python
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: AsyncIO
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Framework :: Django :: 6.1
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aiofiles>=25.1.0
Requires-Dist: cashews<9,>=7.6.0
Requires-Dist: django<6.2,>=5.2.6
Requires-Dist: haystack-ai>=3.5.0
Requires-Dist: django-tasks>=0.12.0
Requires-Dist: httpx>=0.28.1
Requires-Dist: httpx2<3,>=2.12.0
Requires-Dist: openai>=3.26.0
Requires-Dist: puremagic>=2.3.0
Requires-Dist: pydantic>=2.14.4
Requires-Dist: tenacity>=9.2.1
Provides-Extra: all
Requires-Dist: django-ai-sdk[dbos,files,haystack,local,mcp,providers,rag,transformers]; extra == "all"
Provides-Extra: providers
Requires-Dist: django-ai-sdk[anthropic,huggingface,mistral,ollama,openrouter]; extra == "providers"
Provides-Extra: rag
Requires-Dist: django-ai-sdk[chroma,qdrant]; extra == "rag"
Provides-Extra: haystack
Requires-Dist: haystack-ai>=3.5.0; extra == "haystack"
Provides-Extra: mcp
Requires-Dist: mcp-haystack>=2.1.2; extra == "mcp"
Requires-Dist: mcp<3,>=2.3.0; extra == "mcp"
Requires-Dist: authlib>=1.9.10; extra == "mcp"
Provides-Extra: ninja
Requires-Dist: django-ninja>=1.8.0; extra == "ninja"
Provides-Extra: drf
Requires-Dist: djangorestframework>=3.18.3; extra == "drf"
Provides-Extra: files
Requires-Dist: firecrawl-anydoc>=0.2.5; extra == "files"
Requires-Dist: openpyxl>=3.1.5; extra == "files"
Requires-Dist: pdf-inspector>=1.27.0; extra == "files"
Requires-Dist: pdf-oxide==0.3.78; extra == "files"
Requires-Dist: pillow<14,>=10.3.0; extra == "files"
Requires-Dist: python-docx>=1.2.0; extra == "files"
Requires-Dist: python-pptx>=1.0.2; extra == "files"
Provides-Extra: embeddings
Requires-Dist: fastembed-haystack>=3.0.1; extra == "embeddings"
Requires-Dist: django-ai-sdk[haystack]; extra == "embeddings"
Provides-Extra: chroma
Requires-Dist: chroma-haystack>=5.1.1; extra == "chroma"
Requires-Dist: django-ai-sdk[embeddings]; extra == "chroma"
Provides-Extra: qdrant
Requires-Dist: qdrant-haystack>=13.2.0; extra == "qdrant"
Requires-Dist: django-ai-sdk[embeddings]; extra == "qdrant"
Provides-Extra: anthropic
Requires-Dist: anthropic-haystack>=8.1.0; extra == "anthropic"
Provides-Extra: huggingface
Requires-Dist: huggingface-api-haystack>=3.1.1; extra == "huggingface"
Provides-Extra: mistral
Requires-Dist: mistral-haystack>=3.0.0; extra == "mistral"
Provides-Extra: ollama
Requires-Dist: ollama-haystack>=7.0.2; extra == "ollama"
Provides-Extra: openrouter
Requires-Dist: openrouter-haystack>=2.1.0; extra == "openrouter"
Provides-Extra: transformers
Requires-Dist: transformers-haystack>=2.0.1; extra == "transformers"
Provides-Extra: typesafe
Requires-Dist: typesafe-sdk>=0.5.7; extra == "typesafe"
Provides-Extra: local
Requires-Dist: onnxruntime>=1.32.0; extra == "local"
Requires-Dist: tokenizers>=0.23.3; extra == "local"
Requires-Dist: huggingface-hub>=0.23; extra == "local"
Requires-Dist: numpy>=2.5.5; extra == "local"
Provides-Extra: dbos
Requires-Dist: dbos<4,>=3.5.0; extra == "dbos"
Dynamic: license-file

# Django AI SDK

<div align="center">
  <img src="https://raw.githubusercontent.com/django-ai-sdk/django-ai-sdk/refs/heads/dev/public/logo.png" alt="Django AI SDK" width="150">
  <br />
  <i>Build AI agents in Django. Batteries included.</i>
</div>

## Project status: read this first

This is a **beta**. Here's what that means for you:

- **Watch the repo.** Releases come often. Star and watch it to see them.
- **Your feedback shapes the SDK.** Break things, open issues and tell us what hurts.

## Install

```bash
pip install django-ai-sdk
```

Or with uv:

```bash
uv add django-ai-sdk
```

## Quick start

### 1. Add to INSTALLED_APPS

```python
# settings.py
INSTALLED_APPS = [
    ...
    "django_ai_sdk",
]
```

Then run `python manage.py migrate`.

### 2. Define your agent

```python
# agents.py
from django_ai_sdk import Agent
from django_ai_sdk.adapters.base import Stream
from django_ai_sdk.generators import openai_responses_chat
from haystack import Pipeline


class HelpDeskAgent(Agent):
    name = "Help Desk"
    model = "gpt-5-mini"
    instructions = "You are a helpful support agent."
    llm = openai_responses_chat

    async def get_pipeline_adapter(self, thread_id=None, user=None):
        storage_adapter = await self.get_storage_adapter(thread_id)
        generator = self.get_llm()
        return Stream(
            pipeline=Pipeline(),
            generator=generator,
            storage_adapter=storage_adapter,
        )
```

For non-streaming tasks (title generation, structured output), use `Run` instead:

```python
from django_ai_sdk.adapters.base import Run

    async def get_run_adapter(self, thread_id=None, user=None):
        return Run(generator=self.get_llm())
```

### 3. Return a streaming response

```python
# views.py
from .agents import HelpDeskAgent

agent = HelpDeskAgent()


@router.post("/chat")
async def chat(request, payload: ChatRequest):
    return await agent.as_view(
        payload.messages,
        thread_id=payload.thread_id,
    )
```

## Oversight

Block prompt injection, mask personal data and send risky turns to a person, from one
Python class. Add `"django_ai_sdk.oversight"` to `INSTALLED_APPS` and run `migrate`.

```python
from django_ai_sdk import Agent
from django_ai_sdk.errors import Blocked
from django_ai_sdk.oversight import L, Policy, Queue, Rule, register_policy


@register_policy
class Guard(Policy):
    name = "guard"
    extends = ["security.injection"]  # the pack's injection lens and its blocking rule
    rule_mode = "enforce"  # rules start in report mode; this makes them act
    rules = [
        Rule("injection_to_people", when=L("injection_question").is_true, then=Queue("security")),
    ]


class SupportAgent(Agent):
    oversight = Guard


try:
    answer = await SupportAgent().run(messages, user=request.user)
except Blocked as blocked:
    answer = blocked.message  # the policy's own text for the person asking
```

See the [Oversight guide](docs/content/oversight/oversight.md) for the rest, and its limitations.

## Features

- **Agents**: A Django `Agent` class you can run from a view, a task, or the API,
  streaming or not, with tool calling built in.
- **Workflows**: Multi-step, definition-driven runs with persisted steps and run listeners,
  so a process can span multiple agent calls with a record of what happened. Runs are
  durable on DBOS (`django-ai-sdk[dbos]`, one `ai_sdk_worker`): they survive restarts,
  wait days for an approval, and start from cron or webhook triggers.
- **Subagents**: Agents can delegate to other agents as tools, so one agent can
  orchestrate specialists instead of doing everything itself.
- **Tool calling**: MCP, memory and your own functions as tools, declared on the agent.
- **Streaming responses**: SSE streaming that speaks the Vercel AI SDK protocol.
- **Conversation storage**: Every message is stored in its thread, with no code of your own.
- **RAG pipelines**: BM25, ChromaDB and Qdrant hybrid search with query expansion,
  so an agent can answer from your documents.
- **Artifacts**: 16 structured UI types, such as tables, plans, approval cards and code
  blocks, that the model submits through tool calls.
- **File processing**: Uploads of text, CSV, JSON, DOCX, PPTX and XLSX run through a
  processing pipeline. Extraction transforms add metadata to the embeddings.
- **Integrations**: Third-party tools as self-registering Django apps, with caching,
  circuit breaking and OAuth built in.
- **Tracing**: Opt-in Haystack tracing persisted to the ORM, with per-thread and
  per-message token accounting.
- **Oversight**: A lens asks one typed question about every turn. Code, a classifier or
  a model judge answers it, as you pick per lens. Rules block, mask or queue the turn for
  a person, and start in report mode. Every turn of an agent with a policy is checked and
  recorded, masked by default.
- **Reindexing**: Rebuild a memory's index on demand, from code, an endpoint or the
  `reindex_memories` command, without a restart.

## Demo project

[Studio](https://github.com/django-ai-sdk/studio/) is the demo project. It holds a Django
app that uses the SDK's features: chat agents, swarms, deep research, memories, workflows
with a worker, oversight, MCP integrations and both the django-ninja and DRF APIs. It also
has native clients for macOS, iPhone and iPad.

## Documentation

The full documentation is at [django-ai-sdk.github.io/django-ai-sdk/docs](https://django-ai-sdk.github.io/django-ai-sdk/docs/).

## Is it any good?

[Yes.](http://news.ycombinator.com/item?id=3067434)

## License

[BSD-3-Clause](LICENSE)
