Skip to main content

Gravito auto-instrumentation SDK for Python — wrap OpenAI / Anthropic / LangChain / LlamaIndex / Google GenAI / Mistral / Cohere / Together clients so every LLM call is observed by the Gravito coherence ledger.

Project description

gravito-guardrails (Python)

Python mirror of @gravitoai/guardrails. Wrap an LLM client once at construction and every call POSTs an observation to the Gravito coherence ledger at /api/gravito/v1/llm-event.

Install

pip install gravito-guardrails

Quick start

import openai
from gravito import wrap_openai, with_surface

client = wrap_openai(
    openai.OpenAI(),
    api_key="grav_...",       # or set GRAVITO_API_KEY env var
    org_id=1,                  # or set GRAVITO_ORG_ID env var
    default_surface="my_app.llm_call",
)

# Every call below is now silently observed.
res = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)

# Tag a code region with a specific surface:
with with_surface("checkout.fraud_check"):
    client.chat.completions.create(model="gpt-4o", messages=[...])

Supported wrappers

Function SDK Hooks
wrap_openai(client) openai>=1.0 chat.completions.create, completions.create, responses.create, embeddings.create, images.generate
wrap_anthropic(client) anthropic messages.create, completions.create
wrap_mistral(client) mistralai chat.complete/stream, fim.complete, embeddings.create, agents.complete
wrap_cohere(client) cohere chat, chat_stream, generate, generate_stream, embed, rerank, classify, summarize
wrap_together(client) together chat.completions.create, completions.create, embeddings.create, images.generate
wrap_bedrock(client) boto3.client('bedrock-runtime') invoke_model, invoke_model_with_response_stream, converse, converse_stream
wrap_ollama(client) ollama chat, generate, embed, embeddings
wrap_groq(client) groq chat.completions.create, audio.transcriptions/translations.create, embeddings.create
wrap_perplexity(client) openai (with base_url='https://api.perplexity.ai') chat.completions.create, completions.create
wrap_google_genai(model) google-generativeai generate_content, generate_content_async, count_tokens, embed_content, plus chat session send_message/send_message_async
wrap_langchain(runnable) langchain invoke, ainvoke, stream, astream, batch, abatch, call, predict, generate, agenerate
wrap_llamaindex(obj) llama_index chat, complete, query, predict, async variants, stream variants

Surface context

with_surface(...) is a contextvars-backed context manager. It propagates through async (asyncio), threads spawned via threading.Thread (when you contextvars.copy_context().run(...)), and standard call stacks. It mirrors the JS withSurface() exactly.

from gravito import with_surface

with with_surface("rag_pipeline.synthesize", metadata={"user_id": user.id}):
    answer = chain.invoke({"question": q})

Design constraints

Same as the TypeScript package:

  • Fire-and-forget. Every observation POST is a daemon thread; never adds latency to the LLM call.
  • Fail-open. Errors POSTing to Gravito are logged and swallowed; the wrapped LLM call's return value is untouched.
  • No code changes after install. Wrap once at construction.

Production checklist

  1. Wrap every native SDK client and every LangChain/LlamaIndex top-level object.
  2. After your first wrapped call, hit the coverage endpoint to confirm rows landed:
    curl -H "X-Gravito-Api-Key: $GRAVITO_API_KEY" \
         -H "X-Gravito-Org-Id: $YOUR_ORG_ID" \
         https://gravito.ai/api/gravito/v1/coverage
    
  3. Schedule the daily integrity check (see the main repo's check-ingest-key-validity.yml) so silent key rotations don't break the integration for 40 days.

Differences from the TypeScript SDK

  • Python doesn't have JS's Proxy primitive, so wrappers monkey-patch bound methods on the client instance. The client object is mutated by the wrap call (and also returned for convenience).
  • Surface context uses contextvars.ContextVar, the Python equivalent of AsyncLocalStorage. Behavior matches exactly for asyncio code; for raw threads use contextvars.copy_context().run(...) to propagate.
  • The Vercel AI SDK wrapper (wrapVercelAI) isn't included — the Vercel AI SDK is TypeScript-only.

Related packages

Together these cover the dominant production AI surface area above the protocol layer.

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

gravito_guardrails-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

gravito_guardrails-0.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for gravito_guardrails-0.1.0.tar.gz
Algorithm Hash digest
SHA256 28b2d264d9c3a4ce5e5a0614a1bf58f4d37b9735110447892248551b288eceda
MD5 63aa6ada2cc302091db743048ef4bbb1
BLAKE2b-256 2a2ec4fd23d32bcf5112eb6a3615326000483375ae52227064fa510555919316

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gravito_guardrails-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3d86bae11aa7410c3bffca90761538ebb5f0d20b548fc4d02fb11c3fdcb09fe
MD5 7bd3da5b5eb8f2d4f2d3e8e7de694de3
BLAKE2b-256 8dd05f11531ff96e1a54787a64c095c77098121d67a9ac4c11aac764312e2654

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