Skip to content
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
4 changes: 3 additions & 1 deletion src/agents/strict_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
from typing import Any, TypeGuard, cast
from urllib.parse import unquote

from openai import NOT_GIVEN

Expand Down Expand Up @@ -415,7 +416,8 @@ def resolve_ref(*, root: dict[str, object], ref: str) -> object:
if not ref.startswith("#/"):
raise ValueError(f"Unexpected $ref format {ref!r}; Does not start with #/")

path = ref[2:].split("/")
# Decode the URI fragment before interpreting JSON Pointer separators and escapes.
path = unquote(ref[2:], errors="strict").split("/")
resolved = root
for raw_key in path:
key = raw_key.replace("~1", "/").replace("~0", "~")
Expand Down
94 changes: 94 additions & 0 deletions tests/test_function_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,100 @@ async def run_function(ctx: ToolContext[Any], args: str) -> str:
}


@pytest.mark.parametrize(
("definition_name", "ref"),
[
("Value", "#/$defs/Value"),
("A B", "#/$defs/A%20B"),
("café", "#/$defs/caf%C3%A9"),
("A%20B", "#/$defs/A%2520B"),
("a/b", "#/$defs/a%7E1b"),
("a~1b", "#/$defs/a%7E01b"),
("A+B", "#/$defs/A+B"),
("A+B", "#/$defs/A%2BB"),
("Value", "#/%24defs%2FValue"),
],
)
def test_function_tool_uri_fragment_preserves_schema_meaning(definition_name, ref):
async def run_function(ctx: ToolContext[Any], args: str) -> str:
return args

schema = {
"type": "object",
"properties": {"value": {"$ref": ref, "description": "value"}},
"$defs": {definition_name: {"type": "string"}},
}
validator = Draft202012Validator(schema)
assert validator.is_valid({"value": "text"})
assert not validator.is_valid({"value": 1})

tool = FunctionTool(
name="test", description="test", params_json_schema=schema, on_invoke_tool=run_function
)

assert tool.params_json_schema["properties"]["value"] == {
"type": "string",
"description": "value",
}
strict_validator = Draft202012Validator(tool.params_json_schema)
assert strict_validator.is_valid({"value": "text"})
assert not strict_validator.is_valid({"value": 1})


def test_function_tool_uri_fragment_does_not_select_literal_percent_name():
async def run_function(ctx: ToolContext[Any], args: str) -> str:
return args

tool = FunctionTool(
name="test",
description="test",
params_json_schema={
"type": "object",
"properties": {"value": {"$ref": "#/$defs/A%20B", "description": "value"}},
"$defs": {"A B": {"type": "string"}, "A%20B": {"type": "integer"}},
},
on_invoke_tool=run_function,
)

validator = Draft202012Validator(tool.params_json_schema)
assert validator.is_valid({"value": "text"})
assert not validator.is_valid({"value": 1})


def test_function_tool_uri_fragment_rejects_invalid_utf8():
async def run_function(ctx: ToolContext[Any], args: str) -> str:
return args

with pytest.raises(UnicodeDecodeError):
FunctionTool(
name="test",
description="test",
params_json_schema={
"type": "object",
"properties": {"value": {"$ref": "#/$defs/%FF", "description": "value"}},
"$defs": {"\ufffd": {"type": "string"}},
},
on_invoke_tool=run_function,
)


def test_function_tool_uri_fragment_rejects_nested_resource():
async def run_function(ctx: ToolContext[Any], args: str) -> str:
return args

with pytest.raises(UserError, match=r"nested `\$id` resource"):
FunctionTool(
name="test",
description="test",
params_json_schema={
"type": "object",
"properties": {"value": {"$ref": "#/$defs/A%20B", "description": "value"}},
"$defs": {"A B": {"$id": "https://example.test/nested", "type": "string"}},
},
on_invoke_tool=run_function,
)


def test_manual_function_tool_rejects_root_union():
async def run_function(ctx: ToolContext[Any], args: str) -> str:
return args
Expand Down
Loading