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. 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.0

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.0
File Size Uploaded Provenance
pydantic_ai_cloudflare_ai-0.1.0.tar.gz 15.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 39.9 kB

Release files / pydantic_ai_cloudflare_ai-0.1.0.tar.gz

Download URL pydantic_ai_cloudflare_ai-0.1.0.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b13cc84c1343739ec76e64b0ec028e3797926232b3202496f6e05c227d395d3c
BLAKE2b-256 checksum
How to use checksums
ad1d699efb69a6073517a1383edd808133bb5642e4bc86bbc13af3b4fbe60c7f
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.0-py3-none-any.whl

Download URL pydantic_ai_cloudflare_ai-0.1.0-py3-none-any.whl
Size 24.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85e363e873de7353c03439976ca8245f14e9a76c1ccefff5c00da4e41147b5aa
BLAKE2b-256 checksum
How to use checksums
0a24da80b4daff196705ef67d3b38537c3b1ef0e6deff8e678ef345a6f480e88
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.0

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.0 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