Skip to content

Commit 937b47c

Browse files
committed
fix: Wait for input guardrails before executing model requests
Fixes #889 When an input guardrail triggers a tripwire, tools (especially hosted tools like FileSearchTool) should not execute. Previously, input guardrails ran in parallel with model requests, creating a race condition where tools could execute before guardrails completed. This change makes input guardrail execution sequential: 1. Run input guardrails first and wait for completion 2. If any guardrail triggers, raise InputGuardrailTripwireTriggered 3. Only proceed with model request if all guardrails pass This ensures that: - No model requests are sent if guardrails trigger - Hosted tools (FileSearchTool) don't execute unnecessarily - Token consumption is prevented when input is blocked - Slow guardrails (e.g., calling external services) work correctly
1 parent 748ac80 commit 937b47c

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/agents/run.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -599,26 +599,28 @@ async def run(
599599
)
600600

601601
if current_turn == 1:
602-
input_guardrail_results, turn_result = await asyncio.gather(
603-
self._run_input_guardrails(
604-
starting_agent,
605-
starting_agent.input_guardrails
606-
+ (run_config.input_guardrails or []),
607-
_copy_str_or_list(prepared_input),
608-
context_wrapper,
609-
),
610-
self._run_single_turn(
611-
agent=current_agent,
612-
all_tools=all_tools,
613-
original_input=original_input,
614-
generated_items=generated_items,
615-
hooks=hooks,
616-
context_wrapper=context_wrapper,
617-
run_config=run_config,
618-
should_run_agent_start_hooks=should_run_agent_start_hooks,
619-
tool_use_tracker=tool_use_tracker,
620-
server_conversation_tracker=server_conversation_tracker,
621-
),
602+
# Run input guardrails first, before sending any model requests.
603+
# This ensures that if a guardrail triggers a tripwire, no tools
604+
# (especially hosted tools like FileSearchTool) execute.
605+
# See Issue #889: https://github.com/openai/openai-agents-python/issues/889
606+
input_guardrail_results = await self._run_input_guardrails(
607+
starting_agent,
608+
starting_agent.input_guardrails + (run_config.input_guardrails or []),
609+
_copy_str_or_list(prepared_input),
610+
context_wrapper,
611+
)
612+
# Only proceed with agent execution if guardrails passed
613+
turn_result = await self._run_single_turn(
614+
agent=current_agent,
615+
all_tools=all_tools,
616+
original_input=original_input,
617+
generated_items=generated_items,
618+
hooks=hooks,
619+
context_wrapper=context_wrapper,
620+
run_config=run_config,
621+
should_run_agent_start_hooks=should_run_agent_start_hooks,
622+
tool_use_tracker=tool_use_tracker,
623+
server_conversation_tracker=server_conversation_tracker,
622624
)
623625
else:
624626
turn_result = await self._run_single_turn(

0 commit comments

Comments
 (0)