Skip to main content

llm-safecall

Validate model responses with Pydantic, retry failed requests, and keep a call report. Supports OpenAI, Anthropic, and custom providers on Python 3.10+.

Use it when several call sites need the same validation and error handling. If your application already handles those in a provider SDK or framework, you probably don't need another wrapper.

The text filters match known patterns. They can miss attacks and reject harmless text. Tool permissions, approvals, and process isolation belong in the application; this package does not enforce them.

Install

python3 -m venv .venv
. .venv/bin/activate
python -m pip install 'llm-safecall[openai]'

Use the anthropic extra for Anthropic, or install llm-safecall without extras to use your own provider. On Windows, activate with .venv\Scripts\Activate.ps1.

For changes in 0.2.3, see the changelog and migration notes.

Validate a response

Set OPENAI_API_KEY and LLM_MODEL to a model available to your account.

import os

from pydantic import BaseModel
from llm_safecall import OpenAIProvider, SafeCall


class Summary(BaseModel):
    title: str
    summary: str


safe = SafeCall(
    OpenAIProvider(model=os.environ["LLM_MODEL"]),
    output=Summary,
    timeout_s=15,
    retries=2,
)
result = safe.generate('Return a title and summary about unit tests.')
print(result.model_dump())
print(result._report.model_dump())

The schema is included in the prompt. Invalid JSON or a schema mismatch gets one repair call; a policy rejection raises immediately. Requests can incur charges, including retries and repairs.

Without output=, the result is a string with the same _report attribute. To check installation without credentials:

LLM_PROVIDER=mock llm-safecall --prompt "Hello"

Handle errors

Calls raise by default. Catch errors where your application can decide whether to retry later, show a message, or abandon the operation. Set retries=0 if another layer already retries requests.

For a display-only fallback, opt in explicitly:

safe = SafeCall(
    OpenAIProvider(model=os.environ["LLM_MODEL"]),
    output=Summary,
    fail_safe=True,
    fail_safe_return="Summary unavailable",
)
result = safe.generate("Summarize the release notes.")
if isinstance(result, str):
    print(result)
else:
    print(result.title)

A fallback is always a string, even with a schema. It is not a validated result.

Configure checks

Pass policy=load_policy("policy.yml") to load a policy file. Use redact=["email", "phone"] for input redaction. Settings from the process environment are available through from_env(); it does not load .env files.

Streaming checks individual chunks. A secret or other pattern split across chunks can pass through. Use generate() when the complete response must pass validation before anything is displayed or executed.

The behavior reference covers defaults, retries, streaming, tools, reports, budgets, caches, and the CLI. Read the migration notes before reusing an old cache or the pricing helper.

Development

python -m pip install -e '.[dev]'
ruff check .
ruff format --check .
pytest --cov=llm_safecall --cov-fail-under=85
python -m build
python -m twine check dist/*
python scripts/check_package.py dist/*.whl

Contributing · Security reports · MIT license

Metadata

Release files for llm-safecall 0.2.3

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

Source distribution (sdist)

Source distribution for llm-safecall 0.2.3
File Size Uploaded Provenance Actions
llm_safecall-0.2.3.tar.gz 33.4 kB Without provenance Details for llm_safecall-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-safecall 0.2.3
File Interpreter ABI Platform Actions
llm_safecall-0.2.3-py3-none-any.whl Python 3 none any Details for llm_safecall-0.2.3-py3-none-any.whl

Total release size: 60.8 kB

Release files / llm_safecall-0.2.3.tar.gz

Download URL llm_safecall-0.2.3.tar.gz
Size 33.4 kB
Tags Source
SHA-256 checksum
How to use checksums
de8d4d18cd377c3e9a8b3178e375af2b1664565de15e45423fef3abf1ebf39a1
BLAKE2b-256 checksum
How to use checksums
d11243dc07fd87fda8602e032a2683935a3aa08772fadf6e941be0824a4ca0be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / llm_safecall-0.2.3-py3-none-any.whl

Download URL llm_safecall-0.2.3-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7edc8e069cbb91dd6126b4b1fffcf61c9fa35edd5ef0e693434a65d45363d0bf
BLAKE2b-256 checksum
How to use checksums
ecf908755bdf5e258fd5a5b2d5cc4c09b27a926a1411b0bbbe153442da796b84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for llm-safecall 0.2.3

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.

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