@livekit/agents-plugin-openai 1.2.6 → 1.2.7

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.
Files changed (43) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/realtime/index.cjs +0 -19
  4. package/dist/realtime/index.cjs.map +1 -1
  5. package/dist/realtime/index.d.cts +0 -1
  6. package/dist/realtime/index.d.ts +0 -1
  7. package/dist/realtime/index.d.ts.map +1 -1
  8. package/dist/realtime/index.js +0 -4
  9. package/dist/realtime/index.js.map +1 -1
  10. package/dist/realtime/realtime_model.cjs +67 -23
  11. package/dist/realtime/realtime_model.cjs.map +1 -1
  12. package/dist/realtime/realtime_model.d.cts +1 -1
  13. package/dist/realtime/realtime_model.d.ts +1 -1
  14. package/dist/realtime/realtime_model.d.ts.map +1 -1
  15. package/dist/realtime/realtime_model.js +67 -23
  16. package/dist/realtime/realtime_model.js.map +1 -1
  17. package/dist/responses/llm.cjs +3 -1
  18. package/dist/responses/llm.cjs.map +1 -1
  19. package/dist/responses/llm.d.cts +2 -0
  20. package/dist/responses/llm.d.ts +2 -0
  21. package/dist/responses/llm.d.ts.map +1 -1
  22. package/dist/responses/llm.js +3 -1
  23. package/dist/responses/llm.js.map +1 -1
  24. package/dist/ws/llm.cjs +3 -0
  25. package/dist/ws/llm.cjs.map +1 -1
  26. package/dist/ws/llm.d.cts +2 -0
  27. package/dist/ws/llm.d.ts +2 -0
  28. package/dist/ws/llm.d.ts.map +1 -1
  29. package/dist/ws/llm.js +3 -0
  30. package/dist/ws/llm.js.map +1 -1
  31. package/package.json +5 -5
  32. package/src/realtime/index.ts +0 -1
  33. package/src/realtime/realtime_model.ts +89 -30
  34. package/src/responses/llm.ts +6 -1
  35. package/src/ws/llm.ts +6 -0
  36. package/dist/realtime/realtime_model_beta.cjs +0 -1300
  37. package/dist/realtime/realtime_model_beta.cjs.map +0 -1
  38. package/dist/realtime/realtime_model_beta.d.cts +0 -164
  39. package/dist/realtime/realtime_model_beta.d.ts +0 -164
  40. package/dist/realtime/realtime_model_beta.d.ts.map +0 -1
  41. package/dist/realtime/realtime_model_beta.js +0 -1281
  42. package/dist/realtime/realtime_model_beta.js.map +0 -1
  43. package/src/realtime/realtime_model_beta.ts +0 -1663
@@ -234,7 +234,7 @@ export class RealtimeModel extends llm.RealtimeModel {
234
234
  *
235
235
  * @param azureDeployment - The name of your Azure OpenAI deployment.
236
236
  * @param azureEndpoint - The endpoint URL for your Azure OpenAI resource. If undefined, will attempt to read from the environment variable AZURE_OPENAI_ENDPOINT.
237
- * @param apiVersion - API version to use with Azure OpenAI Service. If undefined, will attempt to read from the environment variable OPENAI_API_VERSION.
237
+ * @param apiVersion - **Deprecated.** API version for legacy Azure OpenAI preview models. Will be removed on April 30, 2026. Omit for GA models.
238
238
  * @param apiKey - Azure OpenAI API key. If undefined, will attempt to read from the environment variable AZURE_OPENAI_API_KEY.
239
239
  * @param entraToken - Azure Entra authentication token. Required if not using API key authentication.
240
240
  * @param baseURL - Base URL for the API endpoint. If undefined, constructed from the azure_endpoint.
@@ -287,9 +287,10 @@ export class RealtimeModel extends llm.RealtimeModel {
287
287
  }
288
288
 
289
289
  apiVersion = apiVersion || process.env.OPENAI_API_VERSION;
290
- if (!apiVersion) {
291
- throw new Error(
292
- 'Must provide either the `apiVersion` argument or the `OPENAI_API_VERSION` environment variable',
290
+ if (apiVersion) {
291
+ log().warn(
292
+ 'The `apiVersion` parameter for Azure OpenAI Realtime is deprecated and will be removed on April 30, 2026. ' +
293
+ 'Please use the newer Azure OpenAI Realtime API without specifying an API version.',
293
294
  );
294
295
  }
295
296
 
@@ -327,6 +328,24 @@ export class RealtimeModel extends llm.RealtimeModel {
327
328
  }
328
329
  }
329
330
 
331
+ /**
332
+ * In-place normalization of client event dicts for legacy Azure compatibility.
333
+ *
334
+ * The legacy Azure Realtime API uses "text" for assistant content parts,
335
+ * while the newer OpenAI API uses "output_text".
336
+ */
337
+ function normalizeAzureClientEvent(event: Record<string, unknown>): void {
338
+ const item = event['item'] as Record<string, unknown> | undefined;
339
+ if (!item) return;
340
+ const content = item['content'] as Record<string, unknown>[] | undefined;
341
+ if (!content) return;
342
+ for (const contentPart of content) {
343
+ if (contentPart['type'] === 'output_text') {
344
+ contentPart['type'] = 'text';
345
+ }
346
+ }
347
+ }
348
+
330
349
  function processBaseURL({
331
350
  baseURL,
332
351
  model,
@@ -340,7 +359,9 @@ function processBaseURL({
340
359
  azureDeployment?: string;
341
360
  apiVersion?: string;
342
361
  }): string {
343
- const url = new URL([baseURL, 'realtime'].join('/'));
362
+ // Azure GA (no apiVersion) uses /v1/realtime; legacy preview uses /realtime
363
+ const realtimePath = isAzure && !apiVersion ? 'v1/realtime' : 'realtime';
364
+ const url = new URL([baseURL, realtimePath].join('/'));
344
365
 
345
366
  if (url.protocol === 'https:') {
346
367
  url.protocol = 'wss:';
@@ -354,14 +375,19 @@ function processBaseURL({
354
375
  }
355
376
 
356
377
  const queryParams: Record<string, string> = {};
357
- if (isAzure) {
358
- if (apiVersion) {
359
- queryParams['api-version'] = apiVersion;
360
- }
378
+ if (isAzure && apiVersion) {
379
+ // Legacy Azure preview: /realtime?api-version=<v>&deployment=<d>
380
+ queryParams['api-version'] = apiVersion;
361
381
  if (azureDeployment) {
362
382
  queryParams['deployment'] = azureDeployment;
363
383
  }
384
+ } else if (isAzure) {
385
+ // GA Azure: /v1/realtime?model=<deployment>
386
+ if (azureDeployment) {
387
+ queryParams['model'] = azureDeployment;
388
+ }
364
389
  } else {
390
+ // Standard OpenAI: /realtime?model=<model>
365
391
  queryParams['model'] = model;
366
392
  }
367
393
 
@@ -434,37 +460,58 @@ export class RealtimeSession extends llm.RealtimeSession {
434
460
  }
435
461
 
436
462
  private createSessionUpdateEvent(): api_proto.SessionUpdateEvent {
437
- const audioFormat: api_proto.AudioFormat = { type: 'audio/pcm', rate: SAMPLE_RATE };
438
-
439
- const modality: Modality = this.oaiRealtimeModel._options.modalities.includes('audio')
440
- ? 'audio'
441
- : 'text';
463
+ const opts = this.oaiRealtimeModel._options;
464
+ const maxOutputTokens =
465
+ opts.maxResponseOutputTokens === Infinity ? 'inf' : opts.maxResponseOutputTokens;
466
+
467
+ if (opts.isAzure && opts.apiVersion) {
468
+ // Legacy Azure preview API: flat format
469
+ const modalities: Modality[] = opts.modalities.includes('audio')
470
+ ? ['text', 'audio']
471
+ : ['text'];
472
+ return {
473
+ type: 'session.update',
474
+ session: {
475
+ model: opts.model,
476
+ voice: opts.voice,
477
+ input_audio_format: 'pcm16',
478
+ output_audio_format: 'pcm16',
479
+ modalities,
480
+ turn_detection: opts.turnDetection,
481
+ input_audio_transcription: opts.inputAudioTranscription,
482
+ tool_choice: toOaiToolChoice(opts.toolChoice),
483
+ max_response_output_tokens: maxOutputTokens,
484
+ speed: opts.speed,
485
+ instructions: this.instructions,
486
+ },
487
+ };
488
+ }
442
489
 
490
+ // GA format (OpenAI or Azure GA)
491
+ const audioFormat: api_proto.AudioFormat = { type: 'audio/pcm', rate: SAMPLE_RATE };
492
+ const modality: Modality = opts.modalities.includes('audio') ? 'audio' : 'text';
443
493
  return {
444
494
  type: 'session.update',
445
495
  session: {
446
496
  type: 'realtime',
447
- model: this.oaiRealtimeModel._options.model,
497
+ model: opts.model,
448
498
  output_modalities: [modality],
449
499
  audio: {
450
500
  input: {
451
501
  format: audioFormat,
452
- noise_reduction: this.oaiRealtimeModel._options.inputAudioNoiseReduction,
453
- transcription: this.oaiRealtimeModel._options.inputAudioTranscription,
454
- turn_detection: this.oaiRealtimeModel._options.turnDetection,
502
+ noise_reduction: opts.inputAudioNoiseReduction,
503
+ transcription: opts.inputAudioTranscription,
504
+ turn_detection: opts.turnDetection,
455
505
  },
456
506
  output: {
457
507
  format: audioFormat,
458
- speed: this.oaiRealtimeModel._options.speed,
459
- voice: this.oaiRealtimeModel._options.voice,
508
+ speed: opts.speed,
509
+ voice: opts.voice,
460
510
  },
461
511
  },
462
- max_output_tokens:
463
- this.oaiRealtimeModel._options.maxResponseOutputTokens === Infinity
464
- ? 'inf'
465
- : this.oaiRealtimeModel._options.maxResponseOutputTokens,
466
- tool_choice: toOaiToolChoice(this.oaiRealtimeModel._options.toolChoice),
467
- tracing: this.oaiRealtimeModel._options.tracing,
512
+ max_output_tokens: maxOutputTokens,
513
+ tool_choice: toOaiToolChoice(opts.toolChoice),
514
+ tracing: opts.tracing,
468
515
  instructions: this.instructions,
469
516
  },
470
517
  };
@@ -675,10 +722,12 @@ export class RealtimeSession extends llm.RealtimeSession {
675
722
  }
676
723
  }
677
724
 
725
+ const isLegacyAzure =
726
+ this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
678
727
  return {
679
728
  type: 'session.update',
680
729
  session: {
681
- type: 'realtime',
730
+ ...(!isLegacyAzure && { type: 'realtime' }),
682
731
  model: this.oaiRealtimeModel._options.model,
683
732
  tools: oaiTools,
684
733
  },
@@ -687,11 +736,13 @@ export class RealtimeSession extends llm.RealtimeSession {
687
736
  }
688
737
 
689
738
  async updateInstructions(_instructions: string): Promise<void> {
739
+ const isLegacyAzure =
740
+ this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
690
741
  const eventId = shortuuid('instructions_update_');
691
742
  this.sendEvent({
692
743
  type: 'session.update',
693
744
  session: {
694
- type: 'realtime',
745
+ ...(!isLegacyAzure && { type: 'realtime' }),
695
746
  instructions: _instructions,
696
747
  },
697
748
  event_id: eventId,
@@ -707,8 +758,10 @@ export class RealtimeSession extends llm.RealtimeSession {
707
758
  return;
708
759
  }
709
760
 
761
+ const isLegacyAzure =
762
+ this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
710
763
  const options: api_proto.SessionUpdateEvent['session'] = {
711
- type: 'realtime',
764
+ ...(!isLegacyAzure && { type: 'realtime' }),
712
765
  };
713
766
 
714
767
  this.oaiRealtimeModel._options.toolChoice = toolChoice;
@@ -952,6 +1005,9 @@ export class RealtimeSession extends llm.RealtimeSession {
952
1005
  try {
953
1006
  for (const ev of events) {
954
1007
  this.emit('openai_client_event_queued', ev);
1008
+ if (this.oaiRealtimeModel._options.isAzure && this.oaiRealtimeModel._options.apiVersion) {
1009
+ normalizeAzureClientEvent(ev as unknown as Record<string, unknown>);
1010
+ }
955
1011
  wsConn!.send(JSON.stringify(ev));
956
1012
  }
957
1013
  } catch (error) {
@@ -1042,6 +1098,9 @@ export class RealtimeSession extends llm.RealtimeSession {
1042
1098
  }
1043
1099
 
1044
1100
  this.emit('openai_client_event_queued', event);
1101
+ if (this.oaiRealtimeModel._options.isAzure && this.oaiRealtimeModel._options.apiVersion) {
1102
+ normalizeAzureClientEvent(event as unknown as Record<string, unknown>);
1103
+ }
1045
1104
  wsConn.send(JSON.stringify(event));
1046
1105
  } catch (error) {
1047
1106
  break;
@@ -1823,7 +1882,7 @@ function openAIItemToLivekitItem(item: api_proto.ItemResource): llm.ChatItem {
1823
1882
  // item.content can be a single object or an array; normalize to array
1824
1883
  const contents = Array.isArray(item.content) ? item.content : [item.content];
1825
1884
  for (const c of contents) {
1826
- if (c.type === 'text' || c.type === 'input_text') {
1885
+ if (c.type === 'text' || c.type === 'input_text' || c.type === 'output_text') {
1827
1886
  content.push(c.text);
1828
1887
  } else if (c.type === 'input_image' && (c as api_proto.InputImageContent).image_url) {
1829
1888
  content.push(
@@ -28,6 +28,8 @@ export interface LLMOptions {
28
28
  strictToolSchema?: boolean;
29
29
  /** Specifies the processing tier (e.g. 'auto', 'default', 'priority', 'flex'). */
30
30
  serviceTier?: string;
31
+ /** Upper bound for the number of tokens that can be generated for a response. */
32
+ maxOutputTokens?: number;
31
33
 
32
34
  /**
33
35
  * Whether to use the WebSocket API.
@@ -120,6 +122,10 @@ class ResponsesHttpLLM extends llm.LLM {
120
122
  modelOptions.service_tier = this.#opts.serviceTier;
121
123
  }
122
124
 
125
+ if (this.#opts.maxOutputTokens !== undefined) {
126
+ modelOptions.max_output_tokens = this.#opts.maxOutputTokens;
127
+ }
128
+
123
129
  return new ResponsesHttpLLMStream(this, {
124
130
  model: this.#opts.model,
125
131
  client: this.#client,
@@ -394,7 +400,6 @@ export class LLM extends llm.LLM {
394
400
  this.#llm.prewarm();
395
401
  }
396
402
 
397
- // Ref: python livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/responses/llm.py - 229-233 lines
398
403
  override async aclose(): Promise<void> {
399
404
  await this.#llm.aclose();
400
405
  }
package/src/ws/llm.ts CHANGED
@@ -143,6 +143,8 @@ export interface WSLLMOptions {
143
143
  strictToolSchema?: boolean;
144
144
  /** Specifies the processing tier (e.g. 'auto', 'default', 'priority', 'flex'). */
145
145
  serviceTier?: string;
146
+ /** Upper bound for the number of tokens that can be generated for a response. */
147
+ maxOutputTokens?: number;
146
148
  }
147
149
 
148
150
  const defaultLLMOptions: WSLLMOptions = {
@@ -272,6 +274,10 @@ export class WSLLM extends llm.LLM {
272
274
  modelOptions.service_tier = this.#opts.serviceTier;
273
275
  }
274
276
 
277
+ if (this.#opts.maxOutputTokens !== undefined) {
278
+ modelOptions.max_output_tokens = this.#opts.maxOutputTokens;
279
+ }
280
+
275
281
  let inputChatCtx = chatCtx;
276
282
  let prevResponseId: string | undefined;
277
283
  const canUseStoredResponse = modelOptions.store !== false;