-
Couldn't load subscription status.
- Fork 2.8k
feat: add run_in_parallel parameter to input guardrails #1986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
This design looks flexible and clean enough. @rm-openai what do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a run_in_parallel parameter to input guardrails, allowing developers to choose between parallel execution (default, for better latency) and blocking execution (for better cost efficiency and to prevent race conditions). The change is backward compatible with the default set to True.
Key Changes:
- Added
run_in_parallelboolean field toInputGuardraildataclass and@input_guardraildecorator - Implemented conditional execution logic in
AgentRunnerto run sequential guardrails before the agent starts and parallel guardrails concurrently - Updated documentation and examples to explain the new execution modes
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/agents/guardrail.py | Adds run_in_parallel field to InputGuardrail class and parameter to decorator |
| src/agents/run.py | Implements sequential vs parallel guardrail execution logic in both streaming and non-streaming modes |
| tests/test_guardrails.py | Adds tests for the new run_in_parallel parameter with default and explicit values |
| docs/guardrails.md | Documents the new execution modes for input guardrails |
| examples/agent_patterns/input_guardrails.py | Updates example documentation to explain the new feature |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| run_in_parallel: bool = True | ||
| """Whether the guardrail runs concurrently with the agent (True, default) or before | ||
| the agent starts (False). | ||
| """ |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class-level docstring for InputGuardrail states 'Input guardrails are checks that run in parallel to the agent's execution' but this is no longer accurate since guardrails can now run sequentially. The docstring should be updated to reflect that guardrails can run either in parallel or sequentially based on the run_in_parallel parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 1ad513a
docs/guardrails.md
Outdated
| # Guardrails | ||
|
|
||
| Guardrails run _in parallel_ to your agents, enabling you to do checks and validations of user input. For example, imagine you have an agent that uses a very smart (and hence slow/expensive) model to help with customer requests. You wouldn't want malicious users to ask the model to help them with their math homework. So, you can run a guardrail with a fast/cheap model. If the guardrail detects malicious usage, it can immediately raise an error, which stops the expensive model from running and saves you time/money. | ||
| Guardrails enable you to do checks and validations of user input and agent output. For example, imagine you have an agent that uses a very smart (and hence slow/expensive) model to help with customer requests. You wouldn't want malicious users to ask the model to help them with their math homework. So, you can run a guardrail with a fast/cheap model. If the guardrail detects malicious usage, it can immediately raise an error and prevent the expensive model from running, saving you time and money. |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated documentation now states guardrails 'can immediately raise an error and prevent the expensive model from running' but this is only accurate for blocking guardrails (run_in_parallel=False). For parallel guardrails (the default), the expensive model may have already started. Consider clarifying this distinction or adding a note that this benefit applies specifically to blocking guardrails.
| Guardrails enable you to do checks and validations of user input and agent output. For example, imagine you have an agent that uses a very smart (and hence slow/expensive) model to help with customer requests. You wouldn't want malicious users to ask the model to help them with their math homework. So, you can run a guardrail with a fast/cheap model. If the guardrail detects malicious usage, it can immediately raise an error and prevent the expensive model from running, saving you time and money. | |
| Guardrails enable you to do checks and validations of user input and agent output. For example, imagine you have an agent that uses a very smart (and hence slow/expensive) model to help with customer requests. You wouldn't want malicious users to ask the model to help them with their math homework. So, you can run a guardrail with a fast/cheap model. If the guardrail detects malicious usage, it can immediately raise an error and prevent the expensive model from running, saving you time and money (**when using blocking guardrails; for parallel guardrails, the expensive model may have already started running before the guardrail completes—see "Execution modes" below for details**). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 1ad513a
5b0b203 to
3df0d58
Compare
💡 Codex Reviewopenai-agents-python/src/agents/run.py Lines 1022 to 1056 in 5b0b203
In the streaming path, blocking input guardrails are awaited via ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|
@codex review |
|
I am done from my side. Please review it. Let me know for any changes. Thanks! |
|
Codex Review: Didn't find any major issues. Swish! ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Summary
Adds a
run_in_parallelboolean parameter toInputGuardrailthat allows developers to choose between parallel execution (default, better latency) and blocking execution (better cost efficiency and prevents race conditions).Motivation:
Currently, input guardrails run in parallel with the agent's execution. While this provides low latency, it can lead to:
Changes:
run_in_parallel: bool = Truefield toInputGuardraildataclass (maintains backward compatibility)@input_guardraildecorator to acceptrun_in_parallelparameterAgentRunner:asyncio.gatherdocs/guardrails.mdwith "Execution modes" section explaining use casesexamples/agent_patterns/input_guardrails.pyExecution Modes:
run_in_parallel=True, default): Guardrail runs concurrently with the agent for minimal latencyrun_in_parallel=False): Guardrail runs and completes before the agent starts, preventing any token consumption or tool execution if the guardrail triggersBenefits:
True, maintaining current behaviorTest plan
Tests Added:
test_input_guardrail_run_in_parallel_default- Verifies default isTruetest_input_guardrail_run_in_parallel_false- Verifies can be set toFalsetest_input_guardrail_decorator_with_run_in_parallel- Tests decorator parametertest_input_guardrail_decorator_with_name_and_run_in_parallel- Tests bothnameandrun_in_parallelparametersTests Run:
python -m ruff formatpython -m ruff check --fixpython -m mypy .(pre-existing optional dependency errors only)Verification:
Related issues
Closes #1985
Fixes #889
Fixes #991
Checklist:
make format/ruff formatrunmake lint/ruff checkrun