Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-deepgram-query-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/agents-plugin-deepgram": patch
---

fix(deepgram): avoid double-encoding STT query params
25 changes: 25 additions & 0 deletions plugins/deepgram/src/_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { describe, expect, it } from 'vitest';
import { appendQueryParams } from './_utils.js';

describe('appendQueryParams', () => {
it('preserves non-ASCII keyterm and keywords values in the websocket URL', () => {
const url = new URL('wss://api.deepgram.com/v1/listen');

appendQueryParams(url, {
keyterm: ['słucham', 'dzień dobry', 'znamię', 'dziękuję'],
keywords: ['potwierdź:3', 'żółć:1.5'],
});

expect(url.searchParams.getAll('keyterm')).toEqual([
'słucham',
'dzień dobry',
'znamię',
'dziękuję',
]);
expect(url.searchParams.getAll('keywords')).toEqual(['potwierdź:3', 'żółć:1.5']);
expect(url.toString()).not.toContain('%25');
});
});
19 changes: 19 additions & 0 deletions plugins/deepgram/src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ export class PeriodicCollector<T> {
this.lastFlushTime = performance.now() / 1000;
}
}

type QueryParamScalar = string | number | boolean;
type QueryParamValue = QueryParamScalar | QueryParamScalar[];

export function appendQueryParams(
url: URL,
params: Record<string, QueryParamValue | undefined>,
): void {
Object.entries(params).forEach(([key, value]) => {
if (value === undefined) return;

if (Array.isArray(value)) {
value.forEach((item) => url.searchParams.append(key, String(item)));
return;
}

url.searchParams.append(key, String(value));
});
}
12 changes: 2 additions & 10 deletions plugins/deepgram/src/stt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@livekit/agents';
import type { AudioFrame } from '@livekit/rtc-node';
import { WebSocket } from 'ws';
import { PeriodicCollector } from './_utils.js';
import { PeriodicCollector, appendQueryParams } from './_utils.js';
import type { STTLanguages, STTModels } from './models.js';

export interface STTOptions {
Expand Down Expand Up @@ -195,15 +195,7 @@ export class SpeechStream extends stt.SpeechStream {
language: this.#opts.language,
mip_opt_out: this.#opts.mipOptOut,
};
Object.entries(params).forEach(([k, v]) => {
if (v !== undefined) {
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {
streamURL.searchParams.append(k, encodeURIComponent(v));
} else {
v.forEach((x) => streamURL.searchParams.append(k, encodeURIComponent(x)));
}
}
});
appendQueryParams(streamURL, params);

ws = new WebSocket(streamURL, {
headers: { Authorization: `Token ${this.#opts.apiKey}` },
Expand Down