diff --git a/.changeset/fresh-colts-study.md b/.changeset/fresh-colts-study.md new file mode 100644 index 00000000..74c9f43d --- /dev/null +++ b/.changeset/fresh-colts-study.md @@ -0,0 +1,7 @@ +--- +"workers-ai-provider": patch +--- + +Enhance response handling for number types. +This PR addresses a critical bug in the implementation that caused non-output when streaming responses from models like Llama4 that can return numeric chunks. +The issue stemmed from an inadequate check on incoming stream chunks, which assumed all chunks would have a .length property, leading to errors with non-string data types. diff --git a/packages/workers-ai-provider/src/streaming.ts b/packages/workers-ai-provider/src/streaming.ts index c3a9952e..d4b569f5 100644 --- a/packages/workers-ai-provider/src/streaming.ts +++ b/packages/workers-ai-provider/src/streaming.ts @@ -43,6 +43,19 @@ export function getMappedStream(response: Response) { }); } + // Handling number responses for models like Llama4 + if (typeof chunk.response === "number") { + if (!textId) { + textId = generateId(); + controller.enqueue({ type: "text-start", id: textId }); + } + controller.enqueue({ + type: "text-delta", + id: textId, + delta: String(chunk.response), + }); + } + // Handle reasoning content const reasoningDelta = chunk?.choices?.[0]?.delta?.reasoning_content; if (reasoningDelta?.length) {