Skip to content

Commit 20b3c66

Browse files
Python: fix for enum handling in openai event creation in realtime (#11397)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Fix for how the openai realtime event type constructors deal with enums, fixes #11246 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 3ec6d01 commit 20b3c66

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

python/semantic_kernel/connectors/ai/open_ai/services/_open_ai_realtime.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,51 +151,53 @@ class SendEvents(str, Enum):
151151
RESPONSE_CANCEL = "response.cancel"
152152

153153

154-
def _create_openai_realtime_client_event(event_type: SendEvents, **kwargs: Any) -> RealtimeClientEvent:
154+
def _create_openai_realtime_client_event(event_type: SendEvents | str, **kwargs: Any) -> RealtimeClientEvent:
155155
"""Create an OpenAI Realtime client event from a event type and kwargs."""
156+
if isinstance(event_type, str):
157+
event_type = SendEvents(event_type)
156158
match event_type:
157159
case SendEvents.SESSION_UPDATE:
158160
if "session" not in kwargs:
159161
raise ContentException("Session is required for SessionUpdateEvent")
160162
return SessionUpdateEvent(
161-
type=event_type,
163+
type=event_type.value,
162164
session=kwargs.pop("session"),
163165
**kwargs,
164166
)
165167
case SendEvents.INPUT_AUDIO_BUFFER_APPEND:
166168
if "audio" not in kwargs:
167169
raise ContentException("Audio is required for InputAudioBufferAppendEvent")
168170
return InputAudioBufferAppendEvent(
169-
type=event_type,
171+
type=event_type.value,
170172
**kwargs,
171173
)
172174
case SendEvents.INPUT_AUDIO_BUFFER_COMMIT:
173175
return InputAudioBufferCommitEvent(
174-
type=event_type,
176+
type=event_type.value,
175177
**kwargs,
176178
)
177179
case SendEvents.INPUT_AUDIO_BUFFER_CLEAR:
178180
return InputAudioBufferClearEvent(
179-
type=event_type,
181+
type=event_type.value,
180182
**kwargs,
181183
)
182184
case SendEvents.CONVERSATION_ITEM_CREATE:
183185
if "item" not in kwargs:
184186
raise ContentException("Item is required for ConversationItemCreateEvent")
185-
kwargs["type"] = event_type
187+
kwargs["type"] = event_type.value
186188
return ConversationItemCreateEvent(**kwargs)
187189
case SendEvents.CONVERSATION_ITEM_TRUNCATE:
188190
if "content_index" not in kwargs:
189191
kwargs["content_index"] = 0
190192
return ConversationItemTruncateEvent(
191-
type=event_type,
193+
type=event_type.value,
192194
**kwargs,
193195
)
194196
case SendEvents.CONVERSATION_ITEM_DELETE:
195197
if "item_id" not in kwargs:
196198
raise ContentException("Item ID is required for ConversationItemDeleteEvent")
197199
return ConversationItemDeleteEvent(
198-
type=event_type,
200+
type=event_type.value,
199201
**kwargs,
200202
)
201203
case SendEvents.RESPONSE_CREATE:
@@ -204,13 +206,13 @@ def _create_openai_realtime_client_event(event_type: SendEvents, **kwargs: Any)
204206
else:
205207
response = None
206208
return ResponseCreateEvent(
207-
type=event_type,
209+
type=event_type.value,
208210
response=response,
209211
**kwargs,
210212
)
211213
case SendEvents.RESPONSE_CANCEL:
212214
return ResponseCancelEvent(
213-
type=event_type,
215+
type=event_type.value,
214216
**kwargs,
215217
)
216218

0 commit comments

Comments
 (0)