Skip to content

fix: add automatic text-start and text-end events#13

Open
titanism2197 wants to merge 1 commit intoHelicone:mainfrom
titanism2197:feature/auto-text-events
Open

fix: add automatic text-start and text-end events#13
titanism2197 wants to merge 1 commit intoHelicone:mainfrom
titanism2197:feature/auto-text-events

Conversation

@titanism2197
Copy link
Copy Markdown

Automatically emit text-start and text-end events for proper stream lifecycle management.
Solves the "text part text-0 not found" error by eliminating the need for manual stream wrapping.

Problem

Users encountered "text part text-0 not found" errors in production:

data: {"type":"start"}
data: {"type":"start-step"}
data: {"type":"text-delta","id":"text-0","delta":"지"}
data: {"type":"error","errorText":"text part text-0 not found"}

Root Cause: text-delta events were emitted without preceding text-start events, causing the AI SDK to fail when tracking text parts.

Previous Workaround: Users had to manually wrap streams:

// ❌ Manual wrapper required
const ensureTextParts = (stream) => {
  // Manually inject text-start/text-end events
};

Solution

Text lifecycle events are now emitted automatically following AI SDK standards:

// ✅ Now works automatically!
const result = await streamText({
  model: helicone('gpt-4o'),
  prompt: 'Hello'
});
// text-start → text-delta → text-end → finish

Changes

  • Add text part state tracking (textStarted, textEnded)
  • Auto-emit text-start before first text-delta
  • Auto-emit text-end when finish_reason arrives or stream ends
  • Prevent duplicate events with Set tracking
  • Update tool-calling test to expect text-start as first event

Event Flow

Before (Error):
  text-delta (id: text-0) ❌ <- Missing text-start!
    → ERROR: "text part text-0 not found"

After (Fixed):
  text-start (id: text-0) ✅
    → text-delta (id: text-0) ✅
    → text-delta (id: text-0) ✅
    → text-end (id: text-0) ✅
    → finish ✅

All tests passing: 77/77 ✅

Files Changed

  • src/helicone-language-model.ts (+36/-1 lines)
  • src/tests/tool-calling.test.ts (+2/-2 lines)

Total: 38 insertions(+), 3 deletions(-) ✨

Breaking Changes

⚠️ Minor: Tests expecting text-delta as first event need to expect text-start.
This matches AI SDK's expected event flow and is the correct behavior.

Migration

No code changes needed for end users. Events are added automatically.
If you were using a custom ensureTextParts wrapper, you can now remove it!

Fixes

  • Resolves "text part text-0 not found" errors
  • Ensures proper stream lifecycle
  • Matches Vercel AI SDK event standards

@titanism2197 titanism2197 changed the title feat: add automatic text-start and text-end events fix: add automatic text-start and text-end events Nov 16, 2025
@cyborgshead
Copy link
Copy Markdown

@juliettech13 please have a look, need to fix broken streams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants