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 all commits
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 @@ -191,6 +191,7 @@ def function_schema(
description_override: str | None = None,
use_docstring_info: bool = True,
strict_json_schema: bool = True,
enforce_type_annotations: bool = False,
) -> FuncSchema:
"""
Given a python function, extracts a `FuncSchema` from it, capturing the name, description,
Expand All @@ -209,6 +210,8 @@ def function_schema(
the schema adheres to the "strict" standard the OpenAI API expects. We **strongly**
recommend setting this to True, as it increases the likelihood of the LLM providing
correct JSON input.
enforce_type_annotations: If True, raises a ValueError for any unannotated parameters.
If False (default), unannotated parameters are assumed to be of type `Any`.

Returns:
A `FuncSchema` object containing the function's name, description, parameter descriptions,
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