|
3 | 3 | from typing import Annotated, Any, Literal |
4 | 4 |
|
5 | 5 | import pytest |
6 | | -from pydantic import BaseModel, Field, ValidationError |
| 6 | +from pydantic import AfterValidator, BaseModel, Field, PositiveInt, TypeAdapter, ValidationError |
7 | 7 | from pydantic.json_schema import PydanticJsonSchemaWarning |
8 | 8 | from typing_extensions import TypedDict |
9 | 9 |
|
10 | | -from agents import RunContextWrapper, function_tool |
| 10 | +from agents import Agent, RunConfig, RunContextWrapper, Runner, function_tool |
| 11 | +from agents.decorators import tool |
11 | 12 | from agents.exceptions import ModelBehaviorError, UserError |
12 | 13 | from agents.function_schema import function_schema, generate_func_documentation |
| 14 | +from agents.testing import ScriptedModel, function_call |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def no_args_function(): |
@@ -1432,3 +1434,157 @@ def test_to_call_args_allows_kwargs_key_matching_var_positional_param() -> None: |
1432 | 1434 | args, kwargs_dict = fs.to_call_args(parsed) |
1433 | 1435 |
|
1434 | 1436 | assert _kwargs_var_positional_name(*args, **kwargs_dict) == ((1,), {"rest": 5}) |
| 1437 | + |
| 1438 | + |
| 1439 | +@pytest.mark.parametrize( |
| 1440 | + "annotation, invalid_value, bounds", |
| 1441 | + [ |
| 1442 | + (PositiveInt, -1, {"exclusiveMinimum": 0}), |
| 1443 | + (Annotated[int, Field(ge=0), Field(le=10)], 20, {"minimum": 0, "maximum": 10}), |
| 1444 | + ], |
| 1445 | +) |
| 1446 | +@pytest.mark.parametrize("kind", ["ordinary", "positional", "keyword"]) |
| 1447 | +@pytest.mark.asyncio |
| 1448 | +async def test_annotated_constraints_reject_arguments_before_tool_execution( |
| 1449 | + annotation, invalid_value, bounds, kind |
| 1450 | +): |
| 1451 | + invocations = [] |
| 1452 | + |
| 1453 | + if kind == "ordinary": |
| 1454 | + |
| 1455 | + def update_ordinary(ctx: Annotated[RunContextWrapper[None], "Context"], quantity): |
| 1456 | + invocations.append(quantity) |
| 1457 | + return str(quantity) |
| 1458 | + |
| 1459 | + update_inventory = update_ordinary |
| 1460 | + valid_payload = {"quantity": 5} |
| 1461 | + invalid_payload = {"quantity": invalid_value} |
| 1462 | + elif kind == "positional": |
| 1463 | + |
| 1464 | + def update_positional(*quantity): |
| 1465 | + invocations.extend(quantity) |
| 1466 | + return str(quantity[0]) |
| 1467 | + |
| 1468 | + update_inventory = update_positional |
| 1469 | + valid_payload = {"quantity": [5]} |
| 1470 | + invalid_payload = {"quantity": [invalid_value]} |
| 1471 | + else: |
| 1472 | + |
| 1473 | + def update_keyword(**quantity): |
| 1474 | + invocations.extend(quantity.values()) |
| 1475 | + return str(quantity["value"]) |
| 1476 | + |
| 1477 | + update_inventory = update_keyword |
| 1478 | + valid_payload = {"quantity": {"value": 5}} |
| 1479 | + invalid_payload = {"quantity": {"value": invalid_value}} |
| 1480 | + |
| 1481 | + update_inventory.__annotations__["quantity"] = annotation |
| 1482 | + decorated = tool( |
| 1483 | + update_inventory, |
| 1484 | + name_override="update_inventory", |
| 1485 | + strict_mode=kind != "keyword", |
| 1486 | + failure_error_function=None, |
| 1487 | + ) |
| 1488 | + value_schema = decorated.params_json_schema["properties"]["quantity"] |
| 1489 | + if kind != "ordinary": |
| 1490 | + value_schema = value_schema["items" if kind == "positional" else "additionalProperties"] |
| 1491 | + assert value_schema["type"] == "integer" |
| 1492 | + for key, value in bounds.items(): |
| 1493 | + assert value_schema[key] == value |
| 1494 | + |
| 1495 | + with pytest.raises(ValidationError): |
| 1496 | + TypeAdapter(annotation).validate_python(invalid_value) |
| 1497 | + |
| 1498 | + async def run(payload): |
| 1499 | + agent = Agent( |
| 1500 | + name="inventory", |
| 1501 | + tools=[decorated], |
| 1502 | + model=ScriptedModel([[function_call("update_inventory", payload, call_id="call-1")]]), |
| 1503 | + tool_use_behavior="stop_on_first_tool", |
| 1504 | + ) |
| 1505 | + return await Runner.run( |
| 1506 | + agent, "Update inventory", run_config=RunConfig(tracing_disabled=True) |
| 1507 | + ) |
| 1508 | + |
| 1509 | + with pytest.raises(ModelBehaviorError, match="Invalid JSON input for tool"): |
| 1510 | + await run(invalid_payload) |
| 1511 | + assert invocations == [] |
| 1512 | + |
| 1513 | + result = await run(valid_payload) |
| 1514 | + assert result.final_output == "5" |
| 1515 | + assert invocations == [5] |
| 1516 | + |
| 1517 | + |
| 1518 | +@pytest.mark.parametrize("default", [0, Field(default=0)]) |
| 1519 | +def test_annotated_metadata_preserves_validator_order(default): |
| 1520 | + def add_one(value: int) -> int: |
| 1521 | + return value + 1 |
| 1522 | + |
| 1523 | + def double(value: int) -> int: |
| 1524 | + return value * 2 |
| 1525 | + |
| 1526 | + def func( |
| 1527 | + value: Annotated[ |
| 1528 | + int, AfterValidator(add_one), Field(ge=0), AfterValidator(double) |
| 1529 | + ] = default, |
| 1530 | + ): |
| 1531 | + return value |
| 1532 | + |
| 1533 | + fs = function_schema(func) |
| 1534 | + parsed = fs.params_pydantic_model.model_validate({"value": 2}) |
| 1535 | + args, kwargs = fs.to_call_args(parsed) |
| 1536 | + assert func(*args, **kwargs) == 6 |
| 1537 | + |
| 1538 | + |
| 1539 | +def test_annotated_fields_preserve_description_and_default_precedence(): |
| 1540 | + def func( |
| 1541 | + quantity: Annotated[ |
| 1542 | + int, |
| 1543 | + Field(default=1, ge=0, description="First field"), |
| 1544 | + Field(le=10, description="Second field"), |
| 1545 | + "Annotated description", |
| 1546 | + ] = 5, |
| 1547 | + ): |
| 1548 | + """Update inventory. |
| 1549 | +
|
| 1550 | + Args: |
| 1551 | + quantity: Docstring description. |
| 1552 | + """ |
| 1553 | + return quantity |
| 1554 | + |
| 1555 | + for use_docstring_info, expected_description in ( |
| 1556 | + (True, "Docstring description."), |
| 1557 | + (False, "Annotated description"), |
| 1558 | + ): |
| 1559 | + fs = function_schema(func, use_docstring_info=use_docstring_info) |
| 1560 | + quantity = fs.params_json_schema["properties"]["quantity"] |
| 1561 | + assert quantity["minimum"] == 0 |
| 1562 | + assert quantity["maximum"] == 10 |
| 1563 | + assert quantity["description"] == expected_description |
| 1564 | + assert quantity["default"] == 5 |
| 1565 | + assert fs.params_pydantic_model().quantity == 5 |
| 1566 | + |
| 1567 | + |
| 1568 | +def test_annotated_description_overrides_field_default_description(): |
| 1569 | + def func( |
| 1570 | + quantity: Annotated[int, "Annotated description"] = Field( |
| 1571 | + default=5, ge=0, description="Default description" |
| 1572 | + ), |
| 1573 | + ): |
| 1574 | + """Update inventory. |
| 1575 | +
|
| 1576 | + Args: |
| 1577 | + quantity: Docstring description. |
| 1578 | + """ |
| 1579 | + return quantity |
| 1580 | + |
| 1581 | + for use_docstring_info, expected_description in ( |
| 1582 | + (True, "Docstring description."), |
| 1583 | + (False, "Annotated description"), |
| 1584 | + ): |
| 1585 | + fs = function_schema(func, use_docstring_info=use_docstring_info) |
| 1586 | + quantity = fs.params_json_schema["properties"]["quantity"] |
| 1587 | + assert quantity["description"] == expected_description |
| 1588 | + assert quantity["minimum"] == 0 |
| 1589 | + assert quantity["default"] == 5 |
| 1590 | + assert fs.params_pydantic_model().quantity == 5 |
0 commit comments