Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/agents/function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,19 @@ def function_schema(
# field_name -> (type_annotation, default_value_or_Field(...))
fields: dict[str, Any] = {}

# Pydantic's create_model() treats certain names as model-level configuration
# keys rather than fields (e.g. 'model_config', 'model_fields'). Passing
# a FieldInfo for those names causes a TypeError deep inside Pydantic, so
# we reject them early with a clear message.
_PYDANTIC_RESERVED_NAMES = {"model_config", "model_fields", "model_computed_fields"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include current Pydantic protected method names

With the repo pinned to Pydantic >=2.12.2, Pydantic's default protected namespaces are ('model_validate', 'model_dump') (see https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.protected_namespaces), and fields that exactly collide with existing BaseModel members still raise from Pydantic during create_model. Because this new reserved-name guard omits model_validate and model_dump, a tool like def f(model_dump: str) still bypasses the intended UserError and surfaces a lower-level Pydantic exception; please include those current protected member names in the early check as well.

Useful? React with 👍 / 👎.


for name, param in filtered_params:
if name in _PYDANTIC_RESERVED_NAMES:
raise UserError(
f"Function '{func.__name__}' has a parameter named '{name}', which is reserved "
f"by Pydantic and cannot be used as a tool parameter name. "
f"Please rename the parameter."
)
ann = type_hints.get(name, param.annotation)
default = param.default

Expand Down
10 changes: 10 additions & 0 deletions tests/test_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,13 @@ def func_with_annotated_multiple_field_constraints(

with pytest.raises(ValidationError): # zero factor
fs.params_pydantic_model(**{"score": 50, "factor": 0.0})


def test_pydantic_reserved_param_name_raises_user_error():
"""Parameters named 'model_config' (or other Pydantic reserved names) must raise UserError."""

def func_with_reserved(model_config: str) -> str:
return model_config

with pytest.raises(UserError, match="reserved by Pydantic"):
function_schema(func_with_reserved)