agentcast-py
Structured output for any LLM call. Validate the response, retry with the validation error as feedback, raise CastError if all retries fail. BYO LLM, BYO validator. Zero runtime dependencies.
Python port of @mukundakatta/agentcast.
Install
pip install agentcast-py
# pydantic adapter is optional:
pip install "agentcast-py[pydantic]"
Usage
import asyncio
from anthropic import AsyncAnthropic
from pydantic import BaseModel
from agentcast import cast, adapters, CastError
client = AsyncAnthropic()
async def llm(messages):
resp = await client.messages.create(
model="claude-sonnet-4-6",
max_tokens=512,
messages=messages,
)
return resp.content[0].text
class Diagnosis(BaseModel):
code: str
severity: str
notes: str
async def main():
out = await cast(
llm=llm,
validate=adapters.pydantic(Diagnosis),
prompt="Triage: server is on fire. Output a Diagnosis JSON object.",
max_retries=3,
)
print(out) # validated dict
asyncio.run(main())
If the LLM returns prose, fenced JSON, or near-misses, cast() extracts the JSON, validates it, and retries with the validation error in the chat history -- so the next attempt has the spec. After max_retries failures, raises CastError carrying every attempt for postmortem.
Sync LLMs work too
def sync_llm(messages):
return openai_client.chat.completions.create(...).choices[0].message.content
out = await cast(llm=sync_llm, validate=schema, prompt="...", max_retries=2)
cast() is async (so you can await your async LLM), but the llm callable can be sync or async.
API
cast(*, llm, validate, prompt, system=None, max_retries=2, on_attempt=None) -> Any
Async. Returns the validated value (whatever the validator's value field is). Raises CastError on exhausted retries.
extract_json(text) -> Any | None
Sync. Pulls the largest valid JSON value out of arbitrary text -- whole text, fenced block, or balanced substring. Returns None if nothing parseable.
adapters.shape(spec)
Tiny built-in shape checker. Same as the sibling agentvet. {"field": "str|int|...|str?"}.
adapters.fn(predicate, error_builder?)
Predicate adapter.
adapters.pydantic(model_cls)
Wraps a pydantic v2 BaseModel. Requires pip install agentcast-py[pydantic].
CastError
Carries attempts: list[dict], last_error, last_text, last_parsed for postmortem.
API differences from the JS sibling
cast()isasync def(Python's natural pattern for LLM calls).llmmay be sync or async; both are auto-detected.adapters.pydanticreplacesadapters.zodas the natural Python adapter.- Keyword args throughout (
max_retries=,on_attempt=). - Validators return Python dicts:
{"valid": bool, "value": ... | "error": str}.
See the JS sibling's README for the full design notes.
Metadata
Release files for agentcast-py 0.1.0
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 | |
|---|---|---|---|---|
| agentcast_py-0.1.0.tar.gz | 9.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agentcast_py-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.2 kB
Release files / agentcast_py-0.1.0.tar.gz
| Download URL | agentcast_py-0.1.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b4512249e1216969a2b13708bd2031c6eaf713a2a8fc8b8ad4594f73b0a2482d
|
|
BLAKE2b-256 checksum How to use checksums |
ebe6b21cab337cb03bda3bf001801ac3b208b962113a69b507a525aec9afc827
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / agentcast_py-0.1.0-py3-none-any.whl
| Download URL | agentcast_py-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e69af18b97119a984bbad565fadf98110adb184c1f165b6978a8bb2a71f5d65
|
|
BLAKE2b-256 checksum How to use checksums |
7ae901731939565c83819ff2a27c74863a493fa722a3ad1a45937916e9e1f1c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for agentcast-py 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.