Skip to content

Commit 3adad9c

Browse files
committed
use event type in payload
1 parent a6e2f87 commit 3adad9c

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Sources/OpenAI/Private/Streaming/ModelResponseEventsStreamInterpreter.swift

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,30 @@ final class ModelResponseEventsStreamInterpreter: @unchecked Sendable, StreamInt
5555
}
5656

5757
private func processEvent(_ event: ServerSentEventsStreamParser.Event) throws {
58-
var finalEvent = event
59-
if event.eventType == "response.output_text.annotation.added" {
60-
// Remove when they have fixed (unified)!
61-
//
62-
// By looking at [API Reference](https://platform.openai.com/docs/api-reference/responses-streaming/response/output_text_annotation/added)
63-
// and generated type `Schemas.ResponseOutputTextAnnotationAddedEvent`
64-
// We can see that "output_text.annotation" is incorrect, whereas output_text_annotation is the correct one
65-
let fixedDataString = event.decodedData.replacingOccurrences(of: "response.output_text.annotation.added", with: "response.output_text_annotation.added")
66-
finalEvent = .init(id: event.id, data: fixedDataString.data(using: .utf8) ?? event.data, decodedData: fixedDataString, eventType: "response.output_text_annotation.added", retry: event.retry)
58+
var eventType = event.eventType
59+
60+
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+
}
6775
}
68-
69-
guard let modelResponseEventType = ModelResponseStreamEventType(rawValue: finalEvent.eventType) else {
70-
throw InterpreterError.unknownEventType(finalEvent.eventType)
76+
77+
guard let modelResponseEventType = ModelResponseStreamEventType(rawValue: eventType) else {
78+
throw InterpreterError.unknownEventType(eventType)
7179
}
7280

73-
let responseStreamEvent = try responseStreamEvent(modelResponseEventType: modelResponseEventType, data: finalEvent.data)
81+
let responseStreamEvent = try responseStreamEvent(modelResponseEventType: modelResponseEventType, data: event.data)
7482
onEventDispatched?(responseStreamEvent)
7583
}
7684

0 commit comments

Comments
 (0)