@arizeai/openinference-instrumentation-openai 3.2.1 → 3.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arizeai/openinference-instrumentation-openai",
3
- "version": "3.2.1",
3
+ "version": "3.2.3",
4
4
  "description": "OpenInference instrumentation for OpenAI",
5
5
  "private": false,
6
6
  "main": "dist/src/index.js",
@@ -21,8 +21,8 @@
21
21
  "@opentelemetry/api": "^1.9.0",
22
22
  "@opentelemetry/core": "^1.25.1",
23
23
  "@opentelemetry/instrumentation": "^0.46.0",
24
- "@arizeai/openinference-core": "1.0.5",
25
- "@arizeai/openinference-semantic-conventions": "2.1.1"
24
+ "@arizeai/openinference-core": "1.0.7",
25
+ "@arizeai/openinference-semantic-conventions": "2.1.2"
26
26
  },
27
27
  "keywords": [],
28
28
  "files": [
@@ -37,7 +37,7 @@
37
37
  "@opentelemetry/sdk-trace-base": "^1.25.1",
38
38
  "@opentelemetry/sdk-trace-node": "^1.25.1",
39
39
  "@opentelemetry/semantic-conventions": "^1.25.1",
40
- "jest": "^29.7.0",
40
+ "vitest": "^2.1.8",
41
41
  "openai": "^5.7.0",
42
42
  "zod": "^3.24.3"
43
43
  },
@@ -47,6 +47,6 @@
47
47
  "postbuild": "echo '{\"type\": \"module\"}' > ./dist/esm/package.json && rimraf dist/test",
48
48
  "version:update": "../../scripts/version-update.js",
49
49
  "type:check": "tsc --noEmit",
50
- "test": "jest ."
50
+ "test": "vitest"
51
51
  }
52
52
  }
@@ -94,6 +94,69 @@ function getExecContext(span: Span) {
94
94
  }
95
95
  return execContext;
96
96
  }
97
+
98
+ /**
99
+ * Gets the appropriate LLM provider based on the OpenAI client instance
100
+ * Follows the same logic as the Python implementation by checking the baseURL host
101
+ * @param clientInstance The OpenAI client instance
102
+ * @returns LLMProvider.AZURE for Azure OpenAI, LLMProvider.OPENAI for regular OpenAI
103
+ */
104
+ function getLLMProvider(clientInstance: unknown): LLMProvider {
105
+ try {
106
+ // The clientInstance might be a sub-object (like Completions) that has a _client property
107
+ // pointing to the actual OpenAI/AzureOpenAI client
108
+ const instance = clientInstance as {
109
+ baseURL?: string | { host?: string };
110
+ _client?: {
111
+ baseURL?: string | { host?: string };
112
+ };
113
+ };
114
+
115
+ let host: string | undefined;
116
+ let baseURL: string | { host?: string } | undefined;
117
+
118
+ // First try to get baseURL from the instance itself
119
+ if (instance.baseURL) {
120
+ baseURL = instance.baseURL;
121
+ }
122
+ // If not found, try the _client property (this is where Azure OpenAI stores it)
123
+ else if (instance._client?.baseURL) {
124
+ baseURL = instance._client.baseURL;
125
+ }
126
+
127
+ if (typeof baseURL === "string") {
128
+ // Extract host from URL string
129
+ try {
130
+ const url = new URL(baseURL);
131
+ host = url.hostname;
132
+ } catch {
133
+ // If URL parsing fails, fallback to string matching
134
+ host = baseURL;
135
+ }
136
+ } else if (baseURL && typeof baseURL === "object" && "host" in baseURL) {
137
+ // Direct host property
138
+ host = baseURL.host;
139
+ }
140
+
141
+ if (host && typeof host === "string") {
142
+ // Follow the same pattern as Python implementation
143
+ if (host.includes("api.openai.com")) {
144
+ return LLMProvider.OPENAI;
145
+ } else if (host.includes("openai.azure.com")) {
146
+ return LLMProvider.AZURE;
147
+ } else if (host.includes("api.microsoft.com")) {
148
+ // Additional Azure endpoint pattern
149
+ return LLMProvider.AZURE;
150
+ }
151
+ }
152
+ } catch (error) {
153
+ // If we can't determine, default to regular OpenAI
154
+ diag.debug("Failed to determine LLM provider from instance", error);
155
+ }
156
+
157
+ // Default to OpenAI if we can't determine
158
+ return LLMProvider.OPENAI;
159
+ }
97
160
  /**
98
161
  * An auto instrumentation class for OpenAI that creates {@link https://github.com/Arize-ai/openinference/blob/main/spec/semantic_conventions.md|OpenInference} Compliant spans for the OpenAI API
99
162
  * @param instrumentationConfig The config for the instrumentation @see {@link InstrumentationConfig}
@@ -221,7 +284,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
221
284
  [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
222
285
  JSON.stringify(invocationParameters),
223
286
  [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
224
- [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
287
+ [SemanticConventions.LLM_PROVIDER]: getLLMProvider(this),
225
288
  ...getLLMInputMessagesAttributes(body),
226
289
  ...getLLMToolsJSONSchema(body),
227
290
  },
@@ -310,7 +373,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
310
373
  [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
311
374
  JSON.stringify(invocationParameters),
312
375
  [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
313
- [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
376
+ [SemanticConventions.LLM_PROVIDER]: getLLMProvider(this),
314
377
  ...getCompletionInputValueAndMimeType(body),
315
378
  },
316
379
  },
@@ -389,6 +452,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
389
452
  [SemanticConventions.INPUT_MIME_TYPE]: isStringInput
390
453
  ? MimeType.TEXT
391
454
  : MimeType.JSON,
455
+ [SemanticConventions.LLM_PROVIDER]: getLLMProvider(this),
392
456
  ...getEmbeddingTextAttributes(body),
393
457
  },
394
458
  });
@@ -461,7 +525,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
461
525
  [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
462
526
  JSON.stringify(invocationParameters),
463
527
  [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
464
- [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
528
+ [SemanticConventions.LLM_PROVIDER]: getLLMProvider(this),
465
529
  ...getResponsesInputMessagesAttributes(body),
466
530
  ...getLLMToolsJSONSchema(body),
467
531
  },
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // this is an auto-generated file. see js/scripts/version-update.js
2
- export const VERSION = "3.2.1";
2
+ export const VERSION = "3.2.3";