npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/otel-genai-semconv-core

v0.1.0

Published

OTel GenAI semantic convention constants, types, and span builder

Readme

@reaatech/otel-genai-semconv-core

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Canonical TypeScript types, constants, schemas, and span-builder utilities for the OpenTelemetry GenAI semantic conventions. This package is the single source of truth for all OTel GenAI attribute names, event types, error classifications, and domain models used throughout the @reaatech/otel-genai-semconv-* ecosystem.

Installation

npm install @reaatech/otel-genai-semconv-core
# or
pnpm add @reaatech/otel-genai-semconv-core

Feature Overview

  • 50+ semantic convention constants — every gen_ai.* attribute, event, and metric name defined in one place
  • Complete domain modelLLMRequest, LLMResponse, TokenUsage, CostData, and 20+ supporting interfaces
  • Zod validation schemas — runtime validation for requests, responses, messages, tools, and streaming events
  • Span builder — construct OTel-compliant spans with automatic attribute mapping, message events, and choice events
  • Attribute mapper — provider-agnostic mapping from normalized types to OTel semconv attributes
  • Zero runtime dependencies beyond zod and @opentelemetry/api — lightweight and tree-shakeable
  • Dual ESM/CJS output — works with import and require

Quick Start

import { SpanBuilder, GEN_AI_ATTRIBUTES, type LLMRequest } from "@reaatech/otel-genai-semconv-core";

const builder = new SpanBuilder({ provider: "openai" });

const request: LLMRequest = {
  model: "gpt-4",
  temperature: 0.7,
  maxTokens: 500,
  messages: [{ role: "user", content: "What is OpenTelemetry?" }],
};

const span = builder.startSpan(request);
// span now has gen_ai.request.model, gen_ai.request.temperature, etc.

Exports

Semantic Convention Constants

Every OTel GenAI attribute name defined as a const object with dot-delimited string values:

| Export | Description | |--------|-------------| | GEN_AI_ATTRIBUTES | All request/response/usage attribute names (gen_ai.request.model, etc.) | | COST_ATTRIBUTES | Cost tracking attribute names (llm.cost.total, etc.) | | STREAMING_ATTRIBUTES | Streaming metric attribute names (gen_ai.streaming.time_to_first_token_ms, etc.) | | GEN_AI_EVENTS | Event names (gen_ai.choice, gen_ai.user.message, etc.) | | EVENT_ATTRIBUTES | Event attribute keys (index, finish_reason, content, role) |

Classification Constants

| Export | Description | |--------|-------------| | FINISH_REASONS | Standardized finish reasons: stop, max_tokens, content_filter, tool_calls, length, error | | ERROR_TYPES | Error type classifications: rate_limit, authentication, timeout, server_error, etc. | | OPERATIONS | Operation names: chat, text_completion, embeddings, image_generation | | SPAN_NAMES | Span names per operation: gen_ai.chat.completion, gen_ai.embedding, etc. | | PROVIDER_SYSTEMS | Provider system identifiers: openai, anthropic, gcp.vertex_ai, aws.bedrock |

Metric Constants

| Export | Description | |--------|-------------| | METRIC_NAMES | Metric names: genai.requests.total, genai.request.duration_ms, etc. | | METRIC_ATTRIBUTES | Metric label keys: provider, model, status, error_type | | STATUS_VALUES | Status values: ok, error |

Domain Types

All types are exported with matching Zod schemas for runtime validation:

| Type | Schema | Description | |------|--------|-------------| | LLMRequest | LLMRequestSchema | Normalized request: model, messages, temperature, maxTokens, tools | | LLMResponse | LLMResponseSchema | Normalized response: id, model, choices, usage, finishReasons | | Message | MessageSchema | Conversation message: role, content, toolCalls | | TokenUsage | TokenUsageSchema | Token counts: inputTokens, outputTokens, cachedInputTokens | | CostData | CostDataSchema | Cost breakdown: total, input, output, currency | | Tool | ToolSchema | Function definition for tool calling | | InstrumentationConfig | InstrumentationConfigSchema | Configuration: captureHeaders, trackCosts, pricing, hooks | | StreamingEvent | StreamingEventSchema | Streaming event: chunk, complete, error |

Additional types: ProviderType, ModelInfo, PricingInfo, ContentBlock, ToolChoice, ToolCall, ResponseFormat, Choice, SpanContext, GenAISpanAttributes.

Attribute Mapper

import { AttributeMapper, createAttributeMapper } from "@reaatech/otel-genai-semconv-core";

const mapper = createAttributeMapper("openai");

// Map a normalized request to OTel attributes
const requestAttrs = mapper.mapRequestAttributes(request);
// → { "gen_ai.request.model": "gpt-4", "gen_ai.request.temperature": 0.7, ... }

// Map a normalized response
const responseAttrs = mapper.mapResponseAttributes(response);
// → { "gen_ai.response.model": "gpt-4-0613", "gen_ai.response.finish_reasons": ["stop"] }

// Map finish reasons across providers
const reason = mapper.mapFinishReason("end_turn");       // → "stop" (Anthropic → OTel)
const reason2 = mapper.mapFinishReason("safety");         // → "content_filter" (Vertex → OTel)

Span Builder

import { SpanBuilder } from "@reaatech/otel-genai-semconv-core";

const builder = new SpanBuilder({
  provider: "openai",
  addMessageEvents: true,
  addChoiceEvents: true,
});

const span = builder.startSpan(request, "gpt-4 chat");

// After receiving a response:
builder.addResponse(response);          // sets response + usage attributes, adds choice events
builder.addCostAttributes(costData);    // sets llm.cost.* attributes
builder.addStreamingAttributes({        // sets gen_ai.streaming.* attributes
  timeToFirstTokenMs: 245,
  totalDurationMs: 1200,
  chunkCount: 42,
});

// On error:
builder.recordError(new Error("rate limit"));  // sets status + error attributes + exception
// On success:
builder.setOk();
builder.endSpan();

Usage Patterns

Zod Validation at the Boundary

import { LLMRequestSchema, LLMResponseSchema } from "@reaatech/otel-genai-semconv-core";

function handleRequest(raw: unknown): LLMRequest {
  return LLMRequestSchema.parse(raw); // throws ZodError on invalid input
}

Custom Span Names

const span = builder.startSpan(request, "my-custom-operation");
// Overrides the default span name derived from the model

Related Packages

License

MIT