@arizeai/openinference-instrumentation-openai 1.0.0 → 1.1.0

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.
@@ -175,13 +175,12 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
175
175
  },
176
176
  },
177
177
  );
178
-
179
178
  const execContext = getExecContext(span);
180
179
  const execPromise = safeExecuteInTheMiddle<
181
180
  ReturnType<ChatCompletionCreateType>
182
181
  >(
183
182
  () => {
184
- return context.with(execContext, () => {
183
+ return context.with(trace.setSpan(execContext, span), () => {
185
184
  return original.apply(this, args);
186
185
  });
187
186
  },
@@ -263,7 +262,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
263
262
  ReturnType<CompletionsCreateType>
264
263
  >(
265
264
  () => {
266
- return context.with(execContext, () => {
265
+ return context.with(trace.setSpan(execContext, span), () => {
267
266
  return original.apply(this, args);
268
267
  });
269
268
  },
@@ -335,7 +334,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
335
334
  ReturnType<EmbeddingsCreateType>
336
335
  >(
337
336
  () => {
338
- return context.with(execContext, () => {
337
+ return context.with(trace.setSpan(execContext, span), () => {
339
338
  return original.apply(this, args);
340
339
  });
341
340
  },
@@ -503,15 +502,21 @@ function getChatCompletionInputMessageAttributes(
503
502
  case "assistant":
504
503
  if (message.tool_calls) {
505
504
  message.tool_calls.forEach((toolCall, index) => {
505
+ const toolCallIndexPrefix = `${SemanticConventions.MESSAGE_TOOL_CALLS}.${index}.`;
506
+
507
+ // Add the tool call id if it exists
508
+ if (toolCall.id) {
509
+ attributes[
510
+ `${toolCallIndexPrefix}${SemanticConventions.TOOL_CALL_ID}`
511
+ ] = toolCall.id;
512
+ }
506
513
  // Make sure the tool call has a function
507
514
  if (toolCall.function) {
508
- const toolCallIndexPrefix = `${SemanticConventions.MESSAGE_TOOL_CALLS}.${index}.`;
509
515
  attributes[
510
- toolCallIndexPrefix + SemanticConventions.TOOL_CALL_FUNCTION_NAME
516
+ `${toolCallIndexPrefix}${SemanticConventions.TOOL_CALL_FUNCTION_NAME}`
511
517
  ] = toolCall.function.name;
512
518
  attributes[
513
- toolCallIndexPrefix +
514
- SemanticConventions.TOOL_CALL_FUNCTION_ARGUMENTS_JSON
519
+ `${toolCallIndexPrefix}${SemanticConventions.TOOL_CALL_FUNCTION_ARGUMENTS_JSON}`
515
520
  ] = toolCall.function.arguments;
516
521
  }
517
522
  });
@@ -521,8 +526,10 @@ function getChatCompletionInputMessageAttributes(
521
526
  attributes[SemanticConventions.MESSAGE_FUNCTION_CALL_NAME] = message.name;
522
527
  break;
523
528
  case "tool":
524
- // There's nothing to add for the tool. There is a tool_id, but there are no
525
- // semantic conventions for it
529
+ if (message.tool_call_id) {
530
+ attributes[`${SemanticConventions.MESSAGE_TOOL_CALL_ID}`] =
531
+ message.tool_call_id;
532
+ }
526
533
  break;
527
534
  case "system":
528
535
  // There's nothing to add for the system. Content is captured above
@@ -613,6 +620,12 @@ function getChatCompletionOutputMessageAttributes(
613
620
  if (message.tool_calls) {
614
621
  message.tool_calls.forEach((toolCall, index) => {
615
622
  const toolCallIndexPrefix = `${SemanticConventions.MESSAGE_TOOL_CALLS}.${index}.`;
623
+ // Add the tool call id if it exists
624
+ if (toolCall.id) {
625
+ attributes[
626
+ `${toolCallIndexPrefix}${SemanticConventions.TOOL_CALL_ID}`
627
+ ] = toolCall.id;
628
+ }
616
629
  // Double check that the tool call has a function
617
630
  // NB: OpenAI only supports tool calls with functions right now but this may change
618
631
  if (toolCall.function) {
@@ -759,6 +772,12 @@ function getToolAndFunctionCallAttributesFromStreamChunk(
759
772
  if (choice.delta.tool_calls) {
760
773
  choice.delta.tool_calls.forEach((toolCall, index) => {
761
774
  const toolCallIndexPrefix = `${SemanticConventions.MESSAGE_TOOL_CALLS}.${index}.`;
775
+ // Add the tool call id if it exists
776
+ if (toolCall.id) {
777
+ attributes[
778
+ `${toolCallIndexPrefix}${SemanticConventions.TOOL_CALL_ID}`
779
+ ] = toolCall.id;
780
+ }
762
781
  // Double check that the tool call has a function
763
782
  // NB: OpenAI only supports tool calls with functions right now but this may change
764
783
  if (toolCall.function) {
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 = "1.0.0";
2
+ export const VERSION = "1.1.0";