@@ -55,23 +55,10 @@ final class ModelResponseEventsStreamInterpreter: @unchecked Sendable, StreamInt
5555 }
5656
5757 private func processEvent( _ event: ServerSentEventsStreamParser . Event ) throws {
58- var eventType = event. eventType
58+ var eventType = event. fixMappingError ( ) . eventType
5959
6060 if eventType == " message " { // This is currently the default if no SSE event name is specified
61- struct _TypeEnvelope : Decodable { let type : String }
62-
63- if var payloadType = try ? JSONDecoder ( ) . decode ( _TypeEnvelope. self, from: event. data) . type {
64- if payloadType == " response.output_text.annotation.added " {
65- // Remove when they have fixed (unified)!
66- //
67- // By looking at [API Reference](https://platform.openai.com/docs/api-reference/responses-streaming/response/output_text_annotation/added)
68- // and generated type `Schemas.ResponseOutputTextAnnotationAddedEvent`
69- // We can see that "output_text.annotation" is incorrect, whereas output_text_annotation is the correct one
70- payloadType = " response.output_text_annotation.added "
71- }
72-
73- eventType = payloadType
74- }
61+ eventType = self . getPayloadType ( )
7562 }
7663
7764 guard let modelResponseEventType = ModelResponseStreamEventType ( rawValue: eventType) else {
@@ -81,7 +68,7 @@ final class ModelResponseEventsStreamInterpreter: @unchecked Sendable, StreamInt
8168 let responseStreamEvent = try responseStreamEvent ( modelResponseEventType: modelResponseEventType, data: event. data)
8269 onEventDispatched ? ( responseStreamEvent)
8370 }
84-
71+
8572 private func processError( _ error: Error ) {
8673 onError ? ( error)
8774 }
@@ -218,3 +205,35 @@ final class ModelResponseEventsStreamInterpreter: @unchecked Sendable, StreamInt
218205 try decoder. decode ( T . self, from: data)
219206 }
220207}
208+
209+ private extension ServerSentEventsStreamParser . Event {
210+
211+ // Remove when they have fixed (unified)!
212+ //
213+ // By looking at [API Reference](https://platform.openai.com/docs/api-reference/responses-streaming/response/output_text_annotation/added)
214+ // and generated type `Schemas.ResponseOutputTextAnnotationAddedEvent`
215+ // We can see that "output_text.annotation" is incorrect, whereas output_text_annotation is the correct one
216+ func fixMappingError( ) -> Self {
217+ let incorrectEventType = " response.output_text.annotation.added "
218+ let correctEventType = " response.output_text_annotation.added "
219+
220+ guard self . eventType == incorrectEventType || self . getPayloadType ( ) == incorrectEventType else {
221+ return self
222+ }
223+
224+ let fixedDataString = event. decodedData. replacingOccurrences ( of: incorrectEventType, with: correctEventType)
225+ return . init(
226+ id: event. id,
227+ data: fixedDataString. data ( using: . utf8) ?? event. data,
228+ decodedData: fixedDataString,
229+ eventType: correctEventType,
230+ retry: event. retry
231+ )
232+ }
233+
234+ struct TypeEnvelope : Decodable { let type : String }
235+
236+ func getPayloadType( ) -> String ? {
237+ try ? JSONDecoder ( ) . decode ( TypeEnvelope . self, from: event. data) . type
238+ }
239+ }
0 commit comments