Skip to content

feat(function_schema): add enforce_type_annotations flag for stricter schema validation with type enforcement, clearer errors, and updated docstrings #1092

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 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/agents/function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def generate_func_documentation(

def function_schema(
func: Callable[..., Any],
enforce_type_annotations: bool = False,
Copy link
Member

Choose a reason for hiding this comment

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

adding this option may be a good one, but adding here (=2nd arg) is a breaking change. new arguments must be added as the last one when a method accepts both args and keyword args.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! You're absolutely right, the enforce_type_annotations parameter has been moved to the end of the argument list in the latest update to preserve backward compatibility.

docstring_style: DocstringStyle | None = None,
name_override: str | None = None,
description_override: str | None = None,
Expand All @@ -198,6 +199,8 @@ def function_schema(

Args:
func: The function to extract the schema from.
enforce_type_annotations: If True, raises a ValueError for any unannotated parameters.
If False (default), unannotated parameters are assumed to be of type `Any`.
docstring_style: The style of the docstring to use for parsing. If not provided, we will
attempt to auto-detect the style.
name_override: If provided, use this name instead of the function's `__name__`.
Expand Down Expand Up @@ -266,9 +269,14 @@ def function_schema(
ann = type_hints.get(name, param.annotation)
default = param.default

# If there's no type hint, assume `Any`
# Raise an error for unannotated parameters if enforcement is on
if ann == inspect._empty:
ann = Any
if enforce_type_annotations:
raise ValueError(
f"Parameter '{name}' must be type-annotated. Example: def func({name}: str)"
)
else:
ann = Any # Fallback only if enforcement is off

# If a docstring param description exists, use it
field_description = param_descs.get(name, None)
Expand Down