Skip to content

Fix: Ensure output_guardrail provides function name instead of defaulting to None #1003

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

romankhan26
Copy link

This PR improves the output_guardrail feature by ensuring that a valid function name is always provided. Previously, if the name attribute was missing, it defaulted to None. This change introduces a fallback name to avoid unexpected behavior or errors in downstream processes.

@seratch seratch added enhancement New feature or request feature:core labels Jul 8, 2025
Copy link
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually hang on i dont get it - OutputGuardrail.get_name already does this. Whats the issue this fixes?

@romankhan26
Copy link
Author

@rm-openai
If we create output_guardrail using the decorator without parentheses, it does not assign a name to the guardrail based on the function's name. Instead, the name remains None until the end (guardrail_results). However, if we use parentheses in the decorator and explicitly assign a name, it sets the name as expected. This code ensures that if we do not explicitly define the name in the decorator, the function name will be used as the guardrail name instead of leaving it as None.

Moreover, The thing you mentioned about OutputGuardrail.get_name() doing the same is true, but the issue is that get_name() is not used in the decorator. As a result, the name remains None until we explicitly define it, like @output_guardrail(name='guardrail_name').

@rm-openai
Copy link
Collaborator

That makes sense, but what's the underlying problem that causes?

@romankhan26
Copy link
Author

I was not getting the name of the guardrails in my result.input_guardrail_results

@seratch
Copy link
Member

seratch commented Jul 10, 2025

Can you fix the lint error if you have time for it?

Run make lint
uv run ruff check
src/agents/guardrail.py:313:101: E501 Line too long (102 > 100)
    |
311 |         f: _OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co],
312 |     ) -> OutputGuardrail[TContext_co]:
313 |         #Updating default name None to the function name of guardrail_function if no name is provided.
    |                                                                                                     ^^ E501
314 |         return OutputGuardrail(guardrail_function=f, name=name if name else f.__name__)
    |

Found 1 error.
make: *** [Makefile:[12](https://github.com/openai/openai-agents-python/actions/runs/16059046857/job/45559636014?pr=1003#step:5:13): lint] Error 1
Error: Process completed with exit code 2.

@romankhan26
Copy link
Author

@seratch
Fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants