Skip to content

Dropped chunks while streaming #6

@enzo2

Description

@enzo2

I noticed that when streaming, chunks seemed to be missing, especially during tool calls. Looking at http.rb, each 'data' object is extracted: chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
So, I added logging and found that the 'data' could be split across chunks. For example:
Chunk 1: data: {\"id\":\"gen-123\",\"provider\":\"Anthropic\",\"model\":\"anthropic/claude-3.5-so"
Chunk 2: "nnet\",\"object\":\"chat.completion.chunk\",\"created\":1721502907,\"choices\":[{\"delta\":{\"role\":\"assistant\",\"content\":null,\"tool_calls\":[{\"index\":0,\"type\":\"function\",\"function\":{\"arguments\":\"v0F2\"}}]},\"index\":0}]}......

The split data does not even get passed on to JSON.parse since it doesn't match the regex.
So, it would seem that chunks should be buffered until the complete 'data' is present.

Edit: On second thought, since it's complicated, maybe should just use the gem 'event_stream_parser' for this task like ruby-openai does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions