コンテンツにスキップ

エージェントの実行結果

エージェントの実行 を行うと、結果として次のいずれかを受け取ります。

  • stream: true を付けずに run を呼び出した場合 — RunResult
  • stream: true を付けて run を呼び出した場合 — StreamedRunResult。ストリーミングの詳細は ストリーミング も参照してください。

finalOutput プロパティには、最後に実行されたエージェントの最終出力が入ります。型は次のいずれかです。

  • stringoutputType が定義されていないエージェントのデフォルト
  • unknown — エージェントが JSON スキーマを出力タイプとして定義している場合。JSON はパースされますが、型の確認は手動で行う必要があります
  • z.infer<outputType> — エージェントが Zod スキーマを出力タイプとして定義している場合。出力は自動的にこのスキーマでパースされます
  • undefined — エージェントが出力を生成しなかった場合(たとえば途中で停止した場合)

複数の異なる出力タイプでハンドオフを使用する場合は、new Agent() コンストラクターではなく Agent.create() メソッドでエージェントを作成してください。

これにより、SDK はすべてのハンドオフでの出力タイプを推論し、finalOutput プロパティにユニオン型を提供します。

例:

Handoff final output types
import { Agent, run } from '@openai/agents';
import { z } from 'zod';
const refundAgent = new Agent({
name: 'Refund Agent',
instructions:
'You are a refund agent. You are responsible for refunding customers.',
outputType: z.object({
refundApproved: z.boolean(),
}),
});
const orderAgent = new Agent({
name: 'Order Agent',
instructions:
'You are an order agent. You are responsible for processing orders.',
outputType: z.object({
orderId: z.string(),
}),
});
const triageAgent = Agent.create({
name: 'Triage Agent',
instructions:
'You are a triage agent. You are responsible for triaging customer issues.',
handoffs: [refundAgent, orderAgent],
});
const result = await run(triageAgent, 'I need to a refund for my order');
const output = result.finalOutput;
// ^? { refundApproved: boolean } | { orderId: string } | string | undefined

次ターン用の入力には 2 通りの取得方法があります。

  • result.history — 入力とエージェントの出力の両方をコピーして保持します
  • result.output — エージェント実行全体の出力を保持します

チャットのようなユースケースでは、history を使うと完全な履歴を簡単に保持できます。

History loop
import { AgentInputItem, Agent, user, run } from '@openai/agents';
const agent = new Agent({
name: 'Assistant',
instructions:
'You are a helpful assistant knowledgeable about recent AGI research.',
});
let history: AgentInputItem[] = [
// intial message
user('Are we there yet?'),
];
for (let i = 0; i < 10; i++) {
// run 10 times
const result = await run(agent, history);
// update the history to the new output
history = result.history;
history.push(user('How about now?'));
}

lastAgent プロパティには最後に実行されたエージェントが入ります。たとえばフロントラインのトリアージ エージェントが言語別エージェントへハンドオフする場合、lastAgent を保存しておき、次回ユーザーからメッセージが来た際に再利用できます。

ストリーミング モードでは、現在実行中のエージェントを示す currentAgent プロパティを参照するのが便利なこともあります。

newItems プロパティには実行中に生成された新しいアイテムが入ります。アイテムは RunItem で、LLM が生成した元アイテムをラップしています。これにより、LLM の出力に加え、そのイベントがどのエージェントに紐づくかを取得できます。

  • RunMessageOutputItem — LLM からのメッセージ。raw は生成されたメッセージ
  • RunHandoffCallItem — LLM がハンドオフ ツールを呼び出したことを示します。raw はツール呼び出しアイテム
  • RunHandoffOutputItem — ハンドオフが発生したことを示します。raw はツールのレスポンス。ソース/ターゲット エージェントにもアクセス可能
  • RunToolCallItem — LLM がツールを呼び出したことを示します
  • RunToolCallOutputItem — ツールが呼び出されたことを示します。raw はツールのレスポンス。ツール出力にもアクセス可能
  • RunReasoningItem — LLM からの推論アイテム。raw は生成された推論
  • RunToolApprovalItem — LLM がツール呼び出しの承認を要求したことを示します。raw はツール呼び出しアイテム

state プロパティには実行の状態が入ります。result に付随する多くの情報は state から導出されていますが、state はシリアライズ/デシリアライズ可能で、エラーからの復旧割り込み に対処するために、後続の run 呼び出しの入力として利用できます。

エージェントで needsApproval を使用している場合、run 中に処理すべき interruptions が発生することがあります。その場合、interruptions は割り込みを引き起こした ToolApprovalItem の配列になります。割り込みの扱いについては 人間の介入(HITL) を参照してください。

rawResponses プロパティには、エージェント実行中にモデルが生成した元の LLM レスポンスが入ります。

lastResponseId プロパティには、エージェント実行中にモデルが最後に生成したレスポンスの ID が入ります。

inputGuardrailResultsoutputGuardrailResults には、ガードレールの結果が入ります(存在する場合)。ログや保存に役立つ情報が含まれていることがあるため、これらを利用できます。

input プロパティには run メソッドに渡した元の入力が入ります。通常は不要ですが、必要な場合のために提供しています。