Skip to main content

Validate LLM-generated tool args before execution. Wraps tool functions with arg validation, raises ToolArgError with LLM-friendly retry hint. Python port of @mukundakatta/agentvet.

Project description

agentvet-py

PyPI Python License: MIT

Validate LLM-generated tool args before execution. Wraps tool functions with arg validation; raises ToolArgError with an LLM-friendly retry hint. Zero runtime dependencies.

Python port of @mukundakatta/agentvet.

Install

pip install agentvet-py
# pydantic adapter is optional:
pip install "agentvet-py[pydantic]"

Usage

from agentvet import vet, adapters, ToolArgError

def send_email_impl(args):
    return "sent to " + args["to"]

schema = adapters.shape({"to": "str", "subject": "str", "body": "str?"})
send_email = vet(name="send_email", schema=schema, fn=send_email_impl)

# Happy path:
send_email({"to": "alice@example.com", "subject": "hello"})  # 'sent to alice@example.com'

# Bad args -> raises ToolArgError BEFORE the tool runs:
try:
    send_email({"subject": "hello"})  # missing 'to'
except ToolArgError as err:
    feedback = err.to_llm_feedback()
    # Send `feedback` back to the LLM as a tool_result with is_error=True.

With pydantic

from pydantic import BaseModel, EmailStr
from agentvet import vet, adapters

class SendEmail(BaseModel):
    to: EmailStr
    subject: str
    body: str | None = None

send_email = vet(
    name="send_email",
    schema=adapters.pydantic(SendEmail),
    fn=lambda args: send_impl(**args),
)

Async tools

vet() preserves the sync/async nature of fn:

async def fetch_impl(args):
    return await api.get(args["url"])

fetch = vet(name="fetch", schema=adapters.shape({"url": "str"}), fn=fetch_impl)
result = await fetch({"url": "https://example.com"})

API

vet(*, name, schema, fn, on_error=None) -> wrapped_fn

Wraps a tool function. Validates args BEFORE calling the tool; raises ToolArgError on failure (or invokes on_error(err, args) if provided -- return a non-None value to substitute as the tool's return).

validate(name, schema, args) -> ValidationResult

One-shot validation. Returns ValidationResult(ok=bool, value=..., error=ToolArgError | None).

adapters.shape(spec)

Tiny built-in shape checker. Spec format: {"field": "str"|"int"|"float"|"bool"|"list"|"dict", ...}. Suffix with ? for optional. Accepts JS sibling synonyms ("string", "number", "array", "object") too.

adapters.fn(predicate, error_builder?)

Predicate adapter. predicate(args) -> bool. error_builder may be a string or a callable returning a string.

adapters.pydantic(model_cls)

Wraps a pydantic v2 BaseModel. Requires pip install agentvet-py[pydantic].

adapters.zod(schema)

Compatibility shim for safeParse-style validators (rare in Python; included for parity).

ToolArgError

Carries tool, validation_error, args. .to_llm_feedback() returns the retry message you send back to the LLM.

API differences from the JS sibling

  • vet() uses keyword-only args (vet(name=..., schema=..., fn=..., on_error=...)).
  • Validators return Python dicts: {"valid": True, "value": ...} / {"valid": False, "error": str}.
  • adapters.pydantic replaces adapters.zod as the natural Python adapter.
  • Type names in shape() use Python conventions ("str", "int", "list", "dict"); JS synonyms ("string", "number", "array", "object") are accepted.
  • ToolArgError.to_llm_feedback() is snake_case (mirrors JS toLLMFeedback()).

See the JS sibling's README for the full design notes.

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

agentvet_py-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

agentvet_py-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file agentvet_py-0.1.0.tar.gz.

File metadata

  • Download URL: agentvet_py-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for agentvet_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bb51e0ed7918ff3cb3062be1e8ebb3dc4fa352de5596b7c6b661fd71cda7ab99
MD5 92f1a7eec47257ce8fe5648539e22a26
BLAKE2b-256 a892c153442d0a61f6ac516c6a9cad5419b22442d292144e1cd0d8df77a5c195

See more details on using hashes here.

File details

Details for the file agentvet_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agentvet_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for agentvet_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5eabf81ccfb5d4b06d2755c6724e6ea7979f6cf49639b4a9630fe5b2c1fce74
MD5 3529373fe85aa8fb6db6de2083062af9
BLAKE2b-256 026644632194a3bcf5005419bba30375c2aa01c8a05a88be45bc8da0f46af605

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