LLM テキストストリーミング(大規模言語モデルの出力を少しずつ送信する機能)を Output ワークフローステップに実装する際に、generateTextWithStreaming、Agent.generateWithStreaming、streamText、または Agent.stream を使用します。 次のような場合に使用: - トークン進捗状況(処理の段階的な進行状況)の追加 - onChunk コールバック(受信時に実行される処理)の設定 - streamText の onEnd/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 onEnd/onError with Temporal retries.
streamText() や Agent.stream() で onChunk(到着ごとに実行される処理)や onEnd(終了時)/ onError(エラー時)を使いたい| 必要な機能 | 使う API |
|---|---|
| 完全な結果を一度に取得 | generateText() |
| 完全な結果+進行状況の表示 | generateTextWithStreaming() |
| ストリーミングデータに直接アクセス | streamText() |
| エージェントの完全な結果+進行状況の表示 | Agent.generateWithStreaming() |
| エージェントのストリームに直接アクセス | Agent.stream() |
ワークフロー内のステップ(処理単位)では、進行状況の表示で十分な場合は 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() はエージェントの完全な応答を返し、エージェントが 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 が再試行できなくなります。完了を示す属性を待つと、元の提供元エラーではなく、汎用的な「出力なし」エラーが起こることもあります。
Agent.stream() は finishReason が 'error' でない場合、onEnd でラップされた会話メッセージを保存します。完全な保存済み応答が必要な場合は Agent.generateWithStreaming() を使ってください。
ストリーミング呼び出しの引数: prompt、promptDir、variables、tools、output、toolChoice、stopWhen、abortSignal、そして onChunk(generateTextWithStreaming の場合)または onChunk / onEnd / onError(streamText の場合)。エージェントメソッド: messages、abortSignal、toolChoice、そして前述のストリーミングコールバック。
streamText() でステップを失敗させるために onError だけに頼らないでください。onChunk 内の副作用(その他の処理)を制限してください。1トークンごとに Temporal シグナル(信号)を送ると、シグナルごとに履歴イベントが作成されるため、高頻度の更新はまとめてください。streamText() や Agent.stream() を非推奨として説明しないでください。output-dev-step-function - LLM 呼び出しを Temporal ステップの中に配置するoutput-dev-agent-class - 再利用可能なエージェントを構築・使用するoutput-dev-prompt-file - 生成用のプロンプトファイルを作成するoutput-error-try-catch - ステップとワークフローのエラーを処理するonChunk, or onEnd / onError on streamText() / Agent.stream()| Need | Use |
|---|---|
| Complete single-shot result | generateText() |
Complete result plus onChunk progress |
generateTextWithStreaming() |
Direct access to textStream or stream |
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 onEnd 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 / onEnd / 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 による自動翻訳です。