Skip to main content

pydantic-ai-cloudflare-ai

Cloudflare Workers AI and AI Gateway for Pydantic AI: chat, embeddings, text-to-speech and gateway options.

pip install pydantic-ai-cloudflare-ai
pip install "pydantic-ai-cloudflare-ai[all]"  # + anthropic, google, groq, mistral, typesafe, ws

Configured from CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_API_TOKEN, CLOUDFLARE_GATEWAY_ID and CLOUDFLARE_GATEWAY_TOKEN, or the same values passed explicitly.

Workers AI

Cloudflare-hosted @cf/... models, and other providers' models through your account's unified endpoints: one Cloudflare API token, Cloudflare billing, no provider keys.

from pydantic_ai import Agent, Embedder
from pydantic_ai_cloudflare_ai import WorkersAI

wai = WorkersAI(gateway_id="default")  # gateway_id is optional: logs requests through AI Gateway

agent = Agent(wai.model("@cf/meta/llama-3.3-70b-instruct-fp8-fast"))
agent = Agent(wai.model("openai/gpt-5-mini"))  # OpenAI Chat Completions
agent = Agent(wai.model("openai/gpt-5-mini", api="responses"))  # OpenAI Responses
agent = Agent(wai.model("anthropic/claude-haiku-4.5"))  # Anthropic Messages
agent = Agent(wai.model("google/gemini-2.5-flash"))
embedder = Embedder(wai.embedding_model("@cf/baai/bge-m3"))
result = await wai.run("@cf/openai/whisper", {...})  # any other model

Model ids are Cloudflare's catalog ids (google/..., anthropic/claude-haiku-4.5), not the providers' own.

AI Gateway

Provider-native routes, for your own provider keys (or keys stored in the gateway).

from pydantic_ai_cloudflare_ai import AIGateway

gw = AIGateway()

agent = Agent(gw.model("openai/gpt-5.6"))
agent = Agent(gw.model("anthropic/claude-sonnet-4-5", api_key="sk-ant-..."))
embedder = Embedder(gw.embedding_model("openai/text-embedding-3-small"))

Routes: openai, anthropic, google-ai-studio, groq, mistral. Any other route is a gateway custom provider, called as OpenAI-compatible chat: gw.model("xiaomi/mimo2.6-flash") (or "custom-xiaomi/..."). The provider key is api_key, else the gateway token (BYOK / unified billing), else the provider's own env var. Without a gateway id it uses Cloudflare's default gateway, created on first use with authentication on, so it needs CLOUDFLARE_GATEWAY_TOKEN.

Text-to-speech

Every Workers AI catalog voice (@cf/..., openai/..., google/..., elevenlabs/..., xai/..., ...), plus OpenAI and Gemini voices through the gateway. Results are Pydantic AI BinaryAudio.

audio = await wai.tts.run("openai/tts-1", "Hello", params={"voice": "nova"})
audio.data, audio.media_type  # b"...", "audio/mpeg"

async with wai.tts.stream("@cf/deepgram/aura-1", "Hello") as stream:
    stream.media_type  # known before the first chunk
    async for chunk in stream:
        ...

async with wai.tts.stream("xai/grok-tts", "Hello", transport="ws") as stream:  # `ws` extra
    audio = await stream.read()

audio = await gw.tts.run("google-ai-studio/gemini-2.5-flash-preview-tts", "Hello", params={"voice": "Kore"})

Gateway options

Caching, retries, timeouts, logging, metadata and custom costs, validated and sent as cf-aig-* headers.

from pydantic_ai_cloudflare_ai import GatewayOptions

gw = AIGateway(options=GatewayOptions(cache_ttl=3600, max_attempts=3, backoff="exponential"))

await agent.run("...", model_settings={"extra_headers": GatewayOptions(skip_cache=True).headers()})  # one request

Model strings

import pydantic_ai_cloudflare_ai

pydantic_ai_cloudflare_ai.install()

agent = Agent("cloudflare/workers-ai:@cf/meta/llama-3.3-70b-instruct-fp8-fast")
agent = Agent("cloudflare/openai:gpt-5.6")

Metadata

Release files for pydantic-ai-cloudflare-ai 0.1.1

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

Source distribution (sdist)

Source distribution for pydantic-ai-cloudflare-ai 0.1.1
File Size Uploaded Provenance
pydantic_ai_cloudflare_ai-0.1.1.tar.gz 15.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-cloudflare-ai 0.1.1
File Interpreter ABI Platform
pydantic_ai_cloudflare_ai-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.4 kB

Release files / pydantic_ai_cloudflare_ai-0.1.1.tar.gz

Download URL pydantic_ai_cloudflare_ai-0.1.1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
28ea97565cb3f3a8695ee935d09a399b8d42e92d3f476953aab1af3ff036983e
BLAKE2b-256 checksum
How to use checksums
2d38aa091d34fe7c225efb33065da08fcf111bd9302def6ecf9246b18bb8088c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pydantic_ai_cloudflare_ai-0.1.1-py3-none-any.whl

Download URL pydantic_ai_cloudflare_ai-0.1.1-py3-none-any.whl
Size 24.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9db91a4e6801a019576b8bd6b8f801a8560eecd82ba7a862f4035a8f00fea48
BLAKE2b-256 checksum
How to use checksums
51b0c62e01b7cd6b25ec9149df77b8f06172b81567d983783eedefa7057b785c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pydantic-ai-cloudflare-ai 0.1.1

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

0.1.1 This release

2 release files No provenance

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