# @arizeai/openinference-genai

**JavaScript · Integration**

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`

[npm](https://www.npmjs.com/package/@arizeai/openinference-genai) · [README](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-genai)

## Install

```sh
npm install @arizeai/openinference-genai
```

## `@arizeai/openinference-genai`

### `convertGenAISpanAttributesToOpenInferenceSpanAttributes`

_constant_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/index.ts#L4)

```typescript
const convertGenAISpanAttributesToOpenInferenceSpanAttributes: SafeFunction<(spanAttributes: Attributes) => Attributes>
```

## `@arizeai/openinference-genai/attributes`

### `GenAIInputMessage`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L60)

```typescript
type GenAIInputMessage = ChatMessage
```

### `GenAIInputMessagePart`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L61)

```typescript
type GenAIInputMessagePart = ChatMessage["parts"][number]
```

### `GenAIOutputMessage`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L62)

```typescript
type GenAIOutputMessage = OutputMessage & { text?: string }
```

### `GenAIOutputMessagePart`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L66)

```typescript
type GenAIOutputMessagePart = OutputMessage["parts"][number]
```

### `convertGenAISpanAttributesToOpenInferenceSpanAttributes`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L343)

```typescript
function convertGenAISpanAttributesToOpenInferenceSpanAttributes(
  spanAttributes: Attributes,
): Attributes
```

Convert GenAI span attributes to OpenInference span attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing GenAI span attributes to convert |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The converted OpenInference span attributes |

### `mapProviderAndSystem`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L372)

```typescript
function mapProviderAndSystem(spanAttributes: Attributes): Attributes
```

Map provider and system to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | 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**

| Type | Description |
| - | - |
| `Attributes` | The mapped provider and system attributes |

### `mapAgentAttributes`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L390)

```typescript
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**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The GenAI span attributes to read agent attributes from |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped OpenInference agent attributes |

### `mapConversationId`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L406)

```typescript
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**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The GenAI span attributes to read the conversation id from |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped OpenInference session attributes |

### `mapModels`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L419)

```typescript
function mapModels(spanAttributes: Attributes): Attributes
```

Map model name to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing model name to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped model name attributes |

### `mapFinishReason`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L433)

```typescript
function mapFinishReason(spanAttributes: Attributes): Attributes
```

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

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing the finish reasons to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped finish reason attribute |

### `mapSpanKind`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L448)

```typescript
function mapSpanKind(spanAttributes: Attributes): Attributes
```

Map span kind to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing span kind to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped span kind attributes |

### `mapInvocationParameters`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L488)

```typescript
function mapInvocationParameters(spanAttributes: Attributes): Attributes
```

Map invocation parameters to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing invocation parameters to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped invocation parameters attributes |

### `mapInputValue`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L526)

```typescript
function mapInputValue(spanAttributes: Attributes): Attributes
```

Map input value to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing input value to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped input value attributes |

### `mapOutputValue`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L549)

```typescript
function mapOutputValue(spanAttributes: Attributes): Attributes
```

Map output value to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing output value to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped output value attributes |

### `mapAgent`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L572)

```typescript
function mapAgent(spanAttributes: Attributes): Attributes
```

Map GenAI agent attributes to OpenInference attributes.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing agent details |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped agent attributes |

### `mapSystemInstructions`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L583)

```typescript
function mapSystemInstructions(spanAttributes: Attributes): Attributes
```

Map GenAI system instructions into metadata for preservation.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing system instructions |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped metadata attributes |

### `mapToolDefinitions`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L608)

```typescript
function mapToolDefinitions(spanAttributes: Attributes): Attributes
```

Map GenAI tool definitions to OpenInference LLM tool schema attributes.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing tool definitions |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped tool schema attributes |

### `mapInputMessages`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L629)

```typescript
function mapInputMessages(spanAttributes: Attributes): Attributes
```

Map input messages to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing input messages to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped input messages attributes |

### `mapOutputMessages`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L669)

```typescript
function mapOutputMessages(spanAttributes: Attributes): Attributes
```

Map output messages to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing output messages to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped output messages attributes |

### `mapTokenCounts`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L693)

```typescript
function mapTokenCounts(spanAttributes: Attributes): Attributes
```

Map usage token counts to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing usage token counts to map |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped usage token counts attributes |

### `mapToolExecution`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/attributes.ts#L729)

```typescript
function mapToolExecution(spanAttributes: Attributes): Attributes
```

Map tool execution to openinference attributes

**Parameters**

| Name | Type | Description |
| - | - | - |
| `spanAttributes` | `Attributes` | The span attributes containing tool execution to map |

> **See also**
>
> <https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#execute-tool-span>

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The mapped tool execution attributes |

## `@arizeai/openinference-genai/utils`

### `safelyJSONStringify`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L7)

```typescript
function safelyJSONStringify(value: unknown): string | null
```

### `getNumber`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L15)

```typescript
function getNumber(value: unknown): number | undefined
```

### `getString`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L20)

```typescript
function getString(value: unknown): string | undefined
```

### `getStringArray`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L25)

```typescript
function getStringArray(value: unknown): string[] | undefined
```

### `safelyParseJSON`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L32)

```typescript
function safelyParseJSON(value: unknown): unknown
```

### `getMimeType`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L42)

```typescript
function getMimeType(value: unknown): MimeType
```

### `set`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L54)

```typescript
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**

| Name | Type | Description |
| - | - | - |
| `attrs` | `Attributes` | The attributes object to assign the value to |
| `key` | `string` | The key to assign the value to |
| `value?` | `AttributeValue \| null` | The value to assign to the key |

### `merge`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L65)

```typescript
function merge(...groups: Attributes[]): Attributes
```

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

**Parameters**

| Name | Type | Description |
| - | - | - |
| `...groups` | `Attributes[]` | The groups of attributes to merge |

**Returns**

| Type | Description |
| - | - |
| `Attributes` | The merged attributes |

### `toStringContent`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L75)

```typescript
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**

| Name | Type | Description |
| - | - | - |
| `value` | `unknown` | The value to convert to a string |

**Returns**

| Type | Description |
| - | - |
| `string` | The string representation of the value |

### `withSafety`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/utils.ts#L87)

```typescript
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**

| Name | Type | Description |
| - | - | - |
| `fn` | `{ fn: T; onError?: (error: unknown) => void }` | A function to wrap with a try-catch block. |
| `fn.fn` | `T` | |
| `fn.onError?` | `(error: unknown) => void` | |

**Returns**

| Type | Description |
| - | - |
| `SafeFunction<T>` | A function that returns null if an error is thrown. |

## `@arizeai/openinference-genai/types`

### `Mutable`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/types.ts#L1)

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

### `DeeplyMutable`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/types.ts#L5)

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

### `GenericFunction`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/types.ts#L10)

```typescript
type GenericFunction = (...args: any[]) => any
```

### `SafeFunction`

_type alias_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-genai/src/types.ts#L12)

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