JavaScript APIopeninference-genai

Core

Instrumentation

Integrations

JavaScript · Integration

@arizeai/openinference-genai

OpenInference utilities for converting OpenTelemetry GenAI span attributes to OpenInference span attributes

Version
0.4.4
Peer dependencies
@opentelemetry/api >=1.9.0, @opentelemetry/semantic-conventions >=1.41.1

Install

npm install @arizeai/openinference-genai

@arizeai/openinference-genai

convertGenAISpanAttributesToOpenInferenceSpanAttributes

constantSource ↗
const convertGenAISpanAttributesToOpenInferenceSpanAttributes: SafeFunction<(spanAttributes: Attributes) => Attributes>

@arizeai/openinference-genai/attributes

GenAIInputMessage

type aliasSource ↗
type GenAIInputMessage = ChatMessage

GenAIInputMessagePart

type aliasSource ↗
type GenAIInputMessagePart = ChatMessage["parts"][number]

GenAIOutputMessage

type aliasSource ↗
type GenAIOutputMessage = OutputMessage & { text?: string }

GenAIOutputMessagePart

type aliasSource ↗
type GenAIOutputMessagePart = OutputMessage["parts"][number]

convertGenAISpanAttributesToOpenInferenceSpanAttributes

functionSource ↗
function convertGenAISpanAttributesToOpenInferenceSpanAttributes(
  spanAttributes: Attributes,
): Attributes

Convert GenAI span attributes to OpenInference span attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing GenAI span attributes to convert

Returns

TypeDescription
Attributes

The converted OpenInference span attributes

mapProviderAndSystem

functionSource ↗
function mapProviderAndSystem(spanAttributes: Attributes): Attributes

Map provider and system to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing provider and system to map

TODO: add some heuristics that can map incoming provider names to the correct OpenInference provider name

Returns

TypeDescription
Attributes

The mapped provider and system attributes

mapAgentAttributes

functionSource ↗
function mapAgentAttributes(spanAttributes: Attributes): Attributes

Map GenAI agent attributes to OpenInference agent attributes.

Currently only gen_ai.agent.name -> agent.name. gen_ai.agent.id and gen_ai.agent.description (which also drive AGENT span-kind detection in mapSpanKind) are intentionally not mapped, as OpenInference has no corresponding agent attributes for them.

Parameters

NameTypeDescription
spanAttributesAttributes

The GenAI span attributes to read agent attributes from

Returns

TypeDescription
Attributes

The mapped OpenInference agent attributes

mapConversationId

functionSource ↗
function mapConversationId(spanAttributes: Attributes): Attributes

Map the GenAI conversation id to the OpenInference session id.

gen_ai.conversation.id identifies the conversation (session or thread) a span belongs to, which is what session.id represents in OpenInference. An explicit session.id takes precedence.

Parameters

NameTypeDescription
spanAttributesAttributes

The GenAI span attributes to read the conversation id from

Returns

TypeDescription
Attributes

The mapped OpenInference session attributes

mapModels

functionSource ↗
function mapModels(spanAttributes: Attributes): Attributes

Map model name to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing model name to map

Returns

TypeDescription
Attributes

The mapped model name attributes

mapFinishReason

functionSource ↗
function mapFinishReason(spanAttributes: Attributes): Attributes

Map GenAI response finish reasons to the OpenInference LLM finish reason attribute.

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing the finish reasons to map

Returns

TypeDescription
Attributes

The mapped finish reason attribute

mapSpanKind

functionSource ↗
function mapSpanKind(spanAttributes: Attributes): Attributes

Map span kind to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing span kind to map

Returns

TypeDescription
Attributes

The mapped span kind attributes

mapInvocationParameters

functionSource ↗
function mapInvocationParameters(spanAttributes: Attributes): Attributes

Map invocation parameters to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing invocation parameters to map

Returns

TypeDescription
Attributes

The mapped invocation parameters attributes

mapInputValue

functionSource ↗
function mapInputValue(spanAttributes: Attributes): Attributes

Map input value to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing input value to map

Returns

TypeDescription
Attributes

The mapped input value attributes

mapOutputValue

functionSource ↗
function mapOutputValue(spanAttributes: Attributes): Attributes

Map output value to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing output value to map

Returns

TypeDescription
Attributes

The mapped output value attributes

mapAgent

functionSource ↗
function mapAgent(spanAttributes: Attributes): Attributes

Map GenAI agent attributes to OpenInference attributes.

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing agent details

Returns

TypeDescription
Attributes

The mapped agent attributes

mapSystemInstructions

functionSource ↗
function mapSystemInstructions(spanAttributes: Attributes): Attributes

Map GenAI system instructions into metadata for preservation.

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing system instructions

Returns

TypeDescription
Attributes

The mapped metadata attributes

mapToolDefinitions

functionSource ↗
function mapToolDefinitions(spanAttributes: Attributes): Attributes

Map GenAI tool definitions to OpenInference LLM tool schema attributes.

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing tool definitions

Returns

TypeDescription
Attributes

The mapped tool schema attributes

mapInputMessages

functionSource ↗
function mapInputMessages(spanAttributes: Attributes): Attributes

Map input messages to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing input messages to map

Returns

TypeDescription
Attributes

The mapped input messages attributes

mapOutputMessages

functionSource ↗
function mapOutputMessages(spanAttributes: Attributes): Attributes

Map output messages to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing output messages to map

Returns

TypeDescription
Attributes

The mapped output messages attributes

mapTokenCounts

functionSource ↗
function mapTokenCounts(spanAttributes: Attributes): Attributes

Map usage token counts to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing usage token counts to map

Returns

TypeDescription
Attributes

The mapped usage token counts attributes

mapToolExecution

functionSource ↗
function mapToolExecution(spanAttributes: Attributes): Attributes

Map tool execution to openinference attributes

Parameters

NameTypeDescription
spanAttributesAttributes

The span attributes containing tool execution to map

Returns

TypeDescription
Attributes

The mapped tool execution attributes

@arizeai/openinference-genai/utils

safelyJSONStringify

functionSource ↗
function safelyJSONStringify(value: unknown): string | null

getNumber

functionSource ↗
function getNumber(value: unknown): number | undefined

getString

functionSource ↗
function getString(value: unknown): string | undefined

getStringArray

functionSource ↗
function getStringArray(value: unknown): string[] | undefined

safelyParseJSON

functionSource ↗
function safelyParseJSON(value: unknown): unknown

getMimeType

functionSource ↗
function getMimeType(value: unknown): MimeType

set

functionSource ↗
function set(
  attrs: Attributes,
  key: string,
  value?: AttributeValue | null,
): void

Assign attribute value to attributes object if value is not undefined or null This mutates the attrs object

Parameters

NameTypeDescription
attrsAttributes

The attributes object to assign the value to

keystring

The key to assign the value to

value?AttributeValue | null

The value to assign to the key

merge

functionSource ↗
function merge(...groups: Attributes[]): Attributes

Merge multiple attributes objects into a single attributes object This mutates the first attributes object

Parameters

NameTypeDescription
...groupsAttributes[]

The groups of attributes to merge

Returns

TypeDescription
Attributes

The merged attributes

toStringContent

functionSource ↗
function toStringContent(value: unknown): string

Convert a value to a string. If the value is already a string, return it. If the value can be jsonified, jsonify it. Otherwise, return the string representation of the value.

Parameters

NameTypeDescription
valueunknown

The value to convert to a string

Returns

TypeDescription
string

The string representation of the value

withSafety

functionSource ↗
function withSafety<T extends GenericFunction>(
  fn: { fn: T; onError?: (error: unknown) => void },
): SafeFunction<T>

Wraps a function with a try-catch block to catch and log any errors.

Parameters

NameTypeDescription
fn{ fn: T; onError?: (error: unknown) => void }

A function to wrap with a try-catch block.

fn.fnT
fn.onError?(error: unknown) => void

Returns

TypeDescription
SafeFunction<T>

A function that returns null if an error is thrown.

@arizeai/openinference-genai/types

Mutable

type aliasSource ↗
type Mutable<T> = { -readonly [P in keyof T]: T[P] }

DeeplyMutable

type aliasSource ↗
type DeeplyMutable<T> = { -readonly [P in keyof T]: T[P] extends object ? DeeplyMutable<T[P]> : T[P] }

GenericFunction

type aliasSource ↗
type GenericFunction = (...args: any[]) => any

SafeFunction

type aliasSource ↗
type SafeFunction<T extends GenericFunction> = (...args: Parameters<T>) => ReturnType<T> | null