https://www.boundaryml.com logo
🟡 Should OpenAPI Generated Clients Support Stream...
# questions
j
Should the openapi generated clients support streaming? Building a swift client but not seeing that. But can't find if its expected or not
b
Unfortunately openapi itself doesn’t support streaming client generation:(
We do expose the endpoints themselves at /stream, and you can still use the generated types to call the streaming version of functions, but it’s unfortunately not nearly as nice an experience
a
j
oh interesting, thanks! I will give that a try and see what its like.
With swift openapi generator I got it working I think. seems to be streaming in
Copy code
// MARK: - Streaming
    func streamExtractResume(resumeText: String) -> (AsyncThrowingStream<Components.Schemas.ResumePartial, Error>, () -> Void) {
        let holder = TaskHolder()
        let stream = AsyncThrowingStream<Components.Schemas.ResumePartial, Error> { continuation in
            let task = Task {
                do {
                    let input = Operations.ExtractResumeStream.Input(
                        body: .json(.init(resume: resumeText))
                    )
                    let output = try await client.extractResumeStream(input)
                    switch output {
                    case .ok(let ok):
                        switch ok.body {
                        case .textEventStream(let body):
                            // If OpenAPIRuntime has SSE helpers, you can replace this parsing with asServerSentEvents().
                            let parser = SSEParser()
                            for try await chunk in body {
                                let events = parser.feed(Data(chunk))
                                for evt in events {
                                    if let s = evt.data {
                                        if let obj = try? JSONDecoder().decode(Components.Schemas.ResumePartial.self, from: Data(s.utf8)) {
                                            continuation.yield(obj)
                                        }
                                    }
                                }
                            }
                            // Flush any trailing data
                            for evt in parser.finish() {
                                if let s = evt.data, let obj = try? JSONDecoder().decode(Components.Schemas.ResumePartial.self, from: Data(s.utf8)) {
I have to manually add the live endpoint to the openapi tho of course
Copy code
/stream/ExtractResume:
    post:
      requestBody:
        $ref: '#/components/requestBodies/ExtractResume'
      responses:
        '200':
          description: Successful streaming operation
          content:
            text/event-stream:
              schema:
                $ref: '#/components/schemas/ResumePartial'
      operationId: ExtractResumeStream
but seems to work!
b
Oh, interesting
We didn’t find any evidence that any of the openapi generators had implemented SSE support
So is that what your swift openapi generator is generating? Or is there a method in there that’s generated which I’m missing?
a
whoa this is kinda wild, maybe we will add this as our swift example at some point
j
@baml-sam so this is the generated swift code and the wrapper client codex wrote. I had to add the stream endpoints to the openapi https://gist.github.com/Jonovono/268fa7b5e4c2a0fc1540ef06368c3924