Package implementing adapter from DIAL Chat Completions API to Anthropic API
Project description
Python SDK for adapter from DIAL API to Anthropic API
Overview
The framework provides adapter from AI DIAL Chat Completion API to Anthropic Messages API.
Anthropic API passthrough
In addition to the DIAL-to-Anthropic adapter, the library exposes a transparent passthrough for the native Anthropic Messages API.
The exposed Anthropic Messages API is compatible with the vanilla Anthropic Client from Anthropic SDK:
from anthropic import Anthropic, AsyncAnthropic
client = Anthropic(api_key="...", base_url="${ADAPTER_ORIGIN}/anthropic")
The upstream errors are relayed to the caller in the native Anthropic error schema.
Usage
Mount the passthrough onto any Starlette/FastAPI host application (e.g. a DIALApp) with mount_anthropic_api. The upstream client is chosen per request by a factory you supply:
from aidial_sdk import DIALApp
from anthropic import AsyncAnthropic
from aidial_adapter_anthropic.passthrough import mount_anthropic_api
app = DIALApp(...)
async def get_client(request):
return AsyncAnthropic(api_key=...)
mount_anthropic_api(app, get_client)
The passthrough is mounted at /anthropic by default; pass path=... to change it. The get_client argument may also be a plain client instance instead of a factory.
Proxied endpoints
The following Anthropic endpoints are forwarded (relative to the mount path):
POST /v1/messages— create a message (streaming and non-streaming)POST /v1/messages/batches— create a message batchPOST /v1/messages/count_tokens— count tokens
Supported backends
The client factory may return any of the Anthropic SDK's async clients: AsyncAnthropic, AsyncAnthropicBedrock, AsyncAnthropicBedrockMantle, AsyncAnthropicVertex, and AsyncAnthropicFoundry.
The Bedrock backends require botocore, which is an optional dependency:
pip install aidial-adapter-anthropic[bedrock]
Endpoints a backend does not implement (e.g. Bedrock has no token-counting or batches route) surface as a 404 error.
Prompt caching
Automatic caching
Automatic caching is the simplest way to use prompt caching. A single top-level cache breakpoint instructs Anthropic to automatically apply a cache point to the last cacheable block of the request. This is ideal for multi-turn conversations where the growing message history should be cached automatically. See Automatic caching in the Anthropic docs.
To enable automatic caching, set custom_fields.cache_breakpoint at the top level of the Chat Completion request:
Top-level cache breakpoint
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Hello!"}
],
"custom_fields": {
"cache_breakpoint": {}
}
}
Explicit cache breakpoints
Explicit cache breakpoints give fine-grained control over which parts of the prompt get cached. You can place a cache breakpoint on individual system messages, user/assistant messages, or tool definitions. See Explicit cache breakpoints in the Anthropic docs.
To add a breakpoint, set custom_fields.cache_breakpoint on a message or tool object:
System cache breakpoint
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant with extensive knowledge.",
"custom_fields": {
"cache_breakpoint": {}
}
},
{"role": "user", "content": "Hello!"}
]
}
Message cache breakpoint
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "Here is a long document: ...",
"custom_fields": {
"cache_breakpoint": {}
}
},
{"role": "user", "content": "Summarize it."}
]
}
Tools cache breakpoint
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "What's the weather?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
},
"custom_fields": {
"cache_breakpoint": {}
}
}
]
}
TTL support
A cache breakpoint may include an optional ttl field. Supported values are 5m (5 minutes, default) and 1h (one hour). The ttl field is supported on both top-level and explicit breakpoints. See TTL support in the Anthropic docs.
Top-level cache breakpoint with TTL
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Hello!"}
],
"custom_fields": {
"cache_breakpoint": {
"ttl": "1h"
}
}
}
Web search
Web search gives Claude direct access to real-time web content, allowing it to answer questions with up-to-date information beyond its knowledge cutoff. It is an Anthropic server-side tool: the searches are executed on Anthropic's side, and the final response includes citations for the sources used. See Web search tool in the Anthropic docs.
To enable web search, set custom_fields.configuration.web_search to the Anthropic web search tool definition.
Enable web search
{
"model": "claude-opus-4-8",
"messages": [
{"role": "user", "content": "What is the weather in NYC?"}
],
"custom_fields": {
"configuration": {
"web_search": {
"type": "web_search_20250305",
"name": "web_search"
}
}
}
}
The tool definition supports optional fields such as max_uses, allowed_domains, blocked_domains, and user_location. See Tool definition in the Anthropic docs.
Web search with optional fields
{
"model": "claude-opus-4-8",
"messages": [
{"role": "user", "content": "What is the weather in San Francisco?"}
],
"custom_fields": {
"configuration": {
"web_search": {
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5,
"allowed_domains": ["example.com", "trusteddomain.org"],
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}
}
}
}
Development Environment
This project requires Python ≥3.11 and Poetry ≥2.1.1 for dependency management.
Setup
-
Install Poetry. See the official installation guide.
-
(Optional) Specify custom Python or Poetry executables in
.env.dev. This is useful if multiple versions are installed. By default,pythonandpoetryare used.POETRY_PYTHON=path-to-python-exe POETRY=path-to-poetry-exe
-
Create and activate the virtual environment:
make init_env source .venv/bin/activate
-
Install project dependencies (including linting, formatting, and test tools):
make install
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.13
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
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
[!IMPORTANT] This command doesn't work if you have already installed Git hooks locally or globally.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aidial_adapter_anthropic-0.16.0.dev1.tar.gz.
File metadata
- Download URL: aidial_adapter_anthropic-0.16.0.dev1.tar.gz
- Upload date:
- Size: 49.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.11.15 Linux/6.17.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c772b5ff23d39516df0be7280c8f032a5e6a1d90a12318ade0a4a97bf71fe40
|
|
| MD5 |
47d9570408cb9f274db2e3f7c69c1788
|
|
| BLAKE2b-256 |
eaf3ad996c47123feb15eccfdf12ff3c0c74a42ac3826b87fe734560e22253ee
|
File details
Details for the file aidial_adapter_anthropic-0.16.0.dev1-py3-none-any.whl.
File metadata
- Download URL: aidial_adapter_anthropic-0.16.0.dev1-py3-none-any.whl
- Upload date:
- Size: 65.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.11.15 Linux/6.17.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42698cd659db6bf8034171428d1190475ae3edfbb2bbe4bf591a81e68e55a2bf
|
|
| MD5 |
f0bb396e9396098d7d01143186ee6210
|
|
| BLAKE2b-256 |
3d00c0801fc937dec3f95d561744436cb553a85956cb5789290d5fda7fcd461b
|