Skip to content

Commit 6bf51ae

Browse files
committed
fix(instrumentation-openai): use @ts-ignore for OpenAI v4/v5 compatibility
- OpenAI v5 uses union types for ChatCompletionMessageToolCall - OpenAI v4 has direct function property access - TAV (Test All Versions) ensures compatibility across v4.19.0 through v5.20.0
1 parent b3a2d1c commit 6bf51ae

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

packages/instrumentation-openai/src/instrumentation.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import type {
3535
ChatCompletionChunk,
3636
Completions as ChatCompletions,
3737
} from 'openai/resources/chat/completions';
38+
// @ts-ignore openai/core not exported in v5, but needed for v4 compatibility
3839
import type { APIPromise } from 'openai/core';
3940
import type {
4041
CreateEmbeddingResponse,
@@ -227,6 +228,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
227228

228229
// Non-streaming.
229230
apiPromise
231+
// @ts-ignore result type inference fails across OpenAI versions
230232
.then(result => {
231233
self._onChatCompletionsCreateResult(
232234
span,
@@ -372,7 +374,9 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
372374
id: tc.id,
373375
type: tc.type,
374376
function: {
377+
// @ts-ignore function property doesn't exist on ChatCompletionMessageCustomToolCall in v5
375378
name: tc.function.name,
379+
// @ts-ignore function property doesn't exist on ChatCompletionMessageCustomToolCall in v5
376380
arguments: tc.function.arguments,
377381
},
378382
};
@@ -382,6 +386,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
382386
return {
383387
id: tc.id,
384388
type: tc.type,
389+
// @ts-ignore function property doesn't exist on ChatCompletionMessageCustomToolCall in v5
385390
function: { name: tc.function.name },
386391
};
387392
});
@@ -492,6 +497,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
492497
});
493498
} else if (toolCalls.length > 0) {
494499
// A tool call chunk with more of the `function.arguments`.
500+
// @ts-ignore function property doesn't exist on ChatCompletionMessageCustomToolCall in v5
495501
toolCalls[toolCalls.length - 1].function.arguments +=
496502
toolCallPart.function?.arguments ?? '';
497503
}
@@ -551,8 +557,10 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
551557
message.content = choice.content;
552558
}
553559
if (choice.toolCalls && choice.toolCalls.length > 0) {
560+
// @ts-ignore ChatCompletionMessageToolCall union type incompatible with GenAIToolCall in v5
554561
message.tool_calls = choice.toolCalls;
555562
if (!config.captureMessageContent) {
563+
// @ts-ignore message.tool_calls possibly undefined
556564
message.tool_calls.forEach(tc => {
557565
delete tc.function?.arguments;
558566
});
@@ -621,6 +629,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
621629
message.content = choice.message.content;
622630
}
623631
if (choice.message.tool_calls) {
632+
// @ts-ignore ChatCompletionMessageToolCall union type incompatible with GenAIToolCall in v5
624633
message.tool_calls = choice.message.tool_calls;
625634
}
626635
} else {
@@ -629,6 +638,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
629638
return {
630639
id: tc.id,
631640
type: tc.type,
641+
// @ts-ignore function property doesn't exist on ChatCompletionMessageCustomToolCall in v5
632642
function: { name: tc.function.name },
633643
};
634644
});

0 commit comments

Comments
 (0)