Skip to main content

Alloy (Python): Python for logic. English for intelligence.

Project description

Alloy (Python)

Python for logic. English for intelligence.

CI Docs Docs Site PyPI Downloads License: MIT

from alloy import command

@command(output=float)
def extract_price(text: str) -> str:
    return f"Extract the price from: {text}"

print(extract_price("This costs $49.99"))  # 49.99

Write typed AI functions that feel like normal Python. No framework, no abstractions — just functions that happen to use AI.

InstallTutorialExamplesDocs

Install

pip install alloy-ai                    # OpenAI only
pip install 'alloy-ai[anthropic]'       # With Anthropic
pip install 'alloy-ai[providers]'       # All providers

Quick start (OpenAI):

export OPENAI_API_KEY=sk-...
python -c "from alloy import ask; print(ask('Say hello'))"

Why Alloy?

🎯 Types you can trust: Provider‑enforced structured outputs. Get a real float, not a string to parse.

🐍 Just Python: Commands are functions. Tools are functions. Everything composes.

⚡ Production‑ready: Retries, contracts, streaming, cross‑provider support — batteries included.

🔍 Zero magic: See what’s happening. Control what’s happening. No hidden state.

Examples

Exploration with ask — Quick one‑offs and streaming

from alloy import ask

# One‑liner exploration
print(ask("List 3 reasons Alloy is useful."))

# Stream text output (text‑only streaming)
for chunk in ask.stream("Write a two‑sentence pitch for Alloy."):
    print(chunk, end="")

Typed outputs — Get back real Python objects

from dataclasses import dataclass
from alloy import command

@dataclass
class Analysis:
    sentiment: str
    score: float
    keywords: list[str]

@command(output=Analysis)
def analyze(text: str) -> str:
    return f"Analyze this text: {text}"

result = analyze("Alloy is amazing!")
print(result.score)  # 0.95

TypedDict outputs are supported too:

from typing import TypedDict
from alloy import command

class Product(TypedDict):
    name: str
    price: float

@command(output=Product)
def make() -> str:
    return "Return a Product with name='Test' and price=9.99 (numeric literal)."

print(make()["price"])  # 9.99

Tools + Contracts — Safe multi‑step workflows

from alloy import command, tool, ensure, require

@tool
@ensure(lambda x: x > 0, "Result must be positive")
def calculate(expression: str) -> float:
    return eval(expression)  # simplified example

@command(tools=[calculate])
def solve(problem: str) -> str:
    return f"Solve step by step: {problem}"

See more in examples/ and the Examples guide.

📦 More installation options
# Specific providers
pip install 'alloy-ai[anthropic]'
pip install 'alloy-ai[gemini]'
pip install 'alloy-ai[ollama]'

# Development
pip install -e '.[dev]'
🔧 Configuration
export ALLOY_MODEL=gpt-5-mini
export ALLOY_TEMPERATURE=0.2
export ALLOY_MAX_TOOL_TURNS=10

Or in Python:

from alloy import configure
configure(model="gpt-5-mini", temperature=0.2)
🧪 Run examples offline
export ALLOY_BACKEND=fake
make examples-quick

Providers

Works with major providers — same code, zero changes:

Provider Models (examples) Setup
OpenAI gpt‑5 export OPENAI_API_KEY=...
Anthropic claude‑4 export ANTHROPIC_API_KEY=...
Google gemini export GOOGLE_API_KEY=...
Local ollama ollama run <model> + ALLOY_MODEL=ollama:<model>

See the full provider guide.

Next Steps

New to Alloy? → 10‑minute tutorial

Ready to build? → Browse examples

Need details? → Read the docs

Contributing

We welcome contributions! See .github/CONTRIBUTING.md.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alloy_ai-0.3.0.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alloy_ai-0.3.0-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file alloy_ai-0.3.0.tar.gz.

File metadata

  • Download URL: alloy_ai-0.3.0.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for alloy_ai-0.3.0.tar.gz
Algorithm Hash digest
SHA256 09fa776ac2f343ec8c3a8d44d2c315f090a6f114eb344f0a8b1f40cc50f59d0e
MD5 16e85b1f8fde16219b4aebee4cafb095
BLAKE2b-256 8c143f884310040c8c06f79e27608a3c752d65316bea5ef2d03220e79a7e99d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for alloy_ai-0.3.0.tar.gz:

Publisher: release.yml on lydakis/alloy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file alloy_ai-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: alloy_ai-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 34.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for alloy_ai-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 836ced2f219075ae8aca63d661ca8650f9f08c2e42e80c869c476e0ba4d26150
MD5 c9d44567a2f7ea99f7fa57e4c9649335
BLAKE2b-256 26bff2444543016f26aec33ee626efad45158faecfc3eba883bb51f7e747cc9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for alloy_ai-0.3.0-py3-none-any.whl:

Publisher: release.yml on lydakis/alloy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page