Skip to content

Raise error on more invalid function schemas #356

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/agents/strict_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _ensure_strict_json_schema(
elif (
typ == "object"
and "additionalProperties" in json_schema
and json_schema["additionalProperties"] is True
and json_schema["additionalProperties"]
):
raise UserError(
"additionalProperties should not be set for object types. This could be because "
Expand Down
13 changes: 12 additions & 1 deletion tests/test_function_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Mapping
from enum import Enum
from typing import Any, Literal

Expand Down Expand Up @@ -421,10 +422,20 @@ def test_var_keyword_dict_annotation():
def func(**kwargs: dict[str, int]):
return kwargs

fs = function_schema(func, use_docstring_info=False)
fs = function_schema(func, use_docstring_info=False, strict_json_schema=False)

properties = fs.params_json_schema.get("properties", {})
# The name of the field is "kwargs", and it's a JSON object i.e. a dict.
assert properties.get("kwargs").get("type") == "object"
# The values in the dict are integers.
assert properties.get("kwargs").get("additionalProperties").get("type") == "integer"


def test_schema_with_mapping_raises_strict_mode_error():
"""A mapping type is not allowed in strict mode. Same for dicts. Ensure we raise a UserError."""

def func_with_mapping(test_one: Mapping[str, int]) -> str:
return "foo"

with pytest.raises(UserError):
function_schema(func_with_mapping)