simple-agents-py
Python bindings for SimpleAgents (PyO3-based).
Installation
Install from PyPI:
pip install simple-agents-py
Or with uv:
uv pip install simple-agents-py
Build from Source
uv build
Publish
uv publish
Set UV_PUBLISH_TOKEN or pass --token to publish to PyPI.
Usage
from simple_agents_py import Client
client = Client("openai")
response = client.complete("gpt-4", "Hello from Python!", max_tokens=128, temperature=0.7)
print(response.content)
Feature Guide
Canonical Environment Contract
Use the same cross-binding env contract used by Go/Node examples:
PROVIDER-openai,anthropic, oropenrouterCUSTOM_API_KEYCUSTOM_API_BASE(optional)CUSTOM_API_MODEL
Map these to provider-specific variables when needed:
- OpenAI:
OPENAI_API_KEY, optionalOPENAI_API_BASE - Anthropic:
ANTHROPIC_API_KEY - OpenRouter:
OPENROUTER_API_KEY, optionalOPENROUTER_API_BASE
Streaming
from simple_agents_py import Client
client = Client("openai")
messages = [{"role": "user", "content": "Say hello in one sentence."}]
for chunk in client.complete("gpt-4o-mini", messages, max_tokens=64, stream=True):
if chunk.content:
print(chunk.content, end="", flush=True)
print()
chunk.finish_reason uses Rust parity values (stop, length, content_filter, tool_calls).
Structured Output (JSON Mode)
from simple_agents_py import Client
import json
client = Client("openai")
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"},
},
"required": ["name", "age"],
}
messages = [{"role": "user", "content": "Extract name and age: Alice is 28."}]
json_text = client.complete(
"gpt-4o-mini",
messages,
schema=schema,
schema_name="person",
)
print(json.loads(json_text))
Pydantic models are accepted too:
from pydantic import BaseModel
from simple_agents_py import Client
class Person(BaseModel):
name: str
age: int
client = Client("openai")
messages = [{"role": "user", "content": "Extract name and age: Alice is 28."}]
json_text = client.complete(
"gpt-4o-mini",
messages,
schema=Person,
schema_name="person",
)
print(json_text)
Healing for Structured Outputs: Healing is enabled by default for structured outputs, automatically fixing malformed JSON, type mismatches, and missing fields. To disable healing:
client = Client("openai", healing=False)
Structured Streaming
from simple_agents_py import Client
client = Client("openai")
schema = {
"type": "object",
"properties": {"name": {"type": "string"}, "age": {"type": "number"}},
"required": ["name", "age"],
}
messages = [{"role": "user", "content": "Extract name and age: Alice is 28."}]
for event in client.complete(
"gpt-4o-mini",
messages,
schema=schema,
max_tokens=64,
stream=True,
):
if event.is_partial:
print("partial:", event.partial_value)
else:
print("complete:", event.value)
Response Healing
from simple_agents_py import Client
client = Client("openai")
messages = [{"role": "user", "content": "Return JSON: {\"name\":\"Sam\",\"age\":30}"}]
healed = client.complete(
"gpt-4o-mini",
messages,
max_tokens=64,
response_format="json",
heal=True,
)
print(healed.content)
print(healed.was_healed, healed.confidence)
print(healed.usage.get("total_tokens"))
Tool Calling
from simple_agents_py import Client
client = Client("openai")
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"unit": {"type": "string", "enum": ["c", "f"]},
},
"required": ["city"],
},
},
}
]
messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]
response = client.complete("gpt-4o-mini", messages, tools=tools)
print(response.tool_calls)
ClientBuilder (Providers + Healing)
from simple_agents_py import ClientBuilder, ProviderConfig
builder = (
ClientBuilder()
.add_provider_config(ProviderConfig("openai", api_key="sk-..."))
.add_provider("anthropic", api_key="sk-ant-...")
.with_healing_config(
{
"enabled": True,
"min_confidence": 0.7,
"fuzzy_match_threshold": 0.85,
}
)
)
client = builder.build()
print(client.complete("gpt-4o-mini", "Give me one idea.").content)
ClientBuilder currently supports:
add_provider(...)add_provider_config(...)with_healing_config({...})build()
Examples
OpenAI with a short prompt:
from simple_agents_py import Client
client = Client("openai")
response = client.complete("gpt-4o-mini", "Summarize this in one sentence.")
print(response.content)
Anthropic with custom tokens and temperature:
from simple_agents_py import Client
client = Client("anthropic")
response = client.complete(
"claude-3-5-sonnet-20240620",
"Write a friendly welcome message for a new user.",
max_tokens=120,
temperature=0.5,
)
print(response.content)
OpenRouter with a specific model:
from simple_agents_py import Client
client = Client("openrouter")
response = client.complete("openai/gpt-4o-mini", "Give me three project ideas.")
print(response.content)
OpenRouter with explicit API base and key:
from simple_agents_py import Client
client = Client(
"openrouter",
api_base="https://openrouter.ai/api/v1",
api_key="sk-your-openrouter-key",
)
response = client.complete("openai/gpt-4o-mini", "Give me three project ideas.")
print(response.content)
OpenAI with a custom gateway:
from simple_agents_py import Client
client = Client(
"openai",
api_base="http://localhost:4000/v1",
api_key="sk-your-openai-key",
)
response = client.complete("gpt-4o-mini", "Say hello from a local proxy.")
print(response.content)
Basic error handling:
from simple_agents_py import Client
try:
client = Client("openai")
print(client.complete("gpt-4o-mini", "Hello!").content)
except RuntimeError as exc:
print(f"Request failed: {exc}")
Notes
Clientreads provider configuration from environment variables (e.g.OPENAI_API_KEY) whenapi_keyis not provided.api_baselets you override the default API base URL.max_tokensandtemperatureare optional.
Tests
uv pip install -e .[dev]
uv run pytest
Layered suites:
- Unit:
uv run pytest tests/test_client_builder.py tests/test_client.py tests/test_direct_healing.py tests/test_healing.py tests/test_routing_config.py tests/test_streaming_parser.py - Contract:
uv run pytest tests/test_contract_fixtures.py tests/test_error_mapping_consistency.py - Live:
uv run pytest tests/test_integration_openai.py tests/test_streaming.py tests/test_structured_streaming.py(env-gated)
Package Metrics & Analytics
Download Statistics
View download statistics on:
- PyPI Stats: https://pypistats.org/packages/simple-agents-py
- PePy: https://pepy.tech/project/simple-agents-py
- PyPI Package Page: https://pypi.org/project/simple-agents-py/
The badges above automatically track:
- Version: Current version on PyPI
- Downloads: Monthly download count (from PePy)
- Python Versions: Supported Python versions
- License: Package license
Tracking with shields.io
Most badges use shields.io, while downloads use PePy to avoid upstream rate-limit responses from the PyPI monthly downloads endpoint. Available metrics:
pypi/v/simple-agents-py- Latest versionpypi/dw/simple-agents-py- Weekly downloadsstatic.pepy.tech/badge/simple-agents-py/month- Monthly downloadspypi/pyversions/simple-agents-py- Python version supportpypi/l/simple-agents-py- Licensepypi/status/simple-agents-py- Development statuspypi/format/simple-agents-py- Package format (wheel/sdist)
Optional Badges (for future)
Once you set up CI/CD and documentation, you can add:
<!-- GitHub Actions build status -->
[](https://github.com/yourusername/SimpleAgents/actions/workflows/python.yml)
<!-- Documentation -->
[](https://simpleagents.readthedocs.io/)
<!-- Code coverage -->
[](https://codecov.io/gh/yourusername/SimpleAgents)
<!-- GitHub stats -->
[](https://github.com/yourusername/SimpleAgents)
[](https://github.com/yourusername/SimpleAgents/fork)
Metadata
Release files for simple-agents-py 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_agents_py-0.5.2.tar.gz | 277.9 kB | Without provenance | Details for simple_agents_py-0.5.2.tar.gz |
Release files / simple_agents_py-0.5.2.tar.gz
| Download URL | simple_agents_py-0.5.2.tar.gz |
|---|---|
| Size | 277.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26c2f7883f93a1c92a3fa0fc4510cc2623143f76627aa2a106e5addc41e34d3a
|
|
BLAKE2b-256 checksum How to use checksums |
2534440eebe4c4f2e0be707564540075ddbd33ada818a470f5df0df1592a3b3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.7
|
Security for simple-agents-py 0.5.2
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.