Skip to main content

Lightweight JSONL tracing for debugging failed RAG answers.

Project description

rag-failcase-kit

rag-failcase-kit is a lightweight Python package for recording one RAG pipeline run as one JSONL trace.

When a RAG application returns an empty, inaccurate, or weakly relevant answer, it is often hard to tell where the failure happened. The issue may be retrieval quality, prompt construction, fallback logic, answer generation, or a runtime error. In small projects this often becomes a pile of print statements, which is difficult to search, compare, or replay later.

This package keeps the first version intentionally small: it records query, retrieved documents, scores, fallback usage, answer preview, latency, status, and errors into a JSONL file.

Installation

From PyPI:

pip install rag-failcase-kit

From a local checkout:

pip install -e .

For development and release checks:

pip install -e ".[dev]"

30-Second Usage

from rag_failcase_kit import RagLogger

logger = RagLogger(project="demo-rag", log_path="logs/rag_traces.jsonl")

with logger.trace("What is RAG?") as trace:
    trace.log_retrieval(
        [
            {
                "content": "Retrieval-augmented generation combines retrieval with text generation.",
                "source": "docs/rag.md",
                "score": 0.91,
            }
        ],
        top_k=3,
        min_score=0.7,
    )
    trace.log_answer("RAG retrieves relevant context and uses it to generate an answer.")

The log directory is created automatically. Each trace is appended as one JSON object per line.

CLI Usage

The package installs a rag-failcase command:

rag-failcase summary logs/rag_traces.jsonl
rag-failcase failures logs/rag_traces.jsonl
rag-failcase inspect logs/rag_traces.jsonl --trace-id <trace_id>

All CLI commands print JSON output.

Example summary:

{
  "total_traces": 12,
  "successes": 9,
  "failures": 3,
  "fallback_used": 4,
  "avg_latency_ms": 128.42
}

Log Schema

Example JSONL record:

{
  "trace_id": "9fcd7b91-6b12-46fb-8ff1-5f6f1ef49b15",
  "project": "demo-rag",
  "query": "What is RAG?",
  "retrieval": {
    "documents": [
      {
        "content_preview": "Retrieval-augmented generation combines retrieval with text generation.",
        "metadata": {
          "chunk_id": "rag-001"
        },
        "source": "docs/rag.md",
        "score": 0.91
      }
    ],
    "top_k": 3,
    "min_score": 0.7
  },
  "retrieved_count": 1,
  "fallback": {
    "used": false,
    "strategy": null,
    "reason": null
  },
  "answer": "RAG retrieves relevant context and uses it to generate an answer.",
  "answer_preview": "RAG retrieves relevant context and uses it to generate an answer.",
  "latency_ms": 3.241,
  "status": "success",
  "error": null,
  "created_at": "2026-07-09T00:00:00+00:00"
}

Document content is stored as a bounded preview to avoid unexpectedly large logs.

Real RAG Integration

Wrap the part of your RAG pipeline that handles one user query:

from rag_failcase_kit import RagLogger

logger = RagLogger(project="support-bot", log_path="logs/support_rag.jsonl")


def answer_question(query, retriever, llm):
    with logger.trace(query) as trace:
        documents = retriever.search(query)
        trace.log_retrieval(documents, top_k=5, min_score=0.75)

        if not documents:
            trace.log_fallback("no_documents", reason="Retriever returned no documents.")
            answer = "I do not have enough context to answer."
            trace.log_answer(answer)
            return answer

        answer = llm.generate(query=query, documents=documents)
        trace.log_answer(answer)
        return answer

documents can be dictionaries or document-like objects. For objects, the logger safely checks common fields such as page_content, metadata, source, and score.

Optional Integrations

The package does not depend on FastAPI, LangChain, or LangGraph.

FastAPI example:

pip install fastapi uvicorn
uvicorn examples.fastapi_example:app --reload

LangGraph-style example:

python examples/langgraph_example.py

See:

Development

pip install -e ".[dev]"
pytest
python -m build
twine check dist/*

To test the console script in a clean environment:

python -m venv .venv-test
. .venv-test/bin/activate
pip install dist/rag_failcase_kit-0.1.0-py3-none-any.whl
rag-failcase --help

Roadmap

  • Add prompt logging with explicit redaction controls.
  • Add richer failure categorization helpers.
  • Add Markdown or HTML reports.
  • Add log rotation and sampling controls.
  • Add optional adapters for popular RAG frameworks without making them required dependencies.

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

rag_failcase_kit-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for rag_failcase_kit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5d6f4a0218b12c8267b55d1b151cd1164c6e04ef0bb53e6caed4da9a06a901ce
MD5 53c4b5c8f37bc1972dfb0cc3e9cc2a72
BLAKE2b-256 f8f6814a1e35e2a843ea6c6daf05a2a8b559b4d72b12671e81e73fda7f710b0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for rag_failcase_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 522e502e8b086b54c582f5cee6ec98c6ae706cbfb609ac2c0e17d809048c479c
MD5 5b6be820bb3ef2c3583764e90f0db43a
BLAKE2b-256 c620eb84b7df3d15feb97b3e56d8ffcc59dc38722833feb11fbfd869150c2ca7

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