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.2.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.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: observerai-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 3f89407873f3d103e70e282485f106dac65daa153feef96390fb3dd32934242d
MD5 059f1fa1073328879694be19e3715db1
BLAKE2b-256 fa53b937be954b3ca6717f0ec8c21e87ccee509665333fff65a309f8e5ac5e69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: observerai-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb6adcc07f69c83eca5d3c497b8348b03d3e48b0c1c398a9b2bc2e2e0e87cda5
MD5 4c22349f92a937bf2025f27ac106c322
BLAKE2b-256 539ea4453a727220477eb248affa45439549be56c5cbbb98a9ac1fb0453cec78

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