From 95aa90b83f7da1c55e376062530f72269571c6d0 Mon Sep 17 00:00:00 2001 From: Ishaan Samantray Date: Sun, 31 May 2026 23:37:17 -0400 Subject: [PATCH] fix(function_schema): raise UserError for Pydantic protected-namespace param names Parameters named model_dump, model_dump_json, model_validate, model_validate_json, or model_validate_strings caused an opaque ValueError from inside Pydantic's create_model() instead of a clear SDK error. Guard against these names before calling create_model() and surface a UserError with an actionable message. Fixes #3549 --- src/agents/function_schema.py | 15 +++++++++++++++ tests/test_function_schema.py | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/agents/function_schema.py b/src/agents/function_schema.py index 8fe52df320..c0d8652b7c 100644 --- a/src/agents/function_schema.py +++ b/src/agents/function_schema.py @@ -404,6 +404,21 @@ def function_schema( ) # 3. Dynamically build a Pydantic model + # Pydantic raises ValueError for field names that match built-in BaseModel methods in a + # protected namespace (e.g. model_dump, model_validate). Catch these early. + _PYDANTIC_PROTECTED_NAMES = { + "model_dump", + "model_dump_json", + "model_validate", + "model_validate_json", + "model_validate_strings", + } + colliding = _PYDANTIC_PROTECTED_NAMES.intersection(fields) + if colliding: + raise UserError( + f"Function '{func_name}' has parameter(s) {sorted(colliding)} whose name(s) conflict " + "with Pydantic BaseModel built-in methods. Rename the parameter(s) to avoid this." + ) dynamic_model = create_model(f"{func_name}_args", __base__=BaseModel, **fields) # 4. Build JSON schema from that model diff --git a/tests/test_function_schema.py b/tests/test_function_schema.py index 9771bda99d..8aec1692e9 100644 --- a/tests/test_function_schema.py +++ b/tests/test_function_schema.py @@ -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_protected_namespace_param_raises_user_error(): + # Parameters named after Pydantic's protected BaseModel methods must raise UserError, + # not an opaque ValueError from inside Pydantic's create_model(). + def func_with_model_validate(model_validate: str, query: str) -> str: + return query + + with pytest.raises(UserError, match="model_validate"): + function_schema(func_with_model_validate, use_docstring_info=False)