コンテンツにスキップ

ガードレール

ガードレールは 並行して エージェントと動作し、ユーザー入力やエージェント出力に対してチェックやバリデーションを実行できます。たとえば、高コストなモデルを呼び出す前に軽量モデルをガードレールとして実行し、悪意のある利用を検知した場合にエラーを発生させて高コストなモデルの実行を防ぐことができます。

ガードレールには 2 種類あります。

  1. Input guardrails は初期ユーザー入力に対して実行されます
  2. Output guardrails は最終的なエージェント出力に対して実行されます

Input guardrails は次の 3 ステップで実行されます。

  1. ガードレールはエージェントに渡されたものと同じ入力を受け取ります
  2. ガードレール関数が実行され、GuardrailFunctionOutputInputGuardrailResult にラップして返します
  3. tripwireTriggeredtrue の場合、InputGuardrailTripwireTriggered エラーがスローされます

Note
Input guardrails はユーザー入力を対象としているため、ワークフローでエージェントが 最初 のエージェントである場合にのみ実行されます。ガードレールはエージェントごとに設定します。異なるエージェントでは必要なガードレールが異なることが多いためです。

Output guardrails も同じパターンで動作します。

  1. ガードレールはエージェントに渡されたものと同じ入力を受け取ります
  2. ガードレール関数が実行され、GuardrailFunctionOutputOutputGuardrailResult にラップして返します
  3. tripwireTriggeredtrue の場合、OutputGuardrailTripwireTriggered エラーがスローされます

Note
Output guardrails はワークフローでエージェントが 最後 のエージェントである場合にのみ実行されます。リアルタイム音声インタラクションについては the voice agents guide を参照してください。

ガードレールが失敗すると、トリップワイヤーでそれを通知します。トリップワイヤーが発火した時点で、Runner は該当するエラーをスローし、実行を停止します。

ガードレールは GuardrailFunctionOutput を返す単なる関数です。以下は、別のエージェントを内部で呼び出してユーザーが数学の宿題の手助けを求めているかを判定する最小例です。

入力ガードレールの例
import {
Agent,
run,
InputGuardrailTripwireTriggered,
InputGuardrail,
} from '@openai/agents';
import { z } from 'zod';
const guardrailAgent = new Agent({
name: 'Guardrail check',
instructions: 'Check if the user is asking you to do their math homework.',
outputType: z.object({
isMathHomework: z.boolean(),
reasoning: z.string(),
}),
});
const mathGuardrail: InputGuardrail = {
name: 'Math Homework Guardrail',
execute: async ({ input, context }) => {
const result = await run(guardrailAgent, input, { context });
return {
outputInfo: result.finalOutput,
tripwireTriggered: result.finalOutput?.isMathHomework ?? false,
};
},
};
const agent = new Agent({
name: 'Customer support agent',
instructions:
'You are a customer support agent. You help customers with their questions.',
inputGuardrails: [mathGuardrail],
});
async function main() {
try {
await run(agent, 'Hello, can you help me solve for x: 2x + 3 = 11?');
console.log("Guardrail didn't trip - this is unexpected");
} catch (e) {
if (e instanceof InputGuardrailTripwireTriggered) {
console.log('Math homework guardrail tripped');
}
}
}
main().catch(console.error);

Output guardrails も同様に動作します。

出力ガードレールの例
import {
Agent,
run,
OutputGuardrailTripwireTriggered,
OutputGuardrail,
} from '@openai/agents';
import { z } from 'zod';
// The output by the main agent
const MessageOutput = z.object({ response: z.string() });
type MessageOutput = z.infer<typeof MessageOutput>;
// The output by the math guardrail agent
const MathOutput = z.object({ reasoning: z.string(), isMath: z.boolean() });
// The guardrail agent
const guardrailAgent = new Agent({
name: 'Guardrail check',
instructions: 'Check if the output includes any math.',
outputType: MathOutput,
});
// An output guardrail using an agent internally
const mathGuardrail: OutputGuardrail<typeof MessageOutput> = {
name: 'Math Guardrail',
async execute({ agentOutput, context }) {
const result = await run(guardrailAgent, agentOutput.response, {
context,
});
return {
outputInfo: result.finalOutput,
tripwireTriggered: result.finalOutput?.isMath ?? false,
};
},
};
const agent = new Agent({
name: 'Support agent',
instructions:
'You are a user support agent. You help users with their questions.',
outputGuardrails: [mathGuardrail],
outputType: MessageOutput,
});
async function main() {
try {
const input = 'Hello, can you help me solve for x: 2x + 3 = 11?';
await run(agent, input);
console.log("Guardrail didn't trip - this is unexpected");
} catch (e) {
if (e instanceof OutputGuardrailTripwireTriggered) {
console.log('Math output guardrail tripped');
}
}
}
main().catch(console.error);
  1. guardrailAgent はガードレール関数内で使用されます
  2. ガードレール関数はエージェントの入力または出力を受け取り、結果を返します
  3. ガードレール結果には追加情報を含めることができます
  4. agent がガードレールを適用する実際のワークフローを定義します