- Notifications
You must be signed in to change notification settings - Fork2k
fix: fallback to function name for unnamed output_guardrail decorators#1133
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
rm-openai merged 3 commits intoopenai:mainfromromankhan26:fix/output-guardrail-name-fallbackJul 18, 2025
Merged
fix: fallback to function name for unnamed output_guardrail decorators#1133
rm-openai merged 3 commits intoopenai:mainfromromankhan26:fix/output-guardrail-name-fallbackJul 18, 2025
+5 −1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
seratch approved these changesJul 15, 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.
Similar change to#1053 ; it should be good to go
seratch requested changesJul 15, 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.
lint and test errors need to be resolved
@seratch |
seratch approved these changesJul 16, 2025
rm-openai approved these changesJul 18, 2025
4b8f40e
intoopenai:main 5 checks passed
Uh oh!
There was an error while loading.Please reload this page.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview:
This PR improves the output_guardrail behavior by ensuring a valid name is always assigned to the guardrail, even when the decorator is used without parentheses or without explicitly providing a name.
Problem:
Previously, when the decorator @output_guardrail was used without a name (and without parentheses), the name attribute of the guardrail remained None. This resulted in issues during runtime — specifically, the guardrail name did not appear in result.input_guardrail_results, making it harder to trace or debug guardrail outputs.
While the OutputGuardrail.get_name() method correctly defaults to the function name when name is None, this method is not used inside the decorator. Hence, unless a name is provided explicitly, the OutputGuardrail instance holds None for its name internally.
Solution:
This PR updates the decorator logic to:
Automatically fallback to the function name if the name parameter is not provided.
Ensure that the guardrail always has a meaningful identifier, which improves downstream behavior such as logging, debugging, and result tracing.
Example Behavior Before:
@output_guardrail
def validate_output(...):
Name remains None
Example Behavior After:
@output_guardrail
def validate_output(...):
Name becomes "validate_output" automatically
Why it matters:
This small change avoids hidden bugs or inconsistencies in downstream systems (like guardrail_results) that rely on guardrail names being defined. It also brings consistent behavior whether or not parentheses are used in the decorator.