Skip to main content

Lightweight guard layer for AI agents — retry, catch, log.

Project description

Veridian Guard 🛡️

Robust retry and fallback decorators for unpredictable AI agents, LLM calls, and flaky network requests.

When your AI agents crashes, the API rate limits you, or a network request fails, Veridian Guard gracefully catches the errors, retries the execution with custom delays, and provides safe fallbacks to prevent production crashes.


🚀 Installation

pip install veridian-guard

⚡ Quick Start (Sync)

Wrap any flaky function like an LLM API call with the @guard decorator:

from veridian_guard import guard
import random

@guard(max_retries=3, delay=1.5, fallback="Default safe response")
def call_llm_agent():
    # Simulating a random API failure
    if random.random() < 0.7:
        raise ConnectionError("LLM API Timeout!")
    return "Agent succeeded! Here is your generated text."

result = call_llm_agent()
print(result)

🔄 Async/Await Support Built-in

Veridian Guard automatically detects whether your function is synchronous or asynchronous. It works flawlessly with async/await, making it perfect for modern AI agent chains or Crew AI.

import asyncio
from veridian_guard import guard

@guard(max_retries=3, delay=2.0, fallback="Failed", async_def_fetch_data_from_llm=True)
async def fetch_data_from_llm():
    # Simulating a heavy async API call
    await asyncio.sleep(1)
    raise TimeoutError("API is too busy")

async def main():
    result = await fetch_data_from_llm()
    print(result)  # Will print the fallback: "Failed"

asyncio.run(main())

✨ Why Veridian Guard?

Zero Dependencies - Pure Python, Extremely lightweight.

Smart Logging Built-in - Automatically logs failed attempts and warnings so you can monitor your agent's behavior in the terminal.

Fail-Safe Fallbacks - Never let an unhandled exception crash your main application loop again.

Universal - Seamlessly handles both def and async def functions out of the box.


🔧 Advanced Usage

Catching specific exceptions instead of all errors:

from veridian_guard import guard

@guard(
    max_retries=5,
    delay=2.5,
    exceptions=(TimeoutError, ConnectionError)
)
def call_data():
    # Will only retry on TimeoutError or ConnectionError
    # Other exceptions like ValueError will be raised immediately
    pass

📦 Parameters

Parameter Type Default Description
max_retries int 3 Maximum number of retry attempts
delay float 1.0 Delay in seconds between retries
fallback any None Value to return if all retries fail
exceptions tuple (Exception,) Specific exceptions to catch and retry

📝 Real-World Use Cases

1. LLM API Calls

@guard(max_retries=5, delay=2.0, fallback="Sorry, AI is unavailable")
def ask_chatgpt(prompt):
    return openai.ChatCompletion.create(...)

2. Database Connections

@guard(max_retries=3, delay=1.0, exceptions=(ConnectionError,))
def connect_to_db():
    return psycopg2.connect(...)

3. Web Scraping

@guard(max_retries=4, delay=3.0, fallback=[])
def scrape_website(url):
    response = requests.get(url, timeout=5)
    return response.json()

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


📄 License

MIT License - feel free to use in your projects!


🌟 Star Us!

If Veridian Guard helped you build more resilient AI agents, give us a star on GitHub! ⭐


Made with 💚 for the AI Agent community

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

veridian_guard-0.2.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

veridian_guard-0.2.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file veridian_guard-0.2.1.tar.gz.

File metadata

  • Download URL: veridian_guard-0.2.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for veridian_guard-0.2.1.tar.gz
Algorithm Hash digest
SHA256 07e09a5141a4b73b15b71ccf7d3d1314696894eb6c5dcd756f3a281176b536f9
MD5 54c862831daa6f4af7eea63d9409ff18
BLAKE2b-256 deaeb043373b3b0edd6b88572286d9f5dfb90e043565b8d4b633efd89f450cf5

See more details on using hashes here.

File details

Details for the file veridian_guard-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: veridian_guard-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for veridian_guard-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d098cc46da18d451ffa2f645b58e03e6284c91edfd235d7615d093ebfb1d95e4
MD5 e5bc27be26bcfeaf3ebd3b6374c3414a
BLAKE2b-256 a4fc9c0327c93434a40388b93e06a637fb3dac7b1949a53480684802d8cba8d3

See more details on using hashes here.

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