fix: handle empty string delta.content in OpenAI streaming

Some providers send an empty string as the first delta to signal
streaming start. The falsy check `if (delta.content)` treated "" as
absent, skipping content_block_start. The next delta with actual
content was emitted without it, violating the Anthropic protocol.

Changed to `delta.content != null` to distinguish between absent field
and empty string.

Relates to #42

Co-Authored-By: Juan Camilo <juancamilo.auriti@gmail.com>
This commit is contained in:
Juan Camilo
2026-04-01 17:03:28 +02:00
parent 8750f84464
commit 788cfa3e9a

View File

@@ -345,8 +345,9 @@ async function* openaiStreamToAnthropic(
for (const choice of chunk.choices ?? []) {
const delta = choice.delta
// Text content
if (delta.content) {
// Text content — use != null to distinguish absent field from empty string,
// some providers send "" as first delta to signal streaming start
if (delta.content != null) {
if (!hasEmittedContentStart) {
yield {
type: 'content_block_start',