Skip to content

Commit 5118750

Browse files
authored
Add previous_text and all event types (#695)
1 parent b0fc2bb commit 5118750

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/elevenlabs/realtime/connection.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ class RealtimeEvents(str, Enum):
1616
ERROR = "error"
1717
AUTH_ERROR = "auth_error"
1818
QUOTA_EXCEEDED = "quota_exceeded"
19+
COMMIT_THROTTLED = "commit_throttled"
20+
TRANSCRIBER_ERROR = "transcriber_error"
21+
UNACCEPTED_TERMS_ERROR = "unaccepted_terms_error"
22+
RATE_LIMITED = "rate_limited"
23+
INPUT_ERROR = "input_error"
24+
QUEUE_OVERFLOW = "queue_overflow"
25+
RESOURCE_EXHAUSTED = "resource_exhausted"
26+
SESSION_TIME_LIMIT_EXCEEDED = "session_time_limit_exceeded"
27+
CHUNK_SIZE_EXCEEDED = "chunk_size_exceeded"
28+
INSUFFICIENT_AUDIO_ACTIVITY = "insufficient_audio_activity"
29+
1930

2031

2132
class RealtimeConnection:
@@ -92,6 +103,24 @@ async def _start_message_handler(self) -> None:
92103
try:
93104
event = RealtimeEvents(message_type)
94105
self._emit(event, data)
106+
107+
# Also emit generic ERROR event for specific error types
108+
error_events = {
109+
RealtimeEvents.AUTH_ERROR,
110+
RealtimeEvents.QUOTA_EXCEEDED,
111+
RealtimeEvents.COMMIT_THROTTLED,
112+
RealtimeEvents.TRANSCRIBER_ERROR,
113+
RealtimeEvents.UNACCEPTED_TERMS_ERROR,
114+
RealtimeEvents.RATE_LIMITED,
115+
RealtimeEvents.INPUT_ERROR,
116+
RealtimeEvents.QUEUE_OVERFLOW,
117+
RealtimeEvents.RESOURCE_EXHAUSTED,
118+
RealtimeEvents.SESSION_TIME_LIMIT_EXCEEDED,
119+
RealtimeEvents.CHUNK_SIZE_EXCEEDED,
120+
RealtimeEvents.INSUFFICIENT_AUDIO_ACTIVITY,
121+
}
122+
if event in error_events:
123+
self._emit(RealtimeEvents.ERROR, data)
95124
except ValueError:
96125
# Unknown message type, ignore
97126
pass
@@ -107,7 +136,10 @@ async def send(self, data: typing.Dict[str, typing.Any]) -> None:
107136
Send an audio chunk to the server for transcription.
108137
109138
Args:
110-
data: Dictionary containing audio_base_64 key with base64-encoded audio
139+
data: Dictionary containing the following keys:
140+
- audio_base_64 (str): Base64-encoded audio data to transcribe
141+
- previous_text (str, optional): Previous transcript text to provide context
142+
for more accurate transcription
111143
112144
Raises:
113145
RuntimeError: If the WebSocket connection is not open
@@ -118,6 +150,12 @@ async def send(self, data: typing.Dict[str, typing.Any]) -> None:
118150
connection.send({
119151
"audio_base_64": base64_encoded_audio
120152
})
153+
154+
# Send audio chunk with context - can only be sent with the first chunk of audio
155+
connection.send({
156+
"audio_base_64": base64_encoded_audio,
157+
"previous_text": "Previously transcribed text for context"
158+
})
121159
```
122160
"""
123161
if not self.websocket:
@@ -128,6 +166,7 @@ async def send(self, data: typing.Dict[str, typing.Any]) -> None:
128166
"audio_base_64": data.get("audio_base_64", ""),
129167
"commit": False,
130168
"sample_rate": self.current_sample_rate,
169+
"previous_text": data.get("previous_text"),
131170
}
132171

133172
await self.websocket.send(json.dumps(message))

0 commit comments

Comments
 (0)