Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat(typescript): add AssemblyAI instrumentation (#1234) (#1311)

Impactquadrant86fe50431bce
diff --git a/sdk/typescript/src/helpers.ts b/sdk/typescript/src/helpers.ts
index 6f6577ac..63567d56 100644
--- a/sdk/typescript/src/helpers.ts
+++ b/sdk/typescript/src/helpers.ts
@@ -418,9 +418,24 @@ export default class OpenLitHelper {
}
}
- static getAudioModelCost(model: string, pricingInfo: any, prompt: string): number {
+ static getAudioModelCost(
+ model: string,
+ pricingInfo: any,
+ prompt: string,
+ duration?: number
+ ): number {
try {
- const cost = (prompt.length / OpenLitHelper.PROMPT_TOKEN_FACTOR) * pricingInfo.audio[model];
+ const rate = pricingInfo?.audio?.[model];
+ if (typeof rate !== 'number') {
+ return 0;
+ }
+ if (prompt) {
+ const cost = (prompt.length / OpenLitHelper.PROMPT_TOKEN_FACTOR) * rate;
+ return isNaN(cost) ? 0 : cost;
+ }
+ const durationSeconds =
+ typeof duration === 'number' && Number.isFinite(duration) ? duration : 0;
+ const cost = durationSeconds * rate;
return isNaN(cost) ? 0 : cost;
} catch (error) {
console.error(`Error in getAudioModelCost: ${error}`);
diff --git a/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts b/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts
new file mode 100644
index 00000000..1b61c733
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts
@@ -0,0 +1,360 @@
+import { Span, trace } from '@opentelemetry/api';
+import AssemblyAIWrapper from '../assemblyai/wrapper';
+import OpenlitConfig from '../../config';
+import OpenLitHelper from '../../helpers';
+import BaseWrapper from '../base-wrapper';
+import SemanticConvention from '../../semantic-convention';
+
+jest.mock('../../../src/config');
+jest.mock('../../../src/helpers');
+jest.mock('../../../src/instrumentation/base-wrapper');
+
+const mockTracer = trace.getTracer('test-tracer');
+
+describe('AssemblyAIWrapper', () => {
+ let span: Span;
+
+ beforeEach(() => {
+ span = mockTracer.startSpan('test-span');
+ span.setAttribute = jest.fn();
+ span.addEvent = jest.fn();
+ jest.clearAllMocks();
+
+ (OpenLitHelper as any).buildInputMessages = jest
+ .fn()
+ .mockImplementation((messages: Array<{ role: string; content: string }>) =>
+ JSON.stringify(
+ messages.map((m) => ({
+ role: m.role,
+ parts: [{ type: 'text', content: m.content }],
+ }))
+ )
+ );
+ (OpenLitHelper as any).buildOutputMessages = jest
+ .fn()
+ .mockImplementation((text: string, finishReason: string) =>
+ JSON.stringify([
+ {
+ role: 'assistant',
+ parts: [{ type: 'text', content: text }],
+ finish_reason: finishReason,
+ },
+ ])
+ );
+ (BaseWrapper as any).setBaseSpanAttributes = jest.fn();
+ });
+
+ afterEach(() => {
+ span.end();
+ });
+
+ describe('_parseAudioArgs', () => {
+ it('should parse options and speechModel (camelCase) from a params object', () => {
+ const parsed = AssemblyAIWrapper._parseAudioArgs([
+ {
+ audio: 'https://example.com/audio.mp3',
+ speechModel: 'nano',
+ },
+ ]);
+
+ expect(parsed.requestModel).toBe('nano');
+ expect(parsed.audioUrl).toBe('https://example.com/audio.mp3');
+ });
+
+ it('should parse speech_model (snake_case) and default the model to "best"', () => {
+ const withSnake = AssemblyAIWrapper._parseAudioArgs([
+ { audio: 'https://example.com/a.mp3', speech_model: 'best' },
+ ]);
+ expect(withSnake.requestModel).toBe('best');
+
+ const withDefault = AssemblyAIWrapper._parseAudioArgs([
+ { audio: 'https://example.com/a.mp3' },
+ ]);
+ expect(withDefault.requestModel).toBe('best');
+ });
+
+ it('should treat a bare string arg as a transcript id (e.g. transcripts.get)', () => {
+ const parsed = AssemblyAIWrapper._parseAudioArgs(['transcript-id-123']);
+ expect(parsed.audioUrl).toBe('transcript-id-123');
+ expect(parsed.requestModel).toBe('best');
+ });
+
+ it('should return an empty string (never "undefined"/"null") when audio is missing', () => {
+ expect(AssemblyAIWrapper._parseAudioArgs([{ speechModel: 'nano' }]).audioUrl).toBe('');
+ expect(AssemblyAIWrapper._parseAudioArgs([{ audio: null }]).audioUrl).toBe('');
+ expect(AssemblyAIWrapper._parseAudioArgs([{ audio: undefined }]).audioUrl).toBe('');
+ expect(AssemblyAIWrapper._parseAudioArgs([]).audioUrl).toBe('');
+ });
+ });
+
+ describe('_commonAudioSetter', () => {
+ it('should set span attributes from the transcript response and return metric parameters', () => {
+ const mockArgs = [
+ {
+ audio: 'https://example.com/audio.mp3',
+ speechModel: 'best',
+ },
+ ];
+ const mockResponse = {
+ id: 'transcript-abc',
+ text: 'hello transcribed world',
+ audio_url: 'https://example.com/audio.mp3',
+ audio_duration: 42,
+ };
+
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = true;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ const metricParams = AssemblyAIWrapper._commonAudioSetter({
+ args: mockArgs,
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: mockResponse,
+ span,
+ isStream: false,
+ });
+
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_DURATION,
+ 42
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ 'text'
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_ID,
+ 'transcript-abc'
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
+ ['stop']
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
+ false
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
+ 0
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
+ 0
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ 0
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_PROVIDER_NAME,
+ 'assemblyai'
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL,
+ 'best'
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_INPUT_MESSAGES,
+ JSON.stringify([
+ { role: 'user', parts: [{ type: 'text', content: 'https://example.com/audio.mp3' }] },
+ ])
+ );
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
+ JSON.stringify([
+ {
+ role: 'assistant',
+ parts: [{ type: 'text', content: 'hello transcribed world' }],
+ finish_reason: 'stop',
+ },
+ ])
+ );
+
+ expect(metricParams).toEqual({
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ model: 'best',
+ cost: 0.005,
+ aiSystem: 'assemblyai',
+ serverAddress: 'api.assemblyai.com',
+ serverPort: 443,
+ });
+ });
+
+ it('should stamp the AssemblyAI package version on the span', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = false;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ audio: 'https://example.com/a.mp3' }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: { id: 'x', text: '', audio_url: '', audio_duration: 0 },
+ span,
+ isStream: false,
+ sdkVersion: '4.16.1',
+ });
+
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_SDK_VERSION,
+ '4.16.1'
+ );
+ });
+
+ it('should not set message content if captureMessageContent is false', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = false;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ audio: 'https://example.com/a.mp3' }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: { id: 'x', text: 'hi', audio_url: '', audio_duration: 0 },
+ span,
+ isStream: false,
+ });
+
+ expect(span.setAttribute).not.toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_INPUT_MESSAGES,
+ expect.any(String)
+ );
+ expect(span.setAttribute).not.toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
+ expect.any(String)
+ );
+ });
+
+ it('should emit inference event when disableEvents is false and captureMessageContent is true', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = false;
+ (OpenlitConfig as any).captureMessageContent = true;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ const spyEmit = jest.spyOn(OpenLitHelper, 'emitInferenceEvent');
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ audio: 'https://example.com/a.mp3', speechModel: 'nano' }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: { id: 'rid', text: 'hi', audio_url: '', audio_duration: 5 },
+ span,
+ isStream: false,
+ });
+
+ expect(spyEmit).toHaveBeenCalledWith(
+ span,
+ expect.objectContaining({
+ [SemanticConvention.GEN_AI_OPERATION]: 'audio',
+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: 'nano',
+ [SemanticConvention.GEN_AI_RESPONSE_ID]: 'rid',
+ })
+ );
+ });
+
+ it('should not emit inference event when captureMessageContent is false', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = false;
+ (OpenlitConfig as any).captureMessageContent = false;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ const spyEmit = jest.spyOn(OpenLitHelper, 'emitInferenceEvent');
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ audio: 'https://example.com/a.mp3', speechModel: 'nano' }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: { id: 'rid', text: 'hi', audio_url: '', audio_duration: 5 },
+ span,
+ isStream: false,
+ });
+
+ expect(spyEmit).not.toHaveBeenCalled();
+ });
+
+ it('should resolve the model from the transcript response for transcripts.get polls', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = false;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);
+
+ const metricParams = AssemblyAIWrapper._commonAudioSetter({
+ args: ['transcript-id-123'],
+ genAIEndpoint: 'assemblyai.transcripts.get',
+ response: {
+ id: 'transcript-id-123',
+ text: 'done',
+ speech_model: 'nano',
+ audio_duration: 10,
+ },
+ span,
+ isStream: false,
+ });
+
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL,
+ 'nano'
+ );
+ expect(metricParams.model).toBe('nano');
+ });
+
+ it('should pass audio duration to getAudioModelCost when audio_url is absent', () => {
+ (OpenlitConfig as any).pricingInfo = { audio: { best: 0.001 } };
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = false;
+ const costSpy = jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.01);
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: ['transcript-id-123'],
+ genAIEndpoint: 'assemblyai.transcripts.get',
+ response: {
+ id: 'transcript-id-123',
+ text: 'done',
+ speech_model: 'best',
+ audio_duration: 60,
+ },
+ span,
+ isStream: false,
+ });
+
+ expect(costSpy).toHaveBeenCalledWith('best', { audio: { best: 0.001 } }, '', 60);
+ });
+
+ it('should never emit the literal string "undefined"/"null" when fields are missing', () => {
+ (OpenlitConfig as any).pricingInfo = {};
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = true;
+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0);
+
+ // No audio in args, and a response with no id/text/audio_url.
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ speechModel: 'best' }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: {},
+ span,
+ isStream: false,
+ });
+
+ const calls = (span.setAttribute as jest.Mock).mock.calls;
+ for (const [, value] of calls) {
+ expect(value).not.toBe('undefined');
+ expect(value).not.toBe('null');
+ if (typeof value === 'string') {
+ expect(value).not.toContain('"undefined"');
+ expect(value).not.toContain('"null"');
+ }
+ }
+
+ // Response id is set as an empty string rather than 'undefined'.
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_RESPONSE_ID,
+ ''
+ );
+ // Input message content is the empty string, not 'undefined'.
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ SemanticConvention.GEN_AI_INPUT_MESSAGES,
+ JSON.stringify([{ role: 'user', parts: [{ type: 'text', content: '' }] }])
+ );
+ });
+ });
+});
diff --git a/sdk/typescript/src/instrumentation/__tests__/helpers-genai.test.ts b/sdk/typescript/src/instrumentation/__tests__/helpers-genai.test.ts
index 7bbd8857..63e62cab 100644
--- a/sdk/typescript/src/instrumentation/__tests__/helpers-genai.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/helpers-genai.test.ts
@@ -353,3 +353,27 @@ describe('OpenLitHelper.computeAgentVersionHash', () => {
expect(hash()).toBe(expected);
});
});
+
+describe('OpenLitHelper.getAudioModelCost', () => {
+ const pricingInfo = {
+ audio: {
+ best: 0.00010277777,
+ nano: 0.00003333333,
+ },
+ };
+
+ it('uses prompt length when audio url is present', () => {
+ const prompt = 'https://example.com/audio.mp3';
+ const cost = OpenLitHelper.getAudioModelCost('best', pricingInfo, prompt);
+ expect(cost).toBeCloseTo((prompt.length / 1000) * pricingInfo.audio.best);
+ });
+
+ it('uses duration when prompt is empty', () => {
+ const cost = OpenLitHelper.getAudioModelCost('nano', pricingInfo, '', 120);
+ expect(cost).toBeCloseTo(120 * pricingInfo.audio.nano);
+ });
+
+ it('returns 0 for unknown models', () => {
+ expect(OpenLitHelper.getAudioModelCost('unknown', pricingInfo, '', 60)).toBe(0);
+ });
+});
diff --git a/sdk/typescript/src/instrumentation/assemblyai/index.ts b/sdk/typescript/src/instrumentation/assemblyai/index.ts
new file mode 100644
index 00000000..18f36ee4
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/assemblyai/index.ts
@@ -0,0 +1,119 @@
+import {
+ InstrumentationBase,
+ InstrumentationModuleDefinition,
+ InstrumentationNodeModuleDefinition,
+ isWrapped,
+} from '@opentelemetry/instrumentation';
+import { InstrumentationConfig } from '@opentelemetry/instrumentation';
+import { diag } from '@opentelemetry/api';
+import { INSTRUMENTATION_PREFIX } from '../../constant';
+import AssemblyAIWrapper from './wrapper';
+
+export interface AssemblyAIInstrumentationConfig extends InstrumentationConfig {}
+
+const TRANSCRIBE_METHODS = ['transcribe', 'submit', 'get'];
+
+export default class OpenlitAssemblyAIInstrumentation extends InstrumentationBase {
+ private _transcriptsProto: any = null;
+
+ constructor(config: AssemblyAIInstrumentationConfig = {}) {
+ super(`${INSTRUMENTATION_PREFIX}/instrumentation-assemblyai`, '1.0.0', config);
+ }
+
+ protected init(): void | InstrumentationModuleDefinition | InstrumentationModuleDefinition[] {
+ const assemblyaiModule = new InstrumentationNodeModuleDefinition(
+ 'assemblyai',
+ ['>=4.0.0'],
+ (moduleExports, moduleVersion) => {
+ this._patch(moduleExports, moduleVersion);
+ return moduleExports;
+ },
+ (moduleExports) => {
+ if (moduleExports !== undefined) {
+ this._unpatch(moduleExports);
+ }
+ }
+ );
+
+ return [assemblyaiModule];
+ }
+
+ public manualPatch(assemblyai: any): void {
+ this._patch(assemblyai);
+ }
+
+ protected _patch(moduleExports: any, moduleVersion?: string) {
+ try {
+ const OriginalAssemblyAIClient = moduleExports.AssemblyAI;
+ if (!OriginalAssemblyAIClient || typeof OriginalAssemblyAIClient !== 'function') {
+ return;
+ }
+
+ if (isWrapped(OriginalAssemblyAIClient)) {
+ this._unwrap(moduleExports, 'AssemblyAI');
+ }
+
+ const tracer = this.tracer;
+ const sdkVersion = moduleVersion ? String(moduleVersion) : undefined;
+
+ // Capture instance helpers in closures so the wrapped constructor (a plain
+ // function expression, required to carry the original's static surface)
+ // never needs to alias `this`.
+ const wrap = this._wrap.bind(this);
+ const rememberTranscriptsProto = (proto: any) => {
+ this._transcriptsProto = proto;
+ };
+
+ this._wrap(moduleExports, 'AssemblyAI', (original: (...args: any[]) => any) => {
+ const WrappedAssemblyAI = function (this: any, ...clientArgs: any[]) {
+ const client = new (original as any)(...clientArgs);
+
+ if (client && client.transcripts) {
+ const transcriptsProto = Object.getPrototypeOf(client.transcripts);
+ if (transcriptsProto) {
+ rememberTranscriptsProto(transcriptsProto);
+
+ for (const methodName of TRANSCRIBE_METHODS) {
+ if (
+ typeof transcriptsProto[methodName] === 'function' &&
+ !isWrapped(transcriptsProto[methodName])
+ ) {
+ wrap(
+ transcriptsProto,
+ methodName,
+ AssemblyAIWrapper._patchTranscribe(tracer, methodName, sdkVersion)
+ );
+ }
+ }
+ }
+ }
+
+ return client;
+ };
+
+ // Preserve the original constructor's static surface (static methods,
+ // properties and metadata) so consumers relying on them still work.
+ Object.setPrototypeOf(WrappedAssemblyAI, original);
+ Object.assign(WrappedAssemblyAI, original);
+ WrappedAssemblyAI.prototype = (original as any).prototype;
+
+ return WrappedAssemblyAI;
+ });
+ } catch (e) {
+ diag.error('assemblyai instrumentation: error in _patch method', e);
+ }
+ }
+
+ protected _unpatch(moduleExports: any) {
+ if (moduleExports.AssemblyAI) {
+ this._unwrap(moduleExports, 'AssemblyAI');
+ }
+ if (this._transcriptsProto) {
+ for (const methodName of TRANSCRIBE_METHODS) {
+ if (isWrapped(this._transcriptsProto[methodName])) {
+ this._unwrap(this._transcriptsProto, methodName);
+ }
+ }
+ }
+ }
+}
diff --git a/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts b/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts
new file mode 100644
index 00000000..16a3f5d6
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts
@@ -0,0 +1,273 @@
+import { Span, SpanKind, Tracer, context, trace, Attributes } from '@opentelemetry/api';
+import OpenlitConfig from '../../config';
+import OpenLitHelper, {
+ isFrameworkLlmActive,
+ getFrameworkParentContext,
+} from '../../helpers';
+import SemanticConvention from '../../semantic-convention';
+import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
+
+function spanCreationAttrs(
+ operationName: string,
+ requestModel: string,
+ serverAddress: string,
+ serverPort: number
+): Attributes {
+ return {
+ [SemanticConvention.GEN_AI_OPERATION]: operationName,
+ [SemanticConvention.GEN_AI_PROVIDER_NAME_OTEL]: AssemblyAIWrapper.aiSystem,
+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,
+ [SemanticConvention.SERVER_ADDRESS]: serverAddress,
+ [SemanticConvention.SERVER_PORT]: serverPort,
+ };
+}
+
+class AssemblyAIWrapper extends BaseWrapper {
+ static aiSystem = SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI;
+ static serverAddress = 'api.assemblyai.com';
+ static serverPort = 443;
+
+ /**
+ * Parse the request arguments of `client.transcripts.transcribe` / `submit` / `get`.
+ * AssemblyAI accepts either a params object (`{ audio, speechModel, ... }`) or, for
+ * `get`, a transcript id string. Mirrors the Python instrumentor which reads
+ * `speech_model` from kwargs (default "best").
+ */
+ static _parseAudioArgs(args: any[]): {
+ options: Record<string, any>;
+ requestModel: string;
+ audioUrl: string;
+ } {
+ const options =
+ typeof args[0] === 'object' && args[0] !== null ? args[0] : {};
+ const requestModel =
+ options.speechModel || options.speech_model || 'best';
+ const rawAudioUrl =
+ typeof args[0] === 'string'
+ ? args[0]
+ : options.audio ?? options.audioUrl ?? options.audio_url;
+
+ // Only stringify when a real value is present, so undefined/null never
+ // becomes the literal string 'undefined'/'null'.
+ const audioUrl =
+ rawAudioUrl === undefined || rawAudioUrl === null ? '' : String(rawAudioUrl);
+
+ return { options, requestModel, audioUrl };
+ }
+
+ /**
+ * Prefer the model stamped on the Transcript response (e.g. transcripts.get
+ * polls only pass a transcript id). Fall back to request args, then "best".
+ */
+ static _resolveRequestModel(args: any[], response: any): string {
+ const { requestModel } = AssemblyAIWrapper._parseAudioArgs(args);
+ const responseModel =
+ response?.speech_model ??
+ response?.speechModel ??
+ response?.config?.speech_model ??
+ response?.config?.speechModel;
+ if (typeof responseModel === 'string' && responseModel.length > 0) {
+ return responseModel;
+ }
+ return requestModel;
+ }
+
+ static _patchTranscribe(tracer: Tracer, methodName: string, sdkVersion?: string): any {
+ const genAIEndpoint = `assemblyai.transcripts.${methodName}`;
+ return (originalMethod: (...args: any[]) => any) => {
+ return async function (this: any, ...args: any[]) {
+ if (isFrameworkLlmActive()) return originalMethod.apply(this, args);
+
+ const { requestModel } = AssemblyAIWrapper._parseAudioArgs(args);
+
+ const spanName = `${SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} ${requestModel}`;
+ const effectiveCtx = getFrameworkParentContext() ?? context.active();
+ const span = tracer.startSpan(
+ spanName,
+ {
+ kind: SpanKind.CLIENT,
+ attributes: spanCreationAttrs(
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ requestModel,
+ AssemblyAIWrapper.serverAddress,
+ AssemblyAIWrapper.serverPort
+ ),
+ },
+ effectiveCtx
+ );
+
+ return context.with(trace.setSpan(effectiveCtx, span), async () => {
+ let metricParams;
+ const startTime = Date.now();
+ try {
+ const response = await originalMethod.apply(this, args);
+ const ttft = (Date.now() - startTime) / 1000;
+
+ metricParams = AssemblyAIWrapper._commonAudioSetter({
+ args,
+ genAIEndpoint,
+ response,
+ span,
+ ttft,
+ tbt: 0,
+ isStream: false,
+ sdkVersion,
+ });
+
+ return response;
+ } catch (e: any) {
+ OpenLitHelper.handleException(span, e);
+ BaseWrapper.recordMetrics(span, {
+ genAIEndpoint,
+ model: requestModel,
+ aiSystem: AssemblyAIWrapper.aiSystem,
+ serverAddress: AssemblyAIWrapper.serverAddress,
+ serverPort: AssemblyAIWrapper.serverPort,
+ errorType: e?.constructor?.name || '_OTHER',
+ });
+ throw e;
+ } finally {
+ span.end();
+ if (metricParams) {
+ BaseWrapper.recordMetrics(span, metricParams);
+ }
+ }
+ });
+ };
+ };
+ }
+
+ static _commonAudioSetter({
+ args,
+ genAIEndpoint,
+ response,
+ span,
+ ttft = 0,
+ tbt = 0,
+ isStream = false,
+ sdkVersion,
+ }: {
+ args: any[];
+ genAIEndpoint: string;
+ response: any;
+ span: Span;
+ ttft?: number;
+ tbt?: number;
+ isStream?: boolean;
+ sdkVersion?: string;
+ }): BaseSpanAttributes {
+ const captureContent = OpenlitConfig.captureMessageContent;
+
+ const { audioUrl } = AssemblyAIWrapper._parseAudioArgs(args);
+ const requestModel = AssemblyAIWrapper._resolveRequestModel(args, response);
+
+ // Coerce to a string only when a real value is present, so undefined/null
+ // never becomes the literal string 'undefined'/'null'.
+ const toStr = (v: unknown): string =>
+ v === undefined || v === null ? '' : String(v);
+
+ // Prefer values from the returned Transcript (matches the Python instrumentor,
+ // which reads audio_url / audio_duration / id / text off the response).
+ // Do not treat a bare transcript id (transcripts.get) as an audio URL for
+ // cost or message content — Python uses response.audio_url only.
+ const argsAudioPrompt =
+ typeof args[0] === 'object' && args[0] !== null ? audioUrl : '';
+ const prompt = toStr(
+ response?.audio_url ?? response?.audioUrl ?? argsAudioPrompt
+ );
+ const audioDuration =
+ response?.audio_duration ?? response?.audioDuration ?? 0;
+ const responseId = toStr(response?.id);
+ const responseText = toStr(response?.text);
+
+ // Request parameters
+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_DURATION, audioDuration);
+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, isStream);
+
+ // Response parameters
+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, 'text');
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_ID, responseId);
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, ['stop']);
+
+ // Token usage (AssemblyAI transcription does not report token usage)
+ const usage = response?.usage;
+ const inputTokens = (usage && (usage.input_tokens ?? usage.inputTokens)) || 0;
+ const outputTokens = (usage && (usage.output_tokens ?? usage.outputTokens)) || 0;
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, inputTokens + outputTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, 0);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, 0);
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
+
+ const pricingInfo = OpenlitConfig.pricingInfo || {};
+ const cost = OpenLitHelper.getAudioModelCost(
+ requestModel,
+ pricingInfo,
+ prompt,
+ audioDuration
+ );
+
+ AssemblyAIWrapper.setBaseSpanAttributes(span, {
+ genAIEndpoint,
+ model: requestModel,
+ cost,
+ aiSystem: AssemblyAIWrapper.aiSystem,
+ serverAddress: AssemblyAIWrapper.serverAddress,
+ serverPort: AssemblyAIWrapper.serverPort,
+ });
+
+ // Python stamps gen_ai.system and the AssemblyAI package version (not OpenLIT's).
+ span.setAttribute(SemanticConvention.GEN_AI_PROVIDER_NAME, AssemblyAIWrapper.aiSystem);
+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, requestModel);
+ if (sdkVersion) {
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, sdkVersion);
+ }
+
+ let inputMessagesJson: string | undefined;
+ let outputMessagesJson: string | undefined;
+
+ if (captureContent) {
+ inputMessagesJson = OpenLitHelper.buildInputMessages([{ role: 'user', content: prompt }]);
+ outputMessagesJson = OpenLitHelper.buildOutputMessages(responseText, 'stop');
+
+ span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, inputMessagesJson);
+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
+ }
+
+ // Match Python assemblyai: inference events are emitted only when content
+ // capture is enabled (messages are part of the event payload).
+ if (!OpenlitConfig.disableEvents && captureContent) {
+ const eventAttrs: Attributes = {
+ [SemanticConvention.GEN_AI_OPERATION]: SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,
+ [SemanticConvention.GEN_AI_RESPONSE_MODEL]: requestModel,
+ [SemanticConvention.SERVER_ADDRESS]: AssemblyAIWrapper.serverAddress,
+ [SemanticConvention.SERVER_PORT]: AssemblyAIWrapper.serverPort,
+ [SemanticConvention.GEN_AI_OUTPUT_TYPE]: 'text',
+ [SemanticConvention.GEN_AI_RESPONSE_ID]: responseId,
+ [SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
+ [SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
+ };
+ if (inputMessagesJson) {
+ eventAttrs[SemanticConvention.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
+ }
+ if (outputMessagesJson) {
+ eventAttrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
+ }
+ OpenLitHelper.emitInferenceEvent(span, eventAttrs);
+ }
+
+ return {
+ genAIEndpoint,
+ model: requestModel,
+ cost,
+ aiSystem: AssemblyAIWrapper.aiSystem,
+ serverAddress: AssemblyAIWrapper.serverAddress,
+ serverPort: AssemblyAIWrapper.serverPort,
+ };
+ }
+}
+
+export default AssemblyAIWrapper;
diff --git a/sdk/typescript/src/instrumentation/index.ts b/sdk/typescript/src/instrumentation/index.ts
index 74802051..5eb7fdac 100644
--- a/sdk/typescript/src/instrumentation/index.ts
+++ b/sdk/typescript/src/instrumentation/index.ts
@@ -34,6 +34,7 @@ import AstraInstrumentation from './astra';
import MCPInstrumentation from './mcp';
import Mem0Instrumentation from './mem0';
import ElevenLabsInstrumentation from './elevenlabs';
+import AssemblyAIInstrumentation from './assemblyai';
import TransformersInstrumentation from './transformers';
/**
@@ -111,6 +112,7 @@ export default class Instrumentations {
mcp: new MCPInstrumentation(),
mem0: new Mem0Instrumentation(),
elevenlabs: new ElevenLabsInstrumentation(),
+ assemblyai: new AssemblyAIInstrumentation(),
transformers: new TransformersInstrumentation(),
};
diff --git a/sdk/typescript/src/semantic-convention.ts b/sdk/typescript/src/semantic-convention.ts
index ae13c4f6..0ab33cf3 100644
--- a/sdk/typescript/src/semantic-convention.ts
+++ b/sdk/typescript/src/semantic-convention.ts
@@ -86,6 +86,7 @@ export default class SemanticConvention {
static GEN_AI_REQUEST_TOOL_CHOICE = 'gen_ai.request.tool_choice';
static GEN_AI_REQUEST_AUDIO_VOICE = 'gen_ai.request.audio_voice';
static GEN_AI_REQUEST_AUDIO_SETTINGS = 'gen_ai.request.audio_settings';
+ static GEN_AI_REQUEST_AUDIO_DURATION = 'gen_ai.request.audio_duration';
static GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT = 'gen_ai.request.audio_response_format';
static GEN_AI_REQUEST_AUDIO_SPEED = 'gen_ai.request.audio_speed';
static GEN_AI_REQUEST_FINETUNE_STATUS = 'gen_ai.request.fine_tune_status';
@@ -247,6 +248,7 @@ export default class SemanticConvention {
static GEN_AI_SYSTEM_STRANDS = 'strands_agents';
static GEN_AI_SYSTEM_CURSOR = 'cursor';
static GEN_AI_SYSTEM_ELEVENLABS = 'elevenlabs';
+ static GEN_AI_SYSTEM_ASSEMBLYAI = 'assemblyai';
static GEN_AI_SYSTEM_MCP = 'mcp';
static GEN_AI_SYSTEM_MEM0 = 'mem0';
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index 3ca33794..d2059150 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -3,7 +3,7 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { metrics } from '@opentelemetry/api';
import type { Guard } from './guard/base';
-export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp' | 'mem0' | 'elevenlabs' | 'transformers';
+export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp' | 'mem0' | 'elevenlabs' | 'assemblyai' | 'transformers';
export type OpenlitInstrumentations = Partial<Record<InstrumentationType, any>>;