Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix: openai typescript fixes (#938)
Aman Agarwalcd1bd553f368
diff --git a/sdk/typescript/src/helpers.ts b/sdk/typescript/src/helpers.tsindex f1d91273..316afd36 100644--- a/sdk/typescript/src/helpers.ts+++ b/sdk/typescript/src/helpers.ts@@ -26,11 +26,12 @@ export default class OpenLitHelper {completionTokens: number): number {try {- return (+ const cost = ((promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.chat[model].promptPrice +(completionTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) *pricingInfo.chat[model].completionPrice);+ return isNaN(cost) ? 0 : cost;} catch (error) {console.error(`Error in getChatModelCost: ${error}`);return 0;@@ -39,7 +40,8 @@ export default class OpenLitHelper {static getEmbedModelCost(model: string, pricingInfo: any, promptTokens: number): number {try {- return (promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.embeddings[model];+ const cost = (promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.embeddings[model];+ return isNaN(cost) ? 0 : cost;} catch (error) {console.error(`Error in getEmbedModelCost: ${error}`);return 0;@@ -48,7 +50,8 @@ export default class OpenLitHelper {static getImageModelCost(model: string, pricingInfo: any, size: string, quality: number): number {try {- return pricingInfo.images[model][quality][size];+ const cost = pricingInfo.images[model][quality][size];+ return isNaN(cost) ? 0 : cost;} catch (error) {console.error(`Error in getImageModelCost: ${error}`);return 0;@@ -57,7 +60,8 @@ export default class OpenLitHelper {static getAudioModelCost(model: string, pricingInfo: any, prompt: string): number {try {- return (prompt.length / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.audio[model];+ const cost = (prompt.length / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.audio[model];+ return isNaN(cost) ? 0 : cost;} catch (error) {console.error(`Error in getAudioModelCost: ${error}`);return 0;diff --git a/sdk/typescript/src/instrumentation/__tests__/openai-wrapper.test.ts b/sdk/typescript/src/instrumentation/__tests__/openai-wrapper.test.tsindex 50bc7d2a..3648d5ce 100644--- a/sdk/typescript/src/instrumentation/__tests__/openai-wrapper.test.ts+++ b/sdk/typescript/src/instrumentation/__tests__/openai-wrapper.test.ts@@ -17,6 +17,7 @@ describe('OpenAIWrapper', () => {beforeEach(() => {span = mockTracer.startSpan('test-span');span.setAttribute = jest.fn();+ span.addEvent = jest.fn();jest.clearAllMocks();});@@ -26,12 +27,19 @@ describe('OpenAIWrapper', () => {describe('_chatCompletion', () => {it('should call recordMetrics after span ends', async () => {- const mockArgs = [{ message: 'test message' }];+ const mockArgs = [{ messages: [{ role: 'user', content: 'test message' }] }];const mockResponse = {- response_id: '123',- meta: { billedUnits: { inputTokens: 10, outputTokens: 20 } },+ id: '123',+ model: 'gpt-3.5-turbo',+ usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 },+ choices: [+ {+ message: { content: 'response text', role: 'assistant' },+ finish_reason: 'stop',+ },+ ],};- const mockGenAIEndpoint = 'openai.endpoint';+ const mockGenAIEndpoint = 'openai.resources.chat.completions';jest.spyOn(OpenAIWrapper, '_chatCompletionCommonSetter').mockImplementationOnce(async ({ genAIEndpoint, span }) => {@@ -41,7 +49,7 @@ describe('OpenAIWrapper', () => {return {genAIEndpoint,- model: 'test-model',+ model: 'gpt-3.5-turbo',user: 'test-user',cost: 0.5,aiSystem: 'openai',@@ -57,7 +65,7 @@ describe('OpenAIWrapper', () => {expect(BaseWrapper.recordMetrics).toHaveBeenCalledWith(span, {genAIEndpoint: mockGenAIEndpoint,- model: 'test-model',+ model: 'gpt-3.5-turbo',user: 'test-user',cost: 0.5,aiSystem: 'openai',@@ -69,7 +77,7 @@ describe('OpenAIWrapper', () => {it('should set span attributes and return metric parameters', async () => {const mockArgs = [{- message: 'test message',+ messages: [{ role: 'user', content: 'test message' }],max_tokens: 100,temperature: 0.7,top_p: 1,@@ -77,24 +85,30 @@ describe('OpenAIWrapper', () => {presence_penalty: 2,frequency_penalty: 3,seed: 3,- stream: true,+ stream: false,+ stop: ['STOP'],},];const mockResult = {- response_id: '123',- meta: { billedUnits: { inputTokens: 10, outputTokens: 20 } },- usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 },- text: 'response text',- finishReason: 'stop',+ id: '123',+ model: 'gpt-3.5-turbo',+ usage: {+ prompt_tokens: 10,+ completion_tokens: 20,+ total_tokens: 30,+ completion_tokens_details: { reasoning_tokens: 5 },+ },choices: [{+ message: { content: 'response text', role: 'assistant' },finish_reason: 'stop',},],- model: 'test-model',+ system_fingerprint: 'fp_test',+ service_tier: 'default',};- const mockGenAIEndpoint = 'openai.endpoint';+ const mockGenAIEndpoint = 'openai.resources.chat.completions';jest.restoreAllMocks();@@ -108,6 +122,7 @@ describe('OpenAIWrapper', () => {span,});+ // Basic request parametersexpect(span.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_TOP_P, 1);expect(span.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,@@ -125,19 +140,103 @@ describe('OpenAIWrapper', () => {SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,3);- expect(span.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_SEED, 3);+ expect(span.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_SEED, '3');expect(span.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,- true+ false+ );++ // New attributes+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,+ ['STOP']+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_RESPONSE_MODEL,+ 'gpt-3.5-turbo'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,+ 'fp_test'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,+ 'default'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,+ 30+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING,+ 5);expect(metricParams).toEqual({genAIEndpoint: mockGenAIEndpoint,- model: 'test-model',+ model: 'gpt-3.5-turbo',user: 'test-user',cost: 0.5,aiSystem: 'openai',});});++ it('should handle tool calls properly', async () => {+ const mockArgs = [+ {+ messages: [{ role: 'user', content: 'test message' }],+ tools: [{ type: 'function', function: { name: 'get_weather' } }],+ },+ ];++ const mockResult = {+ id: '123',+ model: 'gpt-3.5-turbo',+ usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 },+ choices: [+ {+ message: {+ content: null,+ role: 'assistant',+ tool_calls: [+ {+ id: 'call_123',+ type: 'function',+ function: { name: 'get_weather', arguments: '{"location":"SF"}' },+ },+ ],+ },+ finish_reason: 'tool_calls',+ },+ ],+ };++ jest.spyOn(OpenlitConfig, 'updatePricingJson').mockResolvedValue({});+ jest.spyOn(OpenLitHelper, 'getChatModelCost').mockReturnValue(0.5);++ await OpenAIWrapper._chatCompletionCommonSetter({+ args: mockArgs,+ genAIEndpoint: 'openai.resources.chat.completions',+ result: mockResult,+ span,+ });++ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_TOOL_NAME,+ 'get_weather'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_TOOL_CALL_ID,+ 'call_123'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS,+ ['{"location":"SF"}']+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_TOOL_TYPE,+ 'function'+ );+ });});});diff --git a/sdk/typescript/src/instrumentation/openai/index.ts b/sdk/typescript/src/instrumentation/openai/index.tsindex fde8cb94..6d0daffa 100644--- a/sdk/typescript/src/instrumentation/openai/index.ts+++ b/sdk/typescript/src/instrumentation/openai/index.ts@@ -62,6 +62,11 @@ export default class OpenlitOpenAIInstrumentation extends InstrumentationBase {if (isWrapped(moduleExports.OpenAI.Audio.Speech.prototype)) {this._unwrap(moduleExports.OpenAI.Audio.Speech.prototype, 'create');}++ // Check if Responses API exists (OpenAI SDK >= 1.92.0)+ if ((moduleExports.OpenAI as any).Responses && isWrapped((moduleExports.OpenAI as any).Responses.prototype.create)) {+ this._unwrap((moduleExports.OpenAI as any).Responses.prototype, 'create');+ }this._wrap(moduleExports.OpenAI.Chat.Completions.prototype,@@ -98,6 +103,15 @@ export default class OpenlitOpenAIInstrumentation extends InstrumentationBase {'create',OpenAIWrapper._patchAudioCreate(this.tracer));++ // Patch Responses API if available (OpenAI SDK >= 1.92.0)+ if ((moduleExports.OpenAI as any).Responses) {+ this._wrap(+ (moduleExports.OpenAI as any).Responses.prototype,+ 'create',+ OpenAIWrapper._patchResponsesCreate(this.tracer)+ );+ }} catch (e) {console.error('Error in _patch method:', e);}@@ -110,5 +124,8 @@ export default class OpenlitOpenAIInstrumentation extends InstrumentationBase {this._unwrap(moduleExports.OpenAI.Images.prototype, 'generate');this._unwrap(moduleExports.OpenAI.Images.prototype, 'createVariation');this._unwrap(moduleExports.OpenAI.Audio.prototype, 'speech');+ if ((moduleExports.OpenAI as any).Responses) {+ this._unwrap((moduleExports.OpenAI as any).Responses.prototype, 'create');+ }}}diff --git a/sdk/typescript/src/instrumentation/openai/wrapper.ts b/sdk/typescript/src/instrumentation/openai/wrapper.tsindex 0a45c10e..5f9bf143 100644--- a/sdk/typescript/src/instrumentation/openai/wrapper.ts+++ b/sdk/typescript/src/instrumentation/openai/wrapper.ts@@ -83,6 +83,9 @@ class OpenAIWrapper extends BaseWrapper {span: Span;}): AsyncGenerator<unknown, any, unknown> {let metricParams;+ const timestamps: number[] = [];+ const startTime = Date.now();+try {const { messages } = args[0];let { tools } = args[0];@@ -90,6 +93,8 @@ class OpenAIWrapper extends BaseWrapper {id: '0',created: -1,model: '',+ system_fingerprint: '',+ service_tier: 'auto',choices: [{index: 0,@@ -102,12 +107,32 @@ class OpenAIWrapper extends BaseWrapper {prompt_tokens: 0,completion_tokens: 0,total_tokens: 0,+ completion_tokens_details: {+ reasoning_tokens: 0,+ audio_tokens: 0,+ },+ prompt_tokens_details: {+ cached_tokens: 0,+ audio_tokens: 0,+ },},};++ let toolCalls: any[] = [];+for await (const chunk of response) {+ timestamps.push(Date.now());+result.id = chunk.id;result.created = chunk.created;result.model = chunk.model;++ if (chunk.system_fingerprint) {+ result.system_fingerprint = chunk.system_fingerprint;+ }+ if (chunk.service_tier) {+ result.service_tier = chunk.service_tier;+ }if (chunk.choices[0]?.finish_reason) {result.choices[0].finish_reason = chunk.choices[0].finish_reason;@@ -119,12 +144,50 @@ class OpenAIWrapper extends BaseWrapper {result.choices[0].message.content += chunk.choices[0].delta.content;}+ // Improved tool calls handling for streamingif (chunk.choices[0]?.delta.tool_calls) {+ const deltaTools = chunk.choices[0].delta.tool_calls;++ for (const tool of deltaTools) {+ const idx = tool.index || 0;++ // Extend array if needed+ while (toolCalls.length <= idx) {+ toolCalls.push({+ id: '',+ type: 'function',+ function: { name: '', arguments: '' }+ });+ }++ if (tool.id) {+ // New tool call+ toolCalls[idx].id = tool.id;+ toolCalls[idx].type = tool.type || 'function';+ if (tool.function?.name) {+ toolCalls[idx].function.name = tool.function.name;+ }+ if (tool.function?.arguments) {+ toolCalls[idx].function.arguments = tool.function.arguments;+ }+ } else if (tool.function?.arguments) {+ // Append arguments to existing tool call+ toolCalls[idx].function.arguments += tool.function.arguments;+ }+ }+tools = true;}yield chunk;}++ if (toolCalls.length > 0) {+ result.choices[0].message = {+ ...result.choices[0].message,+ tool_calls: toolCalls+ } as any;+ }let promptTokens = 0;for (const message of messages || []) {@@ -140,16 +203,28 @@ class OpenAIWrapper extends BaseWrapper {prompt_tokens: promptTokens,completion_tokens: completionTokens,total_tokens: promptTokens + completionTokens,+ completion_tokens_details: result.usage.completion_tokens_details,+ prompt_tokens_details: result.usage.prompt_tokens_details,};}args[0].tools = tools;++ // Calculate TTFT and TBT+ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;+ let tbt = 0;+ if (timestamps.length > 1) {+ const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;+ }metricParams = await OpenAIWrapper._chatCompletionCommonSetter({args,genAIEndpoint,result,span,+ ttft,+ tbt,});return result;@@ -169,11 +244,15 @@ class OpenAIWrapper extends BaseWrapper {genAIEndpoint,result,span,+ ttft = 0,+ tbt = 0,}: {args: any[];genAIEndpoint: string;result: any;span: Span;+ ttft?: number;+ tbt?: number;}) {const traceContent = OpenlitConfig.traceContent;const {@@ -183,6 +262,7 @@ class OpenAIWrapper extends BaseWrapper {n = 1,presence_penalty = 0,seed = null,+ stop = null,temperature = 1,top_p,user,@@ -192,12 +272,18 @@ class OpenAIWrapper extends BaseWrapper {// Request Params attributes : Startspan.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, top_p || 1);- span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, max_tokens);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, max_tokens || -1);span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, temperature);span.setAttribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty);span.setAttribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty);- span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SEED, seed);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SEED, seed ? String(seed) : '');span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, stream);+ if (stop) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, Array.isArray(stop) ? stop : [stop]);+ }+ if (user) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_USER, user);+ }if (traceContent) {// Format 'messages' into a single string@@ -237,6 +323,7 @@ class OpenAIWrapper extends BaseWrapper {span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_ID, result.id);const model = result.model || 'gpt-3.5-turbo';+ const responseModel = result.model || model;const pricingInfo = await OpenlitConfig.updatePricingJson(OpenlitConfig.pricing_json);@@ -256,38 +343,128 @@ class OpenAIWrapper extends BaseWrapper {aiSystem: OpenAIWrapper.aiSystem,});+ // Response model+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, responseModel);++ // OpenAI-specific attributes+ if (result.system_fingerprint) {+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, result.system_fingerprint);+ }+ if (result.service_tier) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, result.service_tier);+ }++ // Token usagespan.setAttribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, result.usage.prompt_tokens);span.setAttribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,result.usage.completion_tokens);span.setAttribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, result.usage.total_tokens);+ span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, result.usage.total_tokens);++ // Enhanced token details+ if (result.usage.completion_tokens_details) {+ if (result.usage.completion_tokens_details.reasoning_tokens) {+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS,+ result.usage.completion_tokens_details.reasoning_tokens+ );+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING,+ result.usage.completion_tokens_details.reasoning_tokens+ );+ }+ if (result.usage.completion_tokens_details.audio_tokens) {+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO,+ result.usage.completion_tokens_details.audio_tokens+ );+ }+ }++ if (result.usage.prompt_tokens_details) {+ if (result.usage.prompt_tokens_details.cached_tokens) {+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,+ result.usage.prompt_tokens_details.cached_tokens+ );+ }+ if (result.usage.prompt_tokens_details.audio_tokens) {+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE,+ result.usage.prompt_tokens_details.audio_tokens+ );+ }+ }++ // TTFT and TBT metrics+ if (ttft > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);+ }+ if (tbt > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);+ }+ // Finish reasonif (result.choices[0].finish_reason) {span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,- result.choices[0].finish_reason+ [result.choices[0].finish_reason]);}++ // Output type+ const outputType = typeof result.choices[0].message.content === 'string'+ ? SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT+ : SemanticConvention.GEN_AI_OUTPUT_TYPE_JSON;+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, outputType);++ // Tool calls handling+ if (result.choices[0].message.tool_calls) {+ const toolCalls = result.choices[0].message.tool_calls;+ const toolNames = toolCalls.map((t: any) => t.function?.name || '').filter(Boolean);+ const toolIds = toolCalls.map((t: any) => t.id || '').filter(Boolean);+ const toolArgs = toolCalls.map((t: any) => t.function?.arguments || '').filter(Boolean);+ const toolTypes = toolCalls.map((t: any) => t.type || '').filter(Boolean);++ if (toolNames.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_NAME, toolNames.join(', '));+ }+ if (toolIds.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, toolIds.join(', '));+ }+ if (toolArgs.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS, toolArgs);+ }+ if (toolTypes.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_TYPE, toolTypes.join(', '));+ }+ }- if (tools) {- span.setAttribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, 'Function called with tools');- } else {- if (traceContent) {- if (n === 1) {- span.setAttribute(- SemanticConvention.GEN_AI_CONTENT_COMPLETION,- result.choices[0].message.content- );- } else {- let i = 0;- while (i < n) {- const attribute_name = `${SemanticConvention.GEN_AI_CONTENT_COMPLETION}.[i]`;- span.setAttribute(attribute_name, result.choices[i].message.content);- i += 1;- }+ // Content+ if (traceContent) {+ // Format completion content - use actual content or empty string if only tool calls+ const completionContent = result.choices[0].message.content || '';++ if (n === 1) {+ span.setAttribute(+ SemanticConvention.GEN_AI_CONTENT_COMPLETION,+ completionContent+ );+ } else {+ let i = 0;+ while (i < n) {+ const attribute_name = `${SemanticConvention.GEN_AI_CONTENT_COMPLETION}.${i}`;+ span.setAttribute(attribute_name, result.choices[i].message.content || '');+ i += 1;}}++ // Add events for backward compatibility+ span.addEvent(SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT, {+ [SemanticConvention.GEN_AI_CONTENT_COMPLETION]: completionContent,+ });}return {@@ -307,13 +484,7 @@ class OpenAIWrapper extends BaseWrapper {return async function (this: any, ...args: any[]) {const span = tracer.startSpan(genAIEndpoint, { kind: SpanKind.CLIENT });return context.with(trace.setSpan(context.active(), span), async () => {- let metricParams: BaseSpanAttributes = {- genAIEndpoint,- model: '',- user: '',- cost: 0,- aiSystem: OpenAIWrapper.aiSystem,- };+ let metricParams: BaseSpanAttributes | undefined;try {const response = await originalMethod.apply(this, args);@@ -331,7 +502,7 @@ class OpenAIWrapper extends BaseWrapper {);const { dimensions, encoding_format = 'float', input, user } = args[0];- // Set base span attribues+ // Set base span attributesOpenAIWrapper.setBaseSpanAttributes(span, {genAIEndpoint,model,@@ -340,11 +511,25 @@ class OpenAIWrapper extends BaseWrapper {aiSystem: OpenAIWrapper.aiSystem,});+ // Set missing critical attributes to match Python SDK+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, 'api.openai.com');+ span.setAttribute(SemanticConvention.SERVER_PORT, 443);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, 0);+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, 0);+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.7.0');+// Request Params attributes : Start- span.setAttribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, encoding_format);- span.setAttribute(SemanticConvention.GEN_AI_REQUEST_EMBEDDING_DIMENSION, dimensions);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [encoding_format]);+ if (dimensions) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_EMBEDDING_DIMENSION, dimensions);+ }+ if (user) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_USER, user);+ }if (traceContent) {- span.setAttribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, input);+ const formattedInput = typeof input === 'string' ? input : JSON.stringify(input);+ span.setAttribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formattedInput);}// Request Params attributes : End@@ -356,6 +541,10 @@ class OpenAIWrapper extends BaseWrapper {SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,response.usage.total_tokens);+ span.setAttribute(+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,+ response.usage.prompt_tokens+ );metricParams = {genAIEndpoint,@@ -368,9 +557,12 @@ class OpenAIWrapper extends BaseWrapper {return response;} catch (e: any) {OpenLitHelper.handleException(span, e);+ throw e;} finally {span.end();- BaseWrapper.recordMetrics(span, metricParams);+ if (metricParams) {+ BaseWrapper.recordMetrics(span, metricParams);+ }}});};@@ -712,6 +904,370 @@ class OpenAIWrapper extends BaseWrapper {};};}++ static _patchResponsesCreate(tracer: Tracer): any {+ const genAIEndpoint = 'openai.resources.responses';+ return (originalMethod: (...args: any[]) => any) => {+ return async function (this: any, ...args: any[]) {+ const span = tracer.startSpan(genAIEndpoint, { kind: SpanKind.CLIENT });+ return context+ .with(trace.setSpan(context.active(), span), async () => {+ return originalMethod.apply(this, args);+ })+ .then((response: any) => {+ const { stream = false } = args[0];++ if (stream) {+ return OpenLitHelper.createStreamProxy(+ response,+ OpenAIWrapper._responsesGenerator({+ args,+ genAIEndpoint,+ response,+ span,+ })+ );+ }++ return OpenAIWrapper._responsesComplete({ args, genAIEndpoint, response, span });+ })+ .catch((e: any) => {+ OpenLitHelper.handleException(span, e);+ span.end();+ });+ };+ };+ }++ static async _responsesComplete({+ args,+ genAIEndpoint,+ response,+ span,+ }: {+ args: any[];+ genAIEndpoint: string;+ response: any;+ span: Span;+ }): Promise<any> {+ let metricParams;+ try {+ metricParams = await OpenAIWrapper._responsesCommonSetter({+ args,+ genAIEndpoint,+ result: response,+ span,+ });+ return response;+ } catch (e: any) {+ OpenLitHelper.handleException(span, e);+ } finally {+ span.end();+ if (metricParams) {+ BaseWrapper.recordMetrics(span, metricParams);+ }+ }+ }++ static async *_responsesGenerator({+ args,+ genAIEndpoint,+ response,+ span,+ }: {+ args: any[];+ genAIEndpoint: string;+ response: any;+ span: Span;+ }): AsyncGenerator<unknown, any, unknown> {+ let metricParams;+ const timestamps: number[] = [];+ const startTime = Date.now();++ try {+ const { input } = args[0];+ const result = {+ id: '',+ model: '',+ service_tier: 'default',+ status: 'completed',+ output: [] as any[],+ usage: {+ input_tokens: 0,+ output_tokens: 0,+ output_tokens_details: {+ reasoning_tokens: 0,+ },+ },+ };++ let llmResponse = '';+ let responseTools: any[] = [];++ for await (const chunk of response) {+ timestamps.push(Date.now());++ if (chunk.type === 'response.output_text.delta') {+ llmResponse += chunk.delta || '';+ } else if (chunk.type === 'response.output_item.added') {+ const item = chunk.item;+ if (item?.type === 'function_call') {+ responseTools.push({+ id: item.id,+ call_id: item.call_id,+ name: item.name,+ type: item.type,+ arguments: item.arguments || '',+ status: item.status,+ });+ }+ } else if (chunk.type === 'response.function_call_arguments.delta') {+ const itemId = chunk.item_id;+ const delta = chunk.delta || '';+ const tool = responseTools.find(t => t.id === itemId);+ if (tool) {+ tool.arguments += delta;+ }+ } else if (chunk.type === 'response.completed') {+ const responseData = chunk.response;+ result.id = responseData.id;+ result.model = responseData.model;+ result.status = responseData.status;++ const usage = responseData.usage || {};+ result.usage.input_tokens = usage.input_tokens || 0;+ result.usage.output_tokens = usage.output_tokens || 0;+ result.usage.output_tokens_details.reasoning_tokens =+ usage.output_tokens_details?.reasoning_tokens || 0;+ }++ yield chunk;+ }++ // Construct output array+ if (llmResponse) {+ result.output.push({+ type: 'message',+ content: [{ type: 'text', text: llmResponse }],+ });+ }++ if (responseTools.length > 0) {+ result.output.push(...responseTools);+ }++ // Calculate TTFT and TBT+ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;+ let tbt = 0;+ if (timestamps.length > 1) {+ const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;+ }++ metricParams = await OpenAIWrapper._responsesCommonSetter({+ args,+ genAIEndpoint,+ result,+ span,+ ttft,+ tbt,+ });++ return result;+ } catch (e: any) {+ OpenLitHelper.handleException(span, e);+ } finally {+ span.end();+ if (metricParams) {+ BaseWrapper.recordMetrics(span, metricParams);+ }+ }+ }++ static async _responsesCommonSetter({+ args,+ genAIEndpoint,+ result,+ span,+ ttft = 0,+ tbt = 0,+ }: {+ args: any[];+ genAIEndpoint: string;+ result: any;+ span: Span;+ ttft?: number;+ tbt?: number;+ }) {+ const traceContent = OpenlitConfig.traceContent;+ const {+ input,+ temperature = 1.0,+ top_p = 1.0,+ max_output_tokens,+ reasoning,+ stream = false,+ } = args[0];++ // Format input for prompt+ let prompt = '';+ if (typeof input === 'string') {+ prompt = input;+ } else if (Array.isArray(input)) {+ const formattedMessages = [];+ for (const item of input) {+ const role = item.role || 'user';+ const content = item.content;++ if (typeof content === 'string') {+ formattedMessages.push(`${role}: ${content}`);+ } else if (Array.isArray(content)) {+ const contentParts = content+ .map((part: any) => {+ if (part.type === 'input_text') {+ return `text: ${part.text || ''}`;+ } else if (part.type === 'input_image' && part.image_url && !part.image_url.startsWith('data:')) {+ return `image_url: ${part.image_url}`;+ }+ return '';+ })+ .filter(Boolean)+ .join(', ');+ formattedMessages.push(`${role}: ${contentParts}`);+ }+ }+ prompt = formattedMessages.join('\n');+ }++ // Request Params attributes+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, temperature);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, top_p);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, max_output_tokens || -1);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, stream);++ if (reasoning?.effort) {+ span.setAttribute('gen_ai.request.reasoning_effort', reasoning.effort);+ }++ if (traceContent) {+ span.setAttribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt);+ }++ span.setAttribute(+ SemanticConvention.GEN_AI_OPERATION,+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT+ );++ const model = result.model || 'gpt-4o';+ const responseModel = result.model || model;++ const pricingInfo = await OpenlitConfig.updatePricingJson(OpenlitConfig.pricing_json);++ // Calculate cost+ const inputTokens = result.usage?.input_tokens || 0;+ const outputTokens = result.usage?.output_tokens || 0;+ const cost = OpenLitHelper.getChatModelCost(+ model,+ pricingInfo,+ inputTokens,+ outputTokens+ );++ OpenAIWrapper.setBaseSpanAttributes(span, {+ genAIEndpoint,+ model,+ user: '',+ cost,+ aiSystem: OpenAIWrapper.aiSystem,+ });++ // Response attributes+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_ID, result.id);+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, responseModel);+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [result.status || 'completed']);+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT);++ if (result.service_tier) {+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, result.service_tier);+ }++ // Token usage+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, inputTokens + outputTokens);+ span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, inputTokens + outputTokens);++ // Reasoning tokens+ if (result.usage?.output_tokens_details?.reasoning_tokens) {+ span.setAttribute(+ SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS,+ result.usage.output_tokens_details.reasoning_tokens+ );+ }++ // TTFT and TBT metrics+ if (ttft > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);+ }+ if (tbt > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);+ }++ // Extract completion text from output+ let completionText = '';+ if (result.output && Array.isArray(result.output)) {+ for (const item of result.output) {+ if (item.type === 'message' && item.content) {+ for (const content of item.content) {+ if (content.type === 'text' || content.type === 'output_text') {+ completionText += content.text || '';+ }+ }+ }+ }+ }++ // Tool calls handling for Responses API+ const toolCalls = result.tools || [];+ if (toolCalls.length > 0) {+ const toolNames = toolCalls.map((t: any) => t.name || '').filter(Boolean);+ const toolIds = toolCalls.map((t: any) => t.call_id || '').filter(Boolean);+ const toolArgs = toolCalls.map((t: any) => t.arguments || '').filter(Boolean);+ const toolTypes = toolCalls.map((t: any) => t.type || '').filter(Boolean);++ if (toolNames.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_NAME, toolNames.join(', '));+ }+ if (toolIds.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, toolIds.join(', '));+ }+ if (toolArgs.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS, toolArgs.join(', '));+ }+ if (toolTypes.length > 0) {+ span.setAttribute(SemanticConvention.GEN_AI_TOOL_TYPE, toolTypes.join(', '));+ }+ }++ // Content+ if (traceContent) {+ // Set completion content - use actual text or empty string if only tool calls+ span.setAttribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, completionText);++ // Add events for backward compatibility+ span.addEvent(SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT, {+ [SemanticConvention.GEN_AI_CONTENT_COMPLETION]: completionText,+ });+ }++ return {+ genAIEndpoint,+ model,+ user: '',+ cost,+ aiSystem: OpenAIWrapper.aiSystem,+ };+ }}export default OpenAIWrapper;diff --git a/sdk/typescript/src/semantic-convention.ts b/sdk/typescript/src/semantic-convention.tsindex da715175..a67ef121 100644--- a/sdk/typescript/src/semantic-convention.ts+++ b/sdk/typescript/src/semantic-convention.ts@@ -11,6 +11,8 @@ export default class SemanticConvention {static GEN_AI_HUB_REPO = 'gen_ai.hub.repo';static GEN_AI_RETRIEVAL_SOURCE = 'gen_ai.retrieval.source';static GEN_AI_REQUESTS = 'gen_ai.total.requests';+ static GEN_AI_SDK_VERSION = 'gen_ai.sdk.version';+ static GEN_AI_OUTPUT_TYPE = 'gen_ai.output.type';// GenAI Request@@ -24,7 +26,8 @@ export default class SemanticConvention {static GEN_AI_REQUEST_SEED = 'gen_ai.request.seed';static GEN_AI_REQUEST_FREQUENCY_PENALTY = 'gen_ai.request.frequency_penalty';static GEN_AI_REQUEST_PRESENCE_PENALTY = 'gen_ai.request.presence_penalty';- static GEN_AI_REQUEST_ENCODING_FORMATS = 'gen_ai.request.embedding_format';+ static GEN_AI_REQUEST_STOP_SEQUENCES = 'gen_ai.request.stop_sequences';+ static GEN_AI_REQUEST_ENCODING_FORMATS = 'gen_ai.request.encoding_formats';static GEN_AI_REQUEST_EMBEDDING_DIMENSION = 'gen_ai.request.embedding_dimension';static GEN_AI_REQUEST_TOOL_CHOICE = 'gen_ai.request.tool_choice';static GEN_AI_REQUEST_AUDIO_VOICE = 'gen_ai.request.audio_voice';@@ -48,36 +51,69 @@ export default class SemanticConvention {static GEN_AI_USAGE_TOTAL_TOKENS = 'gen_ai.usage.total_tokens';static GEN_AI_USAGE_COST = 'gen_ai.usage.cost';static GEN_AI_USAGE_REASONING_TOKENS = 'gen_ai.usage.reasoning_tokens';++ // Enhanced token details (for prompt caching, audio tokens, etc.)+ static GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO = 'gen_ai.usage.completion_tokens_details.audio';+ static GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING = 'gen_ai.usage.completion_tokens_details.reasoning';+ static GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ = 'gen_ai.usage.prompt_tokens_details.cache_read';+ static GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE = 'gen_ai.usage.prompt_tokens_details.cache_write';// GenAI Responsestatic GEN_AI_RESPONSE_ID = 'gen_ai.response.id';- static GEN_AI_RESPONSE_FINISH_REASON = 'gen_ai.response.finish_reason';- static GEN_AI_RESPONSE_IMAGE = 'gen_ai.response.image'; // Not used directly in code yet+ static GEN_AI_RESPONSE_MODEL = 'gen_ai.response.model';+ static GEN_AI_RESPONSE_FINISH_REASON = 'gen_ai.response.finish_reasons';+ static GEN_AI_RESPONSE_IMAGE = 'gen_ai.response.image';static GEN_AI_RESPONSE_IMAGE_SIZE = 'gen_ai.request.image_size';static GEN_AI_RESPONSE_IMAGE_QUALITY = 'gen_ai.request.image_quality';static GEN_AI_RESPONSE_IMAGE_STYLE = 'gen_ai.request.image_style';++ // OpenAI-specific attributes+ static GEN_AI_REQUEST_SERVICE_TIER = 'gen_ai.request.service_tier';+ static GEN_AI_RESPONSE_SERVICE_TIER = 'gen_ai.response.service_tier';+ static GEN_AI_RESPONSE_SYSTEM_FINGERPRINT = 'gen_ai.response.system_fingerprint';// GenAI Content- static GEN_AI_CONTENT_PROMPT = 'gen_ai.content.prompt';+ static GEN_AI_CONTENT_PROMPT = 'gen_ai.prompt';+ static GEN_AI_CONTENT_PROMPT_EVENT = 'gen_ai.content.prompt';static GEN_AI_CONTENT_COMPLETION = 'gen_ai.completion';+ static GEN_AI_CONTENT_COMPLETION_EVENT = 'gen_ai.content.completion';static GEN_AI_CONTENT_REVISED_PROMPT = 'gen_ai.content.revised_prompt';++ // Tool attributes+ static GEN_AI_TOOL_NAME = 'gen_ai.tool.name';+ static GEN_AI_TOOL_TYPE = 'gen_ai.tool.call.type';+ static GEN_AI_TOOL_DESCRIPTION = 'gen_ai.tool.description';+ static GEN_AI_TOOL_DEFINITION = 'gen_ai.tool.definition';+ static GEN_AI_TOOL_CALL_ID = 'gen_ai.tool.call.id';+ static GEN_AI_TOOL_CALL_ARGUMENTS = 'gen_ai.tool.call.arguments';+ static GEN_AI_TOOL_CALL_RESULT = 'gen_ai.tool.call.result';++ static GEN_AI_TOKEN_TYPE = 'gen_ai.token.type';++ static GEN_AI_OPERATION_TYPE_TEXT_COMPLETION = 'text_completion';static GEN_AI_OPERATION_TYPE_CHAT = 'chat';- static GEN_AI_OPERATION_TYPE_EMBEDDING = 'embedding';+ static GEN_AI_OPERATION_TYPE_EMBEDDING = 'embeddings';static GEN_AI_OPERATION_TYPE_IMAGE = 'image';static GEN_AI_OPERATION_TYPE_AUDIO = 'audio';static GEN_AI_OPERATION_TYPE_FINETUNING = 'fine_tuning';static GEN_AI_OPERATION_TYPE_VECTORDB = 'vectordb';- static GEN_AI_OPERATION_TYPE_FRAMEWORK = 'framework';+ static GEN_AI_OPERATION_TYPE_FRAMEWORK = 'workflow';++ // GenAI Output Types+ static GEN_AI_OUTPUT_TYPE_TEXT = 'text';+ static GEN_AI_OUTPUT_TYPE_JSON = 'json';+ static GEN_AI_OUTPUT_TYPE_IMAGE = 'image';+ static GEN_AI_OUTPUT_TYPE_SPEECH = 'speech';static GEN_AI_SYSTEM_HUGGING_FACE = 'huggingface';static GEN_AI_SYSTEM_OPENAI = 'openai';- static GEN_AI_SYSTEM_AZURE_OPENAI = 'azure_openai';+ static GEN_AI_SYSTEM_AZURE_OPENAI = 'az.ai.openai';static GEN_AI_SYSTEM_ANTHROPIC = 'anthropic';static GEN_AI_SYSTEM_COHERE = 'cohere';- static GEN_AI_SYSTEM_MISTRAL = 'mistral';- static GEN_AI_SYSTEM_AWS_BEDROCK = 'bedrock';- static GEN_AI_SYSTEM_VERTEXAI = 'vertexai';+ static GEN_AI_SYSTEM_MISTRAL = 'mistral_ai';+ static GEN_AI_SYSTEM_AWS_BEDROCK = 'aws.bedrock';+ static GEN_AI_SYSTEM_VERTEXAI = 'vertex_ai';static GEN_AI_SYSTEM_LANGCHAIN = 'langchain';// Vector DB@@ -115,7 +151,13 @@ export default class SemanticConvention {static DB_UPDATE_VALUES = 'db.update.values';static DB_UPDATE_ID = 'db.update.id';static GEN_AI_CLIENT_OPERATION_DURATION = 'gen_ai.client.operation.duration';- static GEN_AI_SERVER_TBT = 'gen_ai.server.tbt';- static GEN_AI_SERVER_TTFT = 'gen_ai.server.ttft';+ static GEN_AI_CLIENT_TOKEN_USAGE = 'gen_ai.client.token.usage';+ static GEN_AI_SERVER_TBT = 'gen_ai.server.time_per_output_token';+ static GEN_AI_SERVER_TTFT = 'gen_ai.server.time_to_first_token';static DB_CLIENT_OPERATION_DURATION = 'db.client.operation.duration';++ // Server attributes+ static SERVER_ADDRESS = 'server.address';+ static SERVER_PORT = 'server.port';+ static ERROR_TYPE = 'error.type';}