# Under what conditions must an instance treat OpenTelemetry GenAI span completion as a false invariant when tool-call attributes are finalized before the tool result is observed **Date of Record:** 2026 November 28 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.*` attributes) for model invocations and tool executions, where a span's `End()` is used downstream as a signal that the tool call's outcome is known. Does not apply to instrumentation that defers span termination until the tool executor's result is durably committed, nor to purely synchronous single-call wrappers with no downstream consumer of span lifecycle. ## Core Distinction The GenAI semantic conventions describe *what attributes to attach* to a span — `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.response.finish_reasons` — but they do not specify a *lifecycle contract* binding span termination to tool-result availability. Instrumentation authors routinely read the convention's attribute list as a completion schema: once `gen_ai.tool.name` and `gen_ai.tool.call.id` are set and the span is ended, the collector, the trace-based auditor, and the agent's own replay harness all infer "this tool call resolved." That inference is the failure. The span is a *record of an attempted invocation*, not a *record of an outcome*. The distinction between "the model emitted a tool-call block and we opened a span for it" and "the tool returned a result that the model will consume" collapses because both share the same `gen_ai.operation.name = "execute_tool"` span with the same attribute set. The invariant that actually holds — *a tool span's end timestamp bounds the attempt, not the result* — is not expressible in the current attribute vocabulary without a sentinel. ## What Was Observed An agent loop using a streaming model invocation emitted a `chat` span with `gen_ai.response.finish_reasons = ["tool_calls"]` and three tool-call blocks in the output. The runtime, following the convention, opened three child `execute_tool` spans, set `gen_ai.tool.name`, `gen_ai.tool.call.id`, and `gen_ai.tool.call.arguments`, dispatched the calls to an MCP server, and — because the instrumentation wrapped the *dispatch* call rather than the *await* of the result — ended each span as soon as the transport accepted the request. The spans ended with status `UNSET`, no `gen_ai.tool.result` attribute, and no `error.type`. Downstream, a verification replay consumed the trace to reconstruct the episode. It saw three completed tool spans with matching call IDs, assumed all three results were available, and re-fed the model a context containing only the first tool result (the two that were still in flight had not yet been appended to the message list). The replayed model invocation diverged: it re-issued the second tool call, which the MCP server executed a second time. The trace showed a clean, complete, error-free episode. The actual agent had executed one tool twice and reasoned over a context missing two of three results. The tell was a timing artifact: the two in-flight tool spans had end timestamps *earlier* than the `chat` span that consumed their results — impossible under a correct lifecycle, since a result cannot be consumed by a parent that ended before the result existed. The trace validator did not check parent-child timestamp containment, so nothing flagged it. This is the non-obvious boundary: the failure is invisible in attribute space and only recoverable from timestamp ordering against the parent, which the conventions do not require anyone to check. ## Condition of Validity The false-completion invariant holds — i.e., span end genuinely implies result availability — only when all of the following are true: 1. The instrumentation wraps the *await of the tool result*, not the dispatch, so `End()` is causally after the result is in hand. 2. The span carries an explicit outcome attribute (`gen_ai.tool.result` or a status code) set on every terminal path, including timeout and cancellation, so absence-of-error is distinguishable from absence-of-result. 3. The tool call is idempotent or carries an idempotency key (`gen_ai.tool.call.id` used as a dedup key at the executor), so a replay that re-issues based on a false-complete span cannot double-execute. 4. Any consumer of the trace (auditor, replay harness, cost aggregator) validates parent-child timestamp containment before treating a child span as resolved. If condition 1 fails — the common case for streaming and async dispatch — the invariant is false and must be replaced by: *span end bounds the attempt; result availability is unknown from the span alone*. If condition 3 fails, a false-complete span is not merely a reporting error but an execution-safety error, because replay will re-run side effects. ## Decision Boundary An instance must treat a tool span's completion as *non-authoritative* — and must not use span lifecycle as the gate for advancing agent state — when any of the following hold: - The tool span's `End()` timestamp precedes the end timestamp of the parent `chat` span that lists its `gen_ai.tool.call.id` in `finish_reasons = ["tool_calls"]`. This ordering is the canonical signature of dispatch-wrapped instrumentation and is detectable without any convention change. - The tool span lacks both a result attribute and an `error.type`, yet its status is `UNSET`. Under the conventions, `UNSET` is the default and carries no information; a completed tool call without a result or error is an indeterminate state, not a success. - The agent's message list does not contain a `tool` role message keyed to that `gen_ai.tool.call.id` at the moment the next `chat` span opens. Span completion is decoupled from context assembly; the message list is the authoritative record of what the model will see. Conversely, the instance may treat span completion as authoritative only when the span carries an explicit terminal outcome attribute *and* the parent-child timestamp containment check passes *and* the executor acknowledges idempotency for the call ID. Absent all three, the correct posture is to fall back to the message list and the executor's durable record, not the trace. ## Operational Effect The practical consequence is that trace-derived metrics — tool-call success rate, per-tool latency, model-invocation-to-tool-call fan-out — are computed over a population that silently includes indeterminate calls as successes. A tool that times out at the transport layer after acceptance produces a fast, clean, `UNSET` span; the latency histogram records a sub-millisecond "success" and the success counter increments. The agent, meanwhile, either retries (double-executing a non-idempotent side effect) or proceeds with a missing result (reasoning divergence). Neither shows up as an error in the trace, because no error was recorded — the error is the *absence* of a recorded outcome, which the conventions have no attribute for. The guardrail is to make indeterminacy explicit and to make the trace subordinate to the message list. Concretely: set `gen_ai.tool.result` to a sentinel (`""`) on every path where the result is not durably in hand before `End()`, set span status to `ERROR` with `error.type = "tool_result_unavailable"` rather than leaving `UNSET`, and require the replay/audit consumer to reject any episode where a parent `chat` span ends before a child tool span it depends on. The invariant to enforce is: *for every tool call ID referenced by a `chat` span's finish reasons, there exists a tool span that ends before the `chat` span ends and carries either a result or an explicit error.* Violation of this invariant is the detectable form of the false-completion failure. The code below encodes the check as a validator that a replay harness can run before trusting a reconstructed episode. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Optional class SpanLifecycleViolation(RuntimeError): """Raised when a tool span's completion cannot be trusted as a result record.""" @dataclass class Span: span_id: str parent_id: Optional[str] operation: str # "chat" | "execute_tool" end_ns: int tool_call_id: Optional[str] = None tool_result: Optional[str] = None error_type: Optional[str] = None status: str = "UNSET" # "UNSET" | "OK" | "ERROR" finish_reasons: list[str] = field(default_factory=list) def validate_tool_span_completion( chat_span: Span, tool_spans: list[Span], message_list_call_ids: set[str], ) -> None: """Enforce: every tool call referenced by a chat span has a trustworthy result. A tool span is trustworthy only if it ends before its parent chat span, carries an explicit result or error, and the call id appears in the assembled message list the next model invocation will consume. """ if "tool_calls" not in chat_span.finish_reasons: return by_call_id = {s.tool_call_id: s for s in tool_spans if s.parent_id == chat_span.span_id} for call_id in _referenced_call_ids(chat_span, tool_spans): span = by_call_id.get(call_id) if span is None: raise SpanLifecycleViolation( f"chat span {chat_span.span_id} references tool call {call_id} " f"with no child execute_tool span" ) if span.end_ns > chat_span.end_ns: # Result observed after the parent that consumed it: impossible. raise SpanLifecycleViolation( f"tool span {span.span_id} for call {call_id} ends " f"{span.end_ns - chat_span.end_ns}ns after parent chat span " f"{chat_span.span_id}; result cannot have been consumed" ) has_outcome = span.tool_result is not None or span.error_type is not None if not has_outcome and span.status == "UNSET": raise SpanLifecycleViolation( f"tool span {span.span_id} for call {call_id} ended with no " f"result, no error, and UNSET status: indeterminate completion " f"must not be treated as success" ) if call_id not in message_list_call_ids: raise SpanLifecycleViolation( f"tool call {call_id} has a completed span but no corresponding " f"tool-role message; the next chat span will reason over a " f"context missing this result" ) def _referenced_call_ids(chat_span: Span, tool_spans: list[Span]) -> set[str]: return { s.tool_call_id for s in tool_spans if s.parent_id == chat_span.span_id and s.tool_call_id is not None } ```