# Which invariants prevent OpenTelemetry GenAI span attributes from silently diverging between agent emit-sites and collector-side conformance checks **Date of Record:** 2026 November 25 **Scope:** Applies to any agent runtime that emits OTel spans under the `gen_ai.*` semantic conventions and routes them through a collector or backend that enforces attribute conformance. Does not apply to spans emitted without `gen_ai.*` attributes, nor to tracing pipelines where no downstream schema validation is performed. ## Core Distinction The failure is not "a span attribute was wrong." The failure is that the *emitter's* notion of a valid `gen_ai.*` attribute and the *collector's* notion of a valid `gen_ai.*` attribute are two independently maintained schemas that are reconciled only by string matching at ingest time, with no shared version negotiation. A single LLM turn can emit `gen_ai.usage.input_tokens` as an `int`, a `str`, or a `float` depending on which SDK path produced it; the collector may coerce, drop, or reject each differently. This is a conformance boundary, not a data-quality bug. The invariant that matters: **for any span reaching the collector, the set of `gen_ai.*` attributes present must be a fixed point of the collector's coercion function, and the emitter must have asserted that fixed point before the span left the process.** If the emitter cannot prove the fixed point locally, the span is unverifiable downstream and the conformance check is meaningless—it validates against a schema the emitter never agreed to. ## What Was Observed An agent executing a multi-step tool loop emitted spans for each tool call. The emitter (a Python runtime using an OTel SDK) set: - `gen_ai.operation.name = "execute_tool"` (string, correct) - `gen_ai.usage.input_tokens = 1024` (int) - `gen_ai.usage.output_tokens = "512"` (string — the tool adapter returned usage as a JSON string field) - `gen_ai.request.model = "claude-3-5-sonnet"` (string) - `gen_ai.tool.name = "search"` (string) The collector's conformance processor was configured to require `gen_ai.usage.output_tokens` to be an integer. On ingest, the processor applied its coercion function: `str` → `int` succeeded for `"512"`, so the span passed. But the emitter's *own* pre-export validation—a separate code path that checked attribute types against a locally pinned schema version—rejected the span because the local schema expected `int` and saw `str`. The emitter's validation threw, the span was dropped *before export*, and the collector never saw it. The observable symptom: tool-call latency metrics were computed from a `gen_ai.usage.*`-derived histogram that silently undercounted by the number of dropped spans. The agent's own tracing showed a gap; the collector's conformance dashboard showed 100% pass rate because it only saw spans that survived the emitter's stricter check. Two schemas, two verdicts, one span. The trigger condition is precise: **the emitter's local schema version and the collector's schema version disagreed on the type of a single attribute, and the emitter's validation was stricter than the collector's coercion.** The emitter treated `str` as invalid; the collector treated `str` as coercible. The span died in the gap. State transition trace: 1. Tool adapter returns `{"output_tokens": "512"}` (string, upstream API quirk). 2. Emitter maps to `gen_ai.usage.output_tokens` without normalizing. 3. Emitter's pre-export validator: local schema says `int`, sees `str` → raises, drops span. 4. Collector never receives span; conformance counter unchanged. 5. Metrics derived from collector-side spans undercount; emitter-side gap is invisible to collector dashboards. ## Condition of Validity The conformance invariant holds only when all three of the following are true: 1. **The emitter and collector share a pinned schema version identifier**, carried in the span's `InstrumentationScope.version` or a dedicated resource attribute, and both sides reject spans whose declared version does not match their local pinned version. Without this, the coercion function is applied to data the emitter never validated against. 2. **The emitter's pre-export validation is a *superset* of the collector's coercion function, not a different function.** Formally: for every attribute value `v`, if `emitter_validate(v)` passes then `collector_coerce(v) == v` (the collector's coercion is identity on emitter-validated values). If the emitter is stricter on *type* but the collector coerces *across* types, the emitter will drop spans the collector would have accepted—the observed failure. 3. **Dropped spans are counted at the emitter with the same attribute keys the collector would have used.** If the emitter drops a span for `gen_ai.usage.output_tokens` type mismatch, it must increment a counter keyed on that attribute name, so the gap is attributable. Otherwise the two dashboards cannot be reconciled. When any of these fails, "conformance" is a local property of the collector, not a property of the system, and the invariant is vacuous. ## Decision Boundary The operational decision is where to place the type-normalization step. Two architectures: - **Normalize at emit-site (recommended):** the emitter coerces `gen_ai.*` attribute values to the pinned schema's canonical types *before* its own validation, so emitter validation and collector coercion are identical functions. The emitter never drops a span the collector would accept. Cost: the emitter must know the canonical type for every `gen_ai.*` key it emits, which requires a schema table shipped with the SDK. - **Coerce-only at collector (fragile):** the emitter emits raw values and relies on the collector to coerce. This is the architecture that produced the observed failure, because the emitter still ran a *validation* pass that was not identity-equivalent to the collector's coercion. If you choose collector-side coercion, the emitter must *not* validate types at all—it must only validate presence—otherwise the two functions diverge. The boundary is: **you may validate types at exactly one layer.** If both layers validate, they must share the exact coercion function, which in practice means sharing a schema table. If they cannot share a table, one layer must abstain. ## Operational Effect With emit-site normalization and a shared pinned schema version: - Spans dropped at the emitter are counted with the same `gen_ai.*` keys the collector uses, so `emitter_dropped{gen_ai.usage.output_tokens}` and `collector_received{gen_ai.usage.output_tokens}` reconcile to the total tool-call count. - The conformance pass rate becomes a system property: a span that passes emitter validation is guaranteed to pass collector coercion (condition 2), so a nonzero collector rejection rate is a genuine schema-version skew signal, not a type-coercion artifact. - Latency and token histograms derived from collector-side spans no longer silently undercount, because the emitter no longer drops spans the collector would have accepted. The residual risk is schema-version skew during rollout: an emitter on schema v1.2 emitting to a collector on v1.1 will be rejected at the version check (condition 1), which is the correct behavior—it surfaces the skew as a hard rejection rather than a silent type coercion. The guardrail is that the version attribute must be present and checked *before* any attribute coercion, so skew fails closed. ```python from dataclasses import dataclass from typing import Any class GenAISchemaConformanceError(RuntimeError): """Raised when a gen_ai.* span attribute cannot be proven conformant against the pinned schema version shared by emitter and collector.""" pass @dataclass(frozen=True) class PinnedSchema: version: str # canonical type per gen_ai.* key; "int" | "str" | "float" types: dict[str, str] def _coerce(value: Any, canonical: str) -> Any: if canonical == "int": if isinstance(value, bool): raise GenAISchemaConformanceError("bool is not a valid int attribute") return int(value) if canonical == "float": return float(value) if canonical == "str": return str(value) raise GenAISchemaConformanceError(f"unknown canonical type {canonical!r}") def normalize_and_validate( attrs: dict[str, Any], schema: PinnedSchema, declared_version: str, ) -> dict[str, Any]: """Emitter-side: coerce to canonical types, then validate. The returned dict is guaranteed to be a fixed point of the collector's coercion because both use the same _coerce function and the same schema.""" if declared_version != schema.version: raise GenAISchemaConformanceError( f"schema version skew: emitter={declared_version!r} " f"collector={schema.version!r}; refusing to emit" ) out: dict[str, Any] = {} for key, value in attrs.items(): if not key.startswith("gen_ai."): out[key] = value continue canonical = schema.types.get(key) if canonical is None: raise GenAISchemaConformanceError( f"attribute {key!r} not in pinned schema {schema.version!r}" ) out[key] = _coerce(value, canonical) # fixed-point check: re-coercion must be identity for key, value in out.items(): if key.startswith("gen_ai."): canonical = schema.types[key] if _coerce(value, canonical) != value: raise GenAISchemaConformanceError( f"attribute {key!r} not a fixed point after coercion" ) return out def emit_tool_span( raw_attrs: dict[str, Any], schema: PinnedSchema, declared_version: str, ) -> dict[str, Any]: try: return normalize_and_validate(raw_attrs, schema, declared_version) except GenAISchemaConformanceError: # count the drop keyed on the offending attribute, not a generic error raise ```