Skip to content

Commit 2163d22

Browse files
kraenhansenclaude
andcommitted
fix: use urllib.parse.urlencode in realtime_tts and conversation URLs
Apply the same urlencode fix to the remaining manual query string construction sites in realtime_tts.py and conversation.py. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1674698 commit 2163d22

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/elevenlabs/conversational_ai/conversation.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,14 @@ def _get_wss_url(self):
430430
# Ensure base URL ends with '/' for proper joining
431431
if not base_ws_url.endswith("/"):
432432
base_ws_url += "/"
433-
url = f"{base_ws_url}v1/convai/conversation?agent_id={self.agent_id}&source=python_sdk&version={__version__}"
433+
params: list[tuple[str, str]] = [
434+
("agent_id", self.agent_id),
435+
("source", "python_sdk"),
436+
("version", __version__),
437+
]
434438
if self.environment:
435-
url += f"&environment={self.environment}"
436-
return url
439+
params.append(("environment", self.environment))
440+
return f"{base_ws_url}v1/convai/conversation?{urllib.parse.urlencode(params)}"
437441

438442
def _get_signed_url(self):
439443
response = self.client.conversational_ai.conversations.get_signed_url(
@@ -442,8 +446,10 @@ def _get_signed_url(self):
442446
)
443447
signed_url = response.signed_url
444448
# Append source and version query parameters to the signed URL
445-
separator = "&" if "?" in signed_url else "?"
446-
return f"{signed_url}{separator}source=python_sdk&version={__version__}"
449+
parsed = urllib.parse.urlparse(signed_url)
450+
existing_params = urllib.parse.parse_qsl(parsed.query)
451+
existing_params.extend([("source", "python_sdk"), ("version", __version__)])
452+
return urllib.parse.urlunparse(parsed._replace(query=urllib.parse.urlencode(existing_params)))
447453

448454
def _create_on_prem_initiation_message(self):
449455
return json.dumps(

src/elevenlabs/realtime_tts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_text() -> typing.Iterator[str]:
9494
with connect(
9595
urllib.parse.urljoin(
9696
self._ws_base_url,
97-
f"v1/text-to-speech/{jsonable_encoder(voice_id)}/stream-input?model_id={model_id}&output_format={output_format}"
97+
f"v1/text-to-speech/{jsonable_encoder(voice_id)}/stream-input?{urllib.parse.urlencode({"model_id": model_id, "output_format": output_format})}"
9898
),
9999
additional_headers=jsonable_encoder(
100100
remove_none_from_dict(

0 commit comments

Comments
 (0)