|
| 1 | +# OpenInference GenAI |
| 2 | + |
| 3 | +[](https://badge.fury.io/js/@arizeai%2Fopeninference-genai) |
| 4 | + |
| 5 | +This package provides a set of utilities to convert [OpenTelemetry GenAI](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation/opentelemetry-instrumentation-genai) span attributes to OpenInference span attributes. |
| 6 | + |
| 7 | +> [!WARNING] |
| 8 | +> The OpenTelemetry GenAI conventions are still incubating, and may include breaking changes at any time. |
| 9 | +> This package will attempt best effort conversions of a subset of the OpenTelemetry GenAI attributes to OpenInference attributes. |
| 10 | +> Currently, attributes reflect their definition as of October 2025. |
| 11 | +
|
| 12 | +## Installation |
| 13 | + |
| 14 | +```shell |
| 15 | +npm install --save @arizeai/openinference-genai |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +`@arizeai/openinference-genai` can be used as a standalone set of helper functions, |
| 21 | +or in conjunction with a SpanProcessor in order to automatically convert OpenTelemetry GenAI spans to OpenInference spans. |
| 22 | + |
| 23 | +### Standalone |
| 24 | + |
| 25 | +You can mutate the span attributes in place by using the standalone helper functions. |
| 26 | + |
| 27 | +> [!IMPORTANT] |
| 28 | +> Span mutation is not supported by the OpenTelemetry SDK, so ensure that you are |
| 29 | +> performing mutations in the last-mile of the span's lifetime (i.e. just before exporting the span in a SpanProcessor). |
| 30 | +
|
| 31 | +```ts |
| 32 | +import { convertGenAISpanAttributesToOpenInferenceSpanAttributes } from `@arizeai/openinference-genai` |
| 33 | + |
| 34 | +// obtain a span with OpenTelemetry GenAI attributes from your tracing system |
| 35 | +const span: ReadableSpan = {/* ... */} |
| 36 | + |
| 37 | +// convert the span attributes to OpenInference attributes |
| 38 | +const openinferenceAttributes = convertGenAISpanAttributesToOpenInferenceSpanAttributes(span.attributes) |
| 39 | + |
| 40 | +// add the OpenInference attributes to the span |
| 41 | +span.attributes = {...span.attributes, ...openinferenceAttributes} |
| 42 | +``` |
| 43 | + |
| 44 | +### SpanProcessor |
| 45 | + |
| 46 | +You can use the a custom TraceExporter to automatically convert OpenTelemetry GenAI spans to OpenInference spans. |
| 47 | + |
| 48 | +See [examples/export-spans.ts](./examples/export-spans.ts) for a runnable version of the following sample code. |
| 49 | + |
| 50 | +Start by installing packages |
| 51 | + |
| 52 | +```shell |
| 53 | +pnpm add @opentelemetry/api @opentelemetry/core @opentelemetry/exporter-trace-otlp-proto @opentelemetry/sdk-trace-base @opentelemetry/sdk-trace-node @opentelemetry/semantic-conventions @opentelemetry/resources @arizeai/openinference-genai |
| 54 | +``` |
| 55 | + |
| 56 | +Create a custom TraceExporter that converts the OpenTelemetry GenAI attributes to OpenInference attributes. |
| 57 | + |
| 58 | +```ts |
| 59 | +// openinferenceOTLPTraceExporter.ts |
| 60 | +import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; |
| 61 | +import type { ReadableSpan } from "@opentelemetry/sdk-trace-base"; |
| 62 | +import type { ExportResult } from "@opentelemetry/core"; |
| 63 | + |
| 64 | +import { convertGenAISpanAttributesToOpenInferenceSpanAttributes } from "@arizeai/openinference-genai"; |
| 65 | +import type { Mutable } from "@arizeai/openinference-genai/types"; |
| 66 | + |
| 67 | +class OpenInferenceOTLPTraceExporter extends OTLPTraceExporter { |
| 68 | + export( |
| 69 | + spans: ReadableSpan[], |
| 70 | + resultCallback: (result: ExportResult) => void, |
| 71 | + ) { |
| 72 | + const processedSpans = spans.map((span) => { |
| 73 | + const processedAttributes = |
| 74 | + convertGenAISpanAttributesToOpenInferenceSpanAttributes( |
| 75 | + span.attributes, |
| 76 | + ); |
| 77 | + // optionally you can replace the entire attributes object with the |
| 78 | + // processed attributes if you want _only_ the OpenInference attributes |
| 79 | + (span as Mutable<ReadableSpan>).attributes = { |
| 80 | + ...span.attributes, |
| 81 | + ...processedAttributes, |
| 82 | + }; |
| 83 | + return span; |
| 84 | + }); |
| 85 | + |
| 86 | + super.export(processedSpans, resultCallback); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +And then use it in the SpanProcessor of your choice. |
| 92 | + |
| 93 | +```ts |
| 94 | +// instrumentation.ts |
| 95 | +import { resourceFromAttributes } from "@opentelemetry/resources"; |
| 96 | +import { |
| 97 | + NodeTracerProvider, |
| 98 | + BatchSpanProcessor, |
| 99 | +} from "@opentelemetry/sdk-trace-node"; |
| 100 | +import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; |
| 101 | + |
| 102 | +import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions"; |
| 103 | + |
| 104 | +import { OpenInferenceOTLPTraceExporter } from "./openinferenceOTLPTraceExporter"; |
| 105 | + |
| 106 | +const COLLECTOR_ENDPOINT = process.env.COLLECTOR_ENDPOINT; |
| 107 | +const SERVICE_NAME = "openinference-genai-app"; |
| 108 | + |
| 109 | +export const provider = new NodeTracerProvider({ |
| 110 | + resource: resourceFromAttributes({ |
| 111 | + [ATTR_SERVICE_NAME]: SERVICE_NAME, |
| 112 | + [SEMRESATTRS_PROJECT_NAME]: SERVICE_NAME, |
| 113 | + }), |
| 114 | + spanProcessors: [ |
| 115 | + new BatchSpanProcessor( |
| 116 | + new OpenInferenceOTLPTraceExporter({ |
| 117 | + url: `${COLLECTOR_ENDPOINT}/v1/traces`, |
| 118 | + }), |
| 119 | + ), |
| 120 | + ], |
| 121 | +}); |
| 122 | + |
| 123 | +provider.register(); |
| 124 | +``` |
| 125 | + |
| 126 | +## Examples |
| 127 | + |
| 128 | +See the [examples](./examples) directory in this package for more executable examples. |
| 129 | + |
| 130 | +To execute an example, run the following commands: |
| 131 | + |
| 132 | +```shell |
| 133 | +cd js/packages/openinference-genai |
| 134 | +pnpm install |
| 135 | +pnpm -r build |
| 136 | +pnpx tsx examples/export-spans.ts |
| 137 | +``` |
0 commit comments