Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4854a74

Browse filesBrowse files
authored
Raise error on more invalid function schemas (openai#356)
Towards openai#345 ## Summary: Using a `dict` or `Mapping` isn't strict-mode compliant. But we were checking for the literal `True` whereas the value can also be an array, for example. Fix that. ## Test Plan: Unit tests
1 parent f99fa5f commit 4854a74
Copy full SHA for 4854a74

File tree

Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed

‎src/agents/strict_schema.py

Copy file name to clipboardExpand all lines: src/agents/strict_schema.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _ensure_strict_json_schema(
5454
elif (
5555
typ == "object"
5656
and "additionalProperties" in json_schema
57-
and json_schema["additionalProperties"] is True
57+
and json_schema["additionalProperties"]
5858
):
5959
raise UserError(
6060
"additionalProperties should not be set for object types. This could be because "

‎tests/test_function_schema.py

Copy file name to clipboardExpand all lines: tests/test_function_schema.py
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Mapping
12
from enum import Enum
23
from typing import Any, Literal
34

@@ -421,10 +422,20 @@ def test_var_keyword_dict_annotation():
421422
def func(**kwargs: dict[str, int]):
422423
return kwargs
423424

424-
fs = function_schema(func, use_docstring_info=False)
425+
fs = function_schema(func, use_docstring_info=False, strict_json_schema=False)
425426

426427
properties = fs.params_json_schema.get("properties", {})
427428
# The name of the field is "kwargs", and it's a JSON object i.e. a dict.
428429
assert properties.get("kwargs").get("type") == "object"
429430
# The values in the dict are integers.
430431
assert properties.get("kwargs").get("additionalProperties").get("type") == "integer"
432+
433+
434+
def test_schema_with_mapping_raises_strict_mode_error():
435+
"""A mapping type is not allowed in strict mode. Same for dicts. Ensure we raise a UserError."""
436+
437+
def func_with_mapping(test_one: Mapping[str, int]) -> str:
438+
return "foo"
439+
440+
with pytest.raises(UserError):
441+
function_schema(func_with_mapping)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.