Skip to main content

observerai - Structured Observability for Generative AI

Project description

📦 observerai – Structured Observability for Generative AI

observerai is a Python library that enables runtime observability and evaluation Log-as-Metric for Generative AI applications.

It is designed to be multi-provider (OpenAI, Gemini, Claude, etc.) and multi-modal (text, image, embeddings).

All metrics are emitted as structured logs following the Log-as-Metric approach, allowing seamless integration with platforms like GCP, Datadog, New Relic, Elastic, and others.

✅ Features

  • 📊 Structured metric logging for LLM usage
  • 🧵 Trace & span context via contextvars (thread-safe)
  • ⏱️ Latency & token usage tracking
  • 🧠 Prompt/response capture
  • 🚨 Exception tracing
  • 🧩 Flexible decorator interface
  • 🔧 Custom metadata support
  • 🧱 Built on top of pydantic and structlog

⚙️ Installation

pip install observerai[openai]

Only the OpenAI dependency is included for now. Future versions will support gemini, claude, etc.

🚀 How to Use

In your application, just decorate the function that makes OpenAI requests:

import uuid
from openai import OpenAI
from observerai.openai import metric_chat_create
from observerai.context import TraceContext

client = OpenAI()
TraceContext.set_trace_id(str(uuid.uuid4()))


@metric_chat_create(metadata={"user_id": "123"})
def test_openai_with_metadata():
    return client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": "Qual a capital da França?"}],
    )


@metric_chat_create()
def test_openai_without_metadata():
    return client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": "Qual a capital da Argentina?"}],
    )


resposta = test_openai_with_metadata()
print(resposta.choices[0].message.content)

resposta = test_openai_without_metadata()
print(resposta.choices[0].message.content)

📤 Output (Structured Log)

The decorator logs all metrics as a single structured JSON object to stdout:

{
  "trace_id": "fadfd7d6-9150-4327-961f-dad5f048add1",
  "span_id": "c346b100-30d8-4eea-91e3-ddcc67d8d5e0",
  "flow_id": "d52f542f-2211-45e6-94ca-a6d55617787e", 
  "response": {
    "status_code": 200,
    "latency": {
      "time": 481,
      "unit": "ms"
    }
  },
  "exception": null,
  "version": "0.0.1",
  "metadata": {
    "user_id": "123"
  },
  "name": "gpt-4o",
  "provider": "openai",
  "endpoint": "/chat/completions",
  "conversation": {
    "question": "Qual a capital da França?",
    "answer": "A capital da França é Paris."
  },
  "token": {
    "prompt": 14,
    "completion": 9,
    "total": 23
  },
  "evaluation": null,
  "event": "observerai.openai.completion.chat_create",
  "level": "info",
  "timestamp": "2025-03-24T19:21:08.115226Z"
}

🧭 Roadmap

  • OpenAI support (text completions)
  • Gemini & Claude providers
  • RAG evaluations (RagasX)
  • Plug-and-play LLM evaluations via TruLens

👥 Contributing

PRs and discussions are welcome. Stay tuned for contribution guidelines and plugin architecture.

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

observerai-0.0.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

observerai-0.0.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file observerai-0.0.3.tar.gz.

File metadata

  • Download URL: observerai-0.0.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.0 Darwin/24.3.0

File hashes

Hashes for observerai-0.0.3.tar.gz
Algorithm Hash digest
SHA256 17f005311219ce4637ac77d384ad169af7bbc984a932e647b25697c744a69c2d
MD5 2d25af526f31a425cf964f83ddfe7623
BLAKE2b-256 abe294666a5ad79430233942efce3631bcf367b55084590c925d3302403766bb

See more details on using hashes here.

File details

Details for the file observerai-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: observerai-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.0 Darwin/24.3.0

File hashes

Hashes for observerai-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8cd71d11d1effdb5a19d4a955e6c5d1cbe7d6b8bb4560462ff78a9c166fe7415
MD5 aa761b3954d5fde910051b0ed31502c4
BLAKE2b-256 4c99c1411d7ee62ea272a4e6f43d27ac478662b112b0d8368ae1da956dcf62f5

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