Skip to content

Commit b64cad4

Browse files
fix(schema): decode URI fragments before pointer traversal (#5272)
1 parent 36d7944 commit b64cad4

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎src/agents/strict_schema.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import copy
44
from typing import Any, TypeGuard, cast
5+
from urllib.parse import unquote
56

67
from openai import NOT_GIVEN
78

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

418-
path = ref[2:].split("/")
419+
# Decode the URI fragment before interpreting JSON Pointer separators and escapes.
420+
path = unquote(ref[2:], errors="strict").split("/")
419421
resolved = root
420422
for raw_key in path:
421423
key = raw_key.replace("~1", "/").replace("~0", "~")

‎tests/test_function_tool.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,100 @@ async def run_function(ctx: ToolContext[Any], args: str) -> str:
432432
}
433433

434434

435+
@pytest.mark.parametrize(
436+
("definition_name", "ref"),
437+
[
438+
("Value", "#/$defs/Value"),
439+
("A B", "#/$defs/A%20B"),
440+
("café", "#/$defs/caf%C3%A9"),
441+
("A%20B", "#/$defs/A%2520B"),
442+
("a/b", "#/$defs/a%7E1b"),
443+
("a~1b", "#/$defs/a%7E01b"),
444+
("A+B", "#/$defs/A+B"),
445+
("A+B", "#/$defs/A%2BB"),
446+
("Value", "#/%24defs%2FValue"),
447+
],
448+
)
449+
def test_function_tool_uri_fragment_preserves_schema_meaning(definition_name, ref):
450+
async def run_function(ctx: ToolContext[Any], args: str) -> str:
451+
return args
452+
453+
schema = {
454+
"type": "object",
455+
"properties": {"value": {"$ref": ref, "description": "value"}},
456+
"$defs": {definition_name: {"type": "string"}},
457+
}
458+
validator = Draft202012Validator(schema)
459+
assert validator.is_valid({"value": "text"})
460+
assert not validator.is_valid({"value": 1})
461+
462+
tool = FunctionTool(
463+
name="test", description="test", params_json_schema=schema, on_invoke_tool=run_function
464+
)
465+
466+
assert tool.params_json_schema["properties"]["value"] == {
467+
"type": "string",
468+
"description": "value",
469+
}
470+
strict_validator = Draft202012Validator(tool.params_json_schema)
471+
assert strict_validator.is_valid({"value": "text"})
472+
assert not strict_validator.is_valid({"value": 1})
473+
474+
475+
def test_function_tool_uri_fragment_does_not_select_literal_percent_name():
476+
async def run_function(ctx: ToolContext[Any], args: str) -> str:
477+
return args
478+
479+
tool = FunctionTool(
480+
name="test",
481+
description="test",
482+
params_json_schema={
483+
"type": "object",
484+
"properties": {"value": {"$ref": "#/$defs/A%20B", "description": "value"}},
485+
"$defs": {"A B": {"type": "string"}, "A%20B": {"type": "integer"}},
486+
},
487+
on_invoke_tool=run_function,
488+
)
489+
490+
validator = Draft202012Validator(tool.params_json_schema)
491+
assert validator.is_valid({"value": "text"})
492+
assert not validator.is_valid({"value": 1})
493+
494+
495+
def test_function_tool_uri_fragment_rejects_invalid_utf8():
496+
async def run_function(ctx: ToolContext[Any], args: str) -> str:
497+
return args
498+
499+
with pytest.raises(UnicodeDecodeError):
500+
FunctionTool(
501+
name="test",
502+
description="test",
503+
params_json_schema={
504+
"type": "object",
505+
"properties": {"value": {"$ref": "#/$defs/%FF", "description": "value"}},
506+
"$defs": {"\ufffd": {"type": "string"}},
507+
},
508+
on_invoke_tool=run_function,
509+
)
510+
511+
512+
def test_function_tool_uri_fragment_rejects_nested_resource():
513+
async def run_function(ctx: ToolContext[Any], args: str) -> str:
514+
return args
515+
516+
with pytest.raises(UserError, match=r"nested `\$id` resource"):
517+
FunctionTool(
518+
name="test",
519+
description="test",
520+
params_json_schema={
521+
"type": "object",
522+
"properties": {"value": {"$ref": "#/$defs/A%20B", "description": "value"}},
523+
"$defs": {"A B": {"$id": "https://example.test/nested", "type": "string"}},
524+
},
525+
on_invoke_tool=run_function,
526+
)
527+
528+
435529
def test_manual_function_tool_rejects_root_union():
436530
async def run_function(ctx: ToolContext[Any], args: str) -> str:
437531
return args

0 commit comments

Comments
 (0)