Add Runtime Validation for name
Attribute in Agent
Class
#1050
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.
This pull request proposes adding runtime validation to the
Agent
class in theopenai-agents-sdk
to ensure thename
attribute is a non-empty string. This change addresses the issue where non-string types (e.g., integers or dictionaries) are currently accepted, despite thestr
type annotation, which can lead to unexpected behavior. By implementing validation, we make the SDK more robust and user-friendly.Proposed Change
The proposed solution adds a
__post_init__
method to theAgent
class to validate thatname
is a string and not empty or whitespace-only. This approach is standard for Python dataclasses and does not affect other attributes or methods.Why It Matters
This change prevents potential errors, aligns with type annotations, and improves the user experience by providing clear feedback on invalid inputs. It benefits the community by making the SDK more reliable, encouraging adoption, and reducing support overhead.
The
__post_init__
method is the most suitable, as it is lightweight, aligns with Python dataclass conventions, and requires minimal changes to the existing codebase.