Skip to main content

Google GenAI wrapper with mandatory generate → format → validate pipeline

Project description

safe-genai

PyPI version Python License: MIT Ruff

A Python library that wraps Google Gemini with a mandatory generate → format → validate pipeline, so every response is structured and semantically checked before you receive it.

How it works

Every call to SafeAgent.run() executes three steps in sequence:

  1. Generate — calls Gemini (with Google Search grounding) using your system prompt
  2. Format — coerces the raw text into your Pydantic schema via a second LLM call
  3. Validate — verifies the output fulfills the original prompt requirements, rejecting evasive non-answers (skippable via skip_validation=True)

If validation fails, the pipeline retries from the top (configurable via max_retries). On final failure it raises SafeGenAIValidationError.

Installation

pip install safe-genai

Requires a Google Gemini API key or Vertex AI project:

export GEMINI_API_KEY="your-key-here"

Quickstart

from pydantic import BaseModel
from safe_genai import SafeAgent

class LatestRelease(BaseModel):
    version: str
    release_date: str
    notable_changes: list[str]

agent = SafeAgent(
    output_schema=LatestRelease,
    system_prompt=(
        "You are a software research assistant. "
        "Use Google Search to find the most recent release information. "
        "Always return the latest published version, never guess."
    ),
)

result = agent.run("What is the latest stable release of Python?")
print(result.output.version)
print(result.output.release_date)

Async usage

result = await agent.run_async("What is 2 + 2?")

Configuration

from safe_genai import SafeAgent, AgentConfig

agent = SafeAgent(
    output_schema=Answer,
    system_prompt="You are a helpful assistant.",
    additional_formatter_instructions="Always express numbers as digits, not words.",
    additional_validator_instructions="Reject answers with confidence below 0.8.",
    max_retries=2,
    config=AgentConfig(
        generator_model="gemini-3.1-pro-preview",
        formatter_model="gemini-3-flash-preview",
        validator_model="gemini-3-flash-preview",
        api_key="your-key",  # or set GEMINI_API_KEY env var
    ),
)

Vertex AI (gcloud ADC)

If you're already authenticated with gcloud auth application-default login, you can use Vertex AI instead of an API key:

from safe_genai import SafeAgent, AgentConfig

agent = SafeAgent(
    output_schema=Answer,
    system_prompt="You are a helpful assistant.",
    config=AgentConfig(
        vertexai=True,
        vertex_project="my-gcp-project",   # or set GOOGLE_CLOUD_PROJECT env var
        vertex_location="us-central1",     # default
    ),
)

No GEMINI_API_KEY is needed — the client uses your ambient gcloud credentials.


Error handling

from safe_genai import SafeGenAIValidationError, SafeGenAIFormatterError

try:
    result = agent.run("some prompt")
except SafeGenAIValidationError as e:
    print(f"Validation failed after {e.attempts} attempt(s): {e.feedback}")
    print(e.output)        # last Pydantic model produced
    print(e.raw_response)  # raw text from the generator
except SafeGenAIFormatterError as e:
    print(f"Could not coerce output into {e.schema_name}: {e.cause}")

Token usage

Every result includes a breakdown of token usage by pipeline step, accumulated across all retry attempts:

result = agent.run("What is the latest stable release of Python?")

print(result.usage.generator)  # Usage(input_tokens=..., output_tokens=...)
print(result.usage.formatter)  # Usage(input_tokens=..., output_tokens=...)
print(result.usage.validator)  # Usage(input_tokens=..., output_tokens=...)
print(result.usage.total)      # Usage(input_tokens=..., output_tokens=...)  ← sum of all three

Usage is a plain dataclass with two fields: input_tokens and output_tokens.


Development

uv sync --extra dev
uv run pytest
uv build

License

MIT — see LICENSE.

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

safe_genai-0.3.0.tar.gz (62.9 kB view details)

Uploaded Source

Built Distribution

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

safe_genai-0.3.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file safe_genai-0.3.0.tar.gz.

File metadata

  • Download URL: safe_genai-0.3.0.tar.gz
  • Upload date:
  • Size: 62.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for safe_genai-0.3.0.tar.gz
Algorithm Hash digest
SHA256 68e11bcbddd10d3357f3cd20a5ca86a288ea96e444440405f8eb5fbd638d7179
MD5 71b79311ce945749627a3dcf8dbe58ca
BLAKE2b-256 cf649017ad4aa3dab122fa13ad21de1174099c5b023f39ee38825c1840c7d19e

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_genai-0.3.0.tar.gz:

Publisher: publish.yml on GuilleQP/safe-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file safe_genai-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: safe_genai-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for safe_genai-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f76d1293dd50a770f14314ea46e042b059e6a3ba45e787d341a5a584ac39209
MD5 9a2323a3825fcbcf08b06cc4afeaa670
BLAKE2b-256 25b37fe0dd500cb048b68a3501056569bae7db48c5c1909b87ea73e2f406f726

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_genai-0.3.0-py3-none-any.whl:

Publisher: publish.yml on GuilleQP/safe-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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