Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

AI DIAL Python SDK

About DIALX

PyPI version Discord


Overview

Framework to create applications and model adapters for AI DIAL.

Applications and model adapters implemented using this framework will be compatible with AI DIAL API that was designed based on Azure OpenAI API.


Environment Variables

Variable Default Description
DIAL_SDK_HEADERS_TO_PROXY `` A comma-separated list of headers that should be proxied from incoming requests to outgoing requests to the DIAL API. By default, no headers are proxied.
DIAL_SDK_SSE_HEARTBEAT_INTERVAL When set, the SDK inserts ping comments into streaming chat completion responses after the response has been idle for the specified number of seconds, helping prevent read timeouts when the DIAL application isn't responsive.
PYDANTIC_V2 False When True and Pydantic V2 is installed, DIAL SDK classes for requests/responses will be based on Pydantic V2 BaseModel. Otherwise, they will be based on Pydantic V1 BaseModel.

Logging-related environment variables (log level, console format, and trace/span correlation) are documented in docs/logging.md.


Usage

Install the library using pip:

pip install aidial-sdk

Echo application example

The echo application example replies to the user by repeating their last message:

# Save this as app.py
import uvicorn

from aidial_sdk import DIALApp
from aidial_sdk.chat_completion import ChatCompletion, Request, Response


# ChatCompletion is an abstract class for applications and model adapters
class EchoApplication(ChatCompletion):
    async def chat_completion(self, request: Request, response: Response) -> None:
        # Get last message (the newest) from the history
        last_user_message = request.messages[-1]

        # Generate response with a single choice
        with response.create_single_choice() as choice:
            # Fill the content of the response with the last user's content
            choice.append_content(last_user_message.text())


# DIALApp extends FastAPI to provide a user-friendly interface for routing requests to your applications
app = DIALApp()
app.add_chat_completion("echo", EchoApplication())

# Run built app
if __name__ == "__main__":
    uvicorn.run(app, port=5000)

Run

python3 app.py

Check

Send the next request:

curl http://127.0.0.1:5000/openai/deployments/echo/chat/completions \
  -H "Content-Type: application/json" \
  -H "Api-Key: DIAL_API_KEY" \
  -d '{
    "messages": [{"role": "user", "content": "Repeat me!"}]
  }'

You will see the JSON response as:

{
    "choices":[
        {
            "index": 0,
            "finish_reason": "stop",
            "message": {
                "role": "assistant",
                "content": "Repeat me!"
            }
        }
    ],
    "usage": null,
    "id": "d08cfda2-d7c8-476f-8b95-424195fcdafe",
    "created": 1695298034,
    "object": "chat.completion"
}

Endpoints

Every deployment is exposed under two base paths:

Base path Description
/openai/deployments/{deployment_name} The DIAL API path. The deployment name is a part of the path.
/openai/v1 The Azure OpenAI v1 API path. The deployment name comes from the request headers.

The following endpoints are served under each of them:

  • POST <base path>/chat/completions
  • POST <base path>/rate
  • POST <base path>/tokenize
  • POST <base path>/truncate_prompt
  • GET <base path>/configuration
  • POST <base path>/embeddings

request.deployment_id is resolved from the first of the following sources that is set:

  1. the X-DIAL-OVERRIDE-NAME header,
  2. the deployment name from the path, for the /openai/deployments/{deployment_name} requests,
  3. the X-DIAL-DEPLOYMENT-ID header, which DIAL Core sets when it calls the /openai/v1 endpoints.

An /openai/v1 request with none of the headers set fails with 500 Internal Server Error.


Anthropic Messages to Chat Completions translator

The translator lets an Anthropic-native client (e.g. Claude Code) talk to a model that only speaks the Chat Completions API. It converts an Anthropic Messages request into a Chat Completions request to DIAL Core at DIAL_URL, and converts the response, streaming or not, back into the Anthropic Messages shape.

The translator requires the translators extra and Pydantic V2 models (PYDANTIC_V2=True):

pip install "aidial-sdk[translators]"

Mount it on your application:

import httpx

from aidial_sdk import DIALApp
from aidial_sdk.translators.messages_to_chat_completions import mount_translator

app = DIALApp()
mount_translator(app, httpx.AsyncClient())

The second argument is the HTTP client the translator calls DIAL Core with: either an httpx.AsyncClient or an async function that takes the incoming Request and returns one. The path (/openaiChatCompletions by default) and name keyword arguments configure the mount.

Endpoint Calls on DIAL Core
POST /openaiChatCompletions/anthropic/v1/messages POST {DIAL_URL}/openai/v1/chat/completions
POST /openaiChatCompletions/legacy/anthropic/v1/messages POST {DIAL_URL}/openai/deployments/{deployment}/chat/completions

DIAL_URL must be set, otherwise the endpoints answer 500.

To expose a Chat Completions model under /anthropic/v1/messages, add an anthropicMessages interface to its DIAL Core config, pointing base_url at the mount path of the application that runs the translator (or at /openaiChatCompletions/legacy to call DIAL Core's legacy per-deployment endpoint):

DIAL Core config example
{
  "models": {
    "gpt-5.5": {
      "type": "chat",
      "interfaces": {
        "openaiChatCompletions": {
          "base_url": "${OPENAI_ADAPTER_ORIGIN}"
        },
        "anthropicMessages": {
          "base_url": "${TRANSLATOR_ORIGIN}/openaiChatCompletions"
        }
      },
      "upstreams": [
        {
          "endpoint": "https://api.openai.com/v1/chat/completions",
          "key": "..."
        }
      ]
    }
  }
}

See the translation guide for how request parameters, content blocks, tools, caching, usage, and errors are translated, and for the translator's limitations.


Development

Development Environment

This project requires Python ≥3.11 and Poetry ≥2.1.1 for dependency management.

Setup

  1. Install Poetry. See the official installation guide.

  2. (Optional) Specify custom Python or Poetry executables in .env.dev. This is useful if multiple versions are installed. By default, python and poetry are used.

    POETRY_PYTHON=path-to-python-exe
    POETRY=path-to-poetry-exe
    
  3. Create and activate the virtual environment:

    make init_env
    source .venv/bin/activate
    
  4. Install project dependencies (including linting, formatting, and test tools):

    make install
    

IDE configuration

The recommended IDE is VSCode. Open the project in VSCode and install the recommended extensions. VS Code is configured to use the Ruff formatter.

Alternatively you can use PyCharm that has built-in Ruff support.

Lint

Run the linting before committing:

make lint

To auto-fix formatting issues run:

make format

Test

Run unit tests locally for available python versions:

make test

Run unit tests for the specific python version:

make test PYTHON=3.11

Git hooks

You may optionally install Git hooks that will automatically run the linting step on Git push. You only need to do it once for the given repository.

make install_git_hooks

Clean

To remove the virtual environment and build artifacts run:

make clean

Build

To build the package run:

make build

Publish

To publish the package to PyPI run:

make publish

Metadata

Release files for aidial-sdk 0.47.0.dev8

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

Source distribution (sdist)

Source distribution for aidial-sdk 0.47.0.dev8
File Size Uploaded Provenance Actions
aidial_sdk-0.47.0.dev8.tar.gz 65.9 kB Without provenance Details for aidial_sdk-0.47.0.dev8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aidial-sdk 0.47.0.dev8
File Interpreter ABI Platform Actions
aidial_sdk-0.47.0.dev8-py3-none-any.whl Python 3 none any Details for aidial_sdk-0.47.0.dev8-py3-none-any.whl

Total release size: 154.0 kB

Release files / aidial_sdk-0.47.0.dev8.tar.gz

Download URL aidial_sdk-0.47.0.dev8.tar.gz
Size 65.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1cae865281b3370ca834f20558ee8316e7e8846a8da766b0d61f36aa1601037e
BLAKE2b-256 checksum
How to use checksums
2c036011d34bc6f76b23da6da72d6fbf0d597850435d7b287c75a957f9c67daa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.17 Linux/7.0.0-1017-azure

Release files / aidial_sdk-0.47.0.dev8-py3-none-any.whl

Download URL aidial_sdk-0.47.0.dev8-py3-none-any.whl
Size 88.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d525aac04c394fc1a41384a774c63d0ca307ecf81c8e12d8e1c886f5e0ae8f5
BLAKE2b-256 checksum
How to use checksums
d5558be7acc6f711a2a34299f4e5fc50e793a3a81ff670dfdc539607003b3913
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.17 Linux/7.0.0-1017-azure

Security for aidial-sdk 0.47.0.dev8

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
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