fix(workers-ai-provider): don't duplicate streaming text on dual-format chunks - #615
Open
carlos-rsalazar wants to merge 1 commit into
Conversation
…at chunks
Workers AI's OpenAI-compatible streaming emits BOTH the native top-level `response`
field AND `choices[0].delta.content` in the same SSE chunk, with identical content
(e.g. @cf/meta/llama-3.3-70b-instruct-fp8-fast). getMappedStream() emits a text-delta
for each in two independent if-blocks, so every token is duplicated
("Hello world" -> "HelloHello world world") — and the doubled text is what consumers
persist/render.
Treat the two as mutually exclusive: use choices[0].delta.content only when the native
`response` field is absent for that chunk. Adds a regression test (fails before, passes
after) and a changeset.
🦋 Changeset detectedLatest commit: 27c1331 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Workers AI's OpenAI-compatible streaming emits both the native top-level
responsefield andchoices[0].delta.contentin the same SSE chunk, with identical content (observed with@cf/meta/llama-3.3-70b-instruct-fp8-fast).In
streaming.ts,getMappedStream()handles the two formats in two independentifblocks (nativeresponse~L168, OpenAIchoices[0].delta.content~L224), eachenqueue-ing atext-delta. When a chunk carries both, every token is emitted twice, so streamed output is doubled and the doubled text is what consumers persist/render:This reproduces reliably for any model whose streaming response includes both fields.
Fix
Treat the native and OpenAI text fields as mutually exclusive: use
choices[0].delta.contentonly when the nativeresponsefield is absent for that chunk (they're redundant duplicates when both present). One-line behavioral change plus a comment.Test
Adds a regression test in
stream-text.test.tsthat streams a chunk carrying bothresponseandchoices[].delta.content:Received: "Hello Hello worldworld"."Hello world").Full
workers-ai-providersuite green (455 tests). Formatted withoxfmt, lint-clean.Changeset
patchforworkers-ai-provider.Found while integrating
workers-ai-providerinto a Cloudflare-Workers app; happy to adjust the approach if you'd prefer the guard on the native branch instead.