LLM のテキストストリーミング(大量のテキストを小分けにしてリアルタイムで送信する機能)を出力ワークフロー ステップに実装します。 **使用方法:** - `generateTextWithStreaming` - `Agent.generateWithStreaming` - `streamText` - `Agent.stream` **次のような場合に使用:** - トークン(言語処理の最小単位)の進捗表示を追加する - onChunk コールバック(処理の途中経過を受け取る仕組み)を設定する - streamText の onFinish/onError を Temporal リトライ(エラー時の自動再試行)と組み合わせて処理する
Implement LLM text streaming in Output workflow steps with generateTextWithStreaming, Agent.generateWithStreaming, streamText, or Agent.stream. Use when adding token progress, onChunk callbacks, or handling streamText onFinish/onError with Temporal retries.
streamText() または Agent.stream() 上で onChunk、onFinish/onError を使いたい| 必要な機能 | 使用する API |
|---|---|
| 単発の完全な結果 | generateText() |
完全な結果と onChunk 進捗情報 |
generateTextWithStreaming() |
textStream または fullStream への直接アクセス |
streamText() |
Agent の完全な結果と onChunk 進捗情報 |
Agent.generateWithStreaming() |
| Agent ストリームへの直接アクセス | Agent.stream() |
ワークフロー処理ステップでは、onChunk による進捗情報で十分な場合は generateTextWithStreaming() または Agent.generateWithStreaming() を優先します。
これらは内部でストリームを処理し、generateText() や Agent.generate() と同じように完全な結果を返し、プロバイダー(サービス提供元)のエラー、通信エラー、キャンセルエラーの場合は処理を中断します。中断により、Temporal は失敗したアクティビティを記録し、設定されたステップ再試行ポリシーを適用できます。
streamText() と Agent.stream() は、ストリーム処理を自分で完全に制御する必要があるコードのために引き続きサポートされています。
import { generateTextWithStreaming } from '@outputai/llm';
const result = await generateTextWithStreaming( {
prompt: 'draft@v1',
variables: { topic },
onChunk( { chunk } ) {
if ( chunk.type === 'text-delta' ) {
process.stdout.write( chunk.text );
}
}
} );
return result.result;
結果には、generateText() と同じ完全な応答フィールド(result、text、output、usage、finishReason、cost)が含まれます。aiSdk.Output.* で指定した構造化出力は result.output からアクセスできます。
const result = await agent.generateWithStreaming( {
onChunk( { chunk } ) {
if ( chunk.type === 'text-delta' ) {
process.stdout.write( chunk.text );
}
}
} );
generateWithStreaming() は完全な Agent 応答を返し、Agent が messageStore(メッセージ保存機能)を持つ場合は自動的にメッセージを保存します。
AI SDK ストリーミングは、プロバイダーと通信のエラーを onError 経由で通知します。textStream を反復処理するだけでは、元のエラーが確実に例外として発生しません。ワークフロー処理ステップで streamText() を使う場合は、エラーを捕捉してから処理を中断してください:
import { streamText } from '@outputai/llm';
const captured: { error: unknown } = { error: null };
const result = streamText( {
prompt: 'draft@v1',
variables: { topic },
onError( { error } ) {
captured.error = error;
}
} );
const chunks: string[] = [];
for await ( const chunk of result.textStream ) {
chunks.push( chunk );
}
if ( captured.error ) {
throw captured.error;
}
return chunks.join( '' );
onError を登録しても捕捉したエラーを発生させなければ、ステップが成功した結果を返してしまい、Temporal が再試行できなくなります。完了用プロパティを await する方法も、元のプロバイダーエラーではなく汎用的な「出力なし」エラーを生じる可能性があります。
Agent.stream() は、finishReason が 'error' でない場合、ラップされた onFinish で会話メッセージを保存します。完全に保存された応答で要件が満たされる場合は Agent.generateWithStreaming() を使用してください。
ストリーミング呼び出しの引数:prompt、promptDir、variables、tools、output、toolChoice、stopWhen、abortSignal、および onChunk(generateTextWithStreaming)または onChunk/onFinish/onError(streamText)。Agent メソッド:messages、abortSignal、toolChoice、およびそれらと同じストリーム関数。
streamText() でステップを失敗させるため、onError だけに頼らないでください。onChunk の副作用を制限してください。トークンごとの Temporal シグナルはシグナルごとに履歴イベントを作成するため、高頻度の更新はまとめてください。streamText() または Agent.stream() を非推奨と説明しないでください。output-dev-step-function - LLM 呼び出しを Temporal アクティビティステップ内に配置するoutput-dev-agent-class - 再利用可能な Agent の構築と使用output-dev-prompt-file - 生成用のプロンプトファイルを作成するoutput-error-try-catch - ステップとワークフローの失敗を処理するonChunk, or onFinish / onError on streamText() / Agent.stream()| Need | Use |
|---|---|
| Complete single-shot result | generateText() |
Complete result plus onChunk progress |
generateTextWithStreaming() |
Direct access to textStream or fullStream |
streamText() |
Complete Agent result plus onChunk progress |
Agent.generateWithStreaming() |
| Direct access to the Agent stream | Agent.stream() |
In workflow steps, prefer generateTextWithStreaming() or Agent.generateWithStreaming() when onChunk progress is sufficient. They consume the stream internally, return complete results like generateText() or Agent.generate(), and reject on provider, transport, or abort errors. Rejection allows Temporal to record the failed activity attempt and apply the step retry policy.
streamText() and Agent.stream() remain supported for code that needs direct control over stream consumption.
import { generateTextWithStreaming } from '@outputai/llm';
const result = await generateTextWithStreaming( {
prompt: 'draft@v1',
variables: { topic },
onChunk( { chunk } ) {
if ( chunk.type === 'text-delta' ) {
process.stdout.write( chunk.text );
}
}
} );
return result.result;
The result has the same complete response fields as generateText(), including result, text, output, usage, finishReason, and cost. Structured output passed with aiSdk.Output.* is available through result.output.
const result = await agent.generateWithStreaming( {
onChunk( { chunk } ) {
if ( chunk.type === 'text-delta' ) {
process.stdout.write( chunk.text );
}
}
} );
generateWithStreaming() returns a complete Agent response and automatically stores messages when the Agent has a messageStore.
AI SDK streaming delivers provider and transport failures through onError. Iterating textStream does not reliably throw the original error. When using streamText() in a workflow step, capture the error and throw it after consumption:
import { streamText } from '@outputai/llm';
const captured: { error: unknown } = { error: null };
const result = streamText( {
prompt: 'draft@v1',
variables: { topic },
onError( { error } ) {
captured.error = error;
}
} );
const chunks: string[] = [];
for await ( const chunk of result.textStream ) {
chunks.push( chunk );
}
if ( captured.error ) {
throw captured.error;
}
return chunks.join( '' );
Registering onError without throwing the captured error can let the step return an empty successful result, preventing Temporal from retrying it. Awaiting a completion property may also produce a generic no-output error instead of the original provider error.
Agent.stream() stores conversation messages in its wrapped onFinish when finishReason is not 'error'. Use Agent.generateWithStreaming() when a complete stored response meets the requirement.
Streaming call arguments: prompt, promptDir, variables, tools, output, toolChoice, stopWhen, abortSignal, plus onChunk (generateTextWithStreaming) or onChunk / onFinish / onError (streamText). Agent methods: messages, abortSignal, toolChoice, plus those same stream callbacks.
onError alone to fail a step using streamText().onChunk side effects bounded. A Temporal signal per token creates a history event per signal, so batch high-frequency updates.streamText() or Agent.stream() as deprecated.output-dev-step-function - Put LLM calls inside Temporal activity stepsoutput-dev-agent-class - Construct and use reusable Agentsoutput-dev-prompt-file - Create prompt files for generationoutput-error-try-catch - Handle step and workflow failures原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。