Skip to content

Evaluation and Safety

TraceVerde includes 6 built-in evaluation detectors for monitoring LLM input/output quality and safety. All features are opt-in and can be enabled individually.

PII Detection

Detect personally identifiable information using Microsoft Presidio.

genai_otel.instrument(
    enable_pii_detection=True,
    pii_mode="redact",       # "detect", "redact", or "block"
    pii_threshold=0.5,
    pii_gdpr_mode=True,
    pii_hipaa_mode=True,
    pii_pci_dss_mode=True,
)

Detects 15+ entity types: email, phone, SSN, credit cards, IP addresses, and more.

Examples:

Toxicity Detection

Detect harmful content using Perspective API (cloud) or Detoxify (local).

genai_otel.instrument(
    enable_toxicity_detection=True,
    toxicity_threshold=0.7,
)

Categories: toxicity, severe_toxicity, identity_attack, insult, profanity, threat.

Examples:

Bias Detection

Identify demographic biases in prompts and responses.

genai_otel.instrument(
    enable_bias_detection=True,
    bias_threshold=0.4,
)

8 bias types: gender, race, ethnicity, religion, age, disability, sexual_orientation, political.

Examples:

Prompt Injection Detection

Protect against prompt manipulation attacks.

genai_otel.instrument(
    enable_prompt_injection_detection=True,
    prompt_injection_threshold=0.5,
)

6 injection types: instruction_override, role_playing, jailbreak, context_switching, system_extraction, encoding_obfuscation.

Examples:

Restricted Topics Detection

Monitor and block sensitive topics.

genai_otel.instrument(
    enable_restricted_topics=True,
    restricted_topics=["medical_advice", "legal_advice", "financial_advice"],
    restricted_topics_threshold=0.5,
)

Examples:

Hallucination Detection

Track factual accuracy and groundedness.

genai_otel.instrument(
    enable_hallucination_detection=True,
    hallucination_threshold=0.7,
)

Includes factual claim extraction, hedge word detection, citation tracking, and context contradiction detection.

Examples:

Enable All Evaluations

genai_otel.instrument(
    enable_pii_detection=True,
    enable_toxicity_detection=True,
    enable_bias_detection=True,
    enable_prompt_injection_detection=True,
    enable_restricted_topics=True,
    enable_hallucination_detection=True,
)

See the comprehensive evaluation example and Ollama multiple evaluations.

Where Detectors Apply

Detectors and block policies apply to sync and async clients alike, to the OpenAI Responses API as well as Chat Completions, to all four AWS Bedrock runtime calls (converse, converse_stream, invoke_model, invoke_model_with_response_stream, including the system prompt), and to streamed requests, which are checked before they are sent. A blocked request raises PolicyViolationError before anything reaches the provider, whether or not its trace is sampled.

Framework-level spans (LangChain, LlamaIndex and agent frameworks) are not checked themselves. Detection happens on the provider call beneath them, so the provider's instrumentor has to be enabled for a block policy to take effect. input passed to an embeddings call is not treated as a prompt.

Not covered yet: a prompt passed positionally (LiteLLM, llama.cpp), and providers whose prompt is not messages, prompt or the Responses input: Google Gemini and Vertex AI (contents), Cohere chat (message) and Replicate (input). A block policy does not stop those requests.

Where Detectors Run

A block policy is checked before the request is sent, and the call waits for the verdict. On async clients that check runs on a worker thread, so it does not hold your event loop.

The detectors that annotate a span run after your call has returned, on a worker thread. The span is exported when they have finished, with the end time of your call, so its duration is the call's and not the detectors'. Two things follow from that:

  • evaluation.* attributes are not on the span at the moment the call returns. Code that reads them in the same process waits first:

    genai_otel.flush_telemetry()  # waits for pending evaluations, then flushes
    
  • An LLM span can be exported after the span that encloses it.

To have the detectors finish before the call returns, as they did up to 1.35.0:

genai_otel.instrument(enable_pii_detection=True, evaluation_mode="inline")

or set GENAI_EVALUATION_MODE=inline. Mistral, Hyperbolic, HuggingFace pipelines and Pydantic AI always evaluate inline. A streamed response is evaluated when its stream ends, on the thread that ends it, and only when content capture is on.

If your application installs its own tracer provider before calling instrument(), the detectors run inline and a warning says so: a provider this library did not create does not wait for spans that are still being evaluated when it is flushed or shut down.

When Detectors Fall Behind

At most GENAI_EVALUATION_QUEUE_SIZE spans wait for detectors. Past that a span is exported at once without evaluation, marked evaluation.skipped="overloaded", and a warning, at most one a minute, says how many were skipped. Tracing is never slowed or dropped because detectors are slow. Filter on evaluation.skipped to see how much went unevaluated.

Detectors share the Python interpreter with your application. A process that spends its time in Python code, as opposed to waiting on the network, leaves them less time.

At Shutdown

At exit and on uninstrument(), pending evaluations get GENAI_EVALUATION_SHUTDOWN_TIMEOUT seconds. Spans the detectors have not reached by then are exported without evaluation, marked evaluation.skipped="shutdown", so no span is lost. In a serverless function, call genai_otel.flush_telemetry() before returning, or use inline mode.

How Much Text Is Analysed

Detectors read the last GENAI_EVALUATION_MAX_CHARS characters of a prompt, which is the newest content of a conversation, and the first of a response. The default is 1500. A span whose text was longer says so:

Attribute Meaning
evaluation.truncated true when detectors read only part of the text
evaluation.prompt.length Characters in the prompt
evaluation.prompt.analyzed_length Characters of the prompt the detectors read
evaluation.response.length Characters in the response
evaluation.response.analyzed_length Characters of the response the detectors read
evaluation.pii.prompt.pattern_analyzed_length Characters the PII patterns covered
evaluation.pii.prompt.model_analyzed_length Characters the PII entity model read
evaluation.<detector>.prompt.analyzed_length Characters a pattern-based detector with a block policy read: the whole prompt

PII detection keeps pattern coverage of the whole prompt. Card numbers, e-mail addresses, phone numbers, US social security numbers, IP addresses, the Indian identifiers and your custom patterns are found wherever they are. Entity types that need the language model, such as names and locations, are looked for only in the analysed part.

The local toxicity model has its own, smaller limit: it reads the last GENAI_TOXICITY_MAX_CHARS characters of the prompt (default 500), recorded in evaluation.toxicity.prompt.analyzed_length. With both limits at 0 it scores the whole text in pieces and reports the highest score per category.

PII in redact mode is not limited: a redacted copy is only built from text that was analysed. Under GENAI_PROFILE=strict both limits default to 0.

A block policy on prompt injection, restricted topics or bias always reads the whole prompt, whatever the limit, so padding a prompt cannot hide text from it. The span records that in evaluation.<detector>.prompt.analyzed_length.

The limit applies to PII and toxicity block policies

With a block policy, text outside the analysed part is not checked for toxicity, and not for names or locations. Set GENAI_EVALUATION_MAX_CHARS=0 and GENAI_TOXICITY_MAX_CHARS=0 when every character must be checked. Detection time then grows with the length of the prompt.

With a block policy a prompt is analysed once per call: the check before the request and the annotation after it share the result. Each detector remembers its most recent results, keyed by a digest of the analysed text and never by the text itself (GENAI_EVALUATION_CACHE_SIZE).

Toxicity Model Options

GENAI_TOXICITY_QUANTIZE=true runs the local Detoxify model with 8-bit weights. It is faster, and its scores differ from the default model's by enough to change some verdicts near the threshold, so compare the two on your own data before relying on it. It is off by default.

GENAI_TOXICITY_TORCH_THREADS sets how many threads torch may use. The setting is process-wide and applies to every torch model in the process, so it is unset by default.

Enabling only the pattern-based detectors (bias, prompt injection, restricted topics, hallucination) does not load torch or the toxicity model.

Metrics

Each detector records metrics for monitoring:

  • genai.evaluation.<detector>.detections - Detection events
  • genai.evaluation.<detector>.blocked - Blocked requests
  • genai.evaluation.<detector>.score - Score distribution (histogram)

See the README for the complete list of span attributes.

Evaluation outcomes, provenance, and coverage

Evaluation spans carry bounded outcome metadata so a missing verdict cannot be read as a successful check:

Attribute Values / meaning
evaluation.outcome completed, no_applicable_input, unsupported_operation, skipped, or error for the detector suite
evaluation.<detector>.outcome The same bounded values for pii, toxicity, bias, prompt_injection, restricted_topics, and hallucination
evaluation.content.outcomes A bounded list containing captured, omitted, truncated, redacted, rejected, or error as applicable
evaluation.provenance.sdk.name / .version SDK identity and installed version
evaluation.provenance.detector.<name>.implementation / .version Detector implementation identifier and SDK version
evaluation.config.fingerprint SHA-256 of a normalized schema version and the enabled detector names; thresholds, custom values, credentials, and content are excluded

completed means that the detector ran on at least one applicable text input. It is not a claim that every part of the operation was captured, that detection is complete, or that a policy was enforced. no_applicable_input means the instrumentor did not provide usable text; unsupported_operation identifies operations such as embeddings, for which these text detectors do not apply; skipped records work bypassed by queue pressure or shutdown; and error records a detector failure. These attributes are additive and do not change detection, redaction, blocking, or content-capture behavior.

Operation surfaces

The evaluation processor reads text supplied through GenAI prompt events (gen_ai.prompt.*), prompt attributes (gen_ai.prompt, gen_ai.request.prompt, gen_ai.request.first_message), OpenInference llm.input_messages / llm.output_messages, and common input.value / output.value attributes. It analyzes text that the SDK instrumentor can extract as a string. Input checks cover prompts; PII, toxicity, bias, and restricted-topic checks can also cover response text; prompt-injection checks cover prompts; hallucination checks cover responses.

Streaming checks run after a supported stream finishes and its instrumentor has assembled text. A stream that is abandoned, closed before text is available, or whose instrumentor does not expose text may have no applicable input or be skipped. Embedding vectors, image/audio payloads, and other non-text content are not evaluated by these detectors. Only content actually present on the span can be reported as captured; no outcome field contains source text. The reported outcome is instrumentation evidence, not proof of complete capture or runtime enforcement. Blocking is enforced only by the separate pre-call policy hook.

All Examples

Browse all evaluation examples in the examples/ directory.