diff --git a/src/agents/function_schema.py b/src/agents/function_schema.py index 8860c15180..5b3a4357b8 100644 --- a/src/agents/function_schema.py +++ b/src/agents/function_schema.py @@ -511,6 +511,13 @@ def function_schema( ) # 3. Dynamically build a Pydantic model + if "model_config" in fields: + # Pydantic reads a ``model_config`` keyword as the model's configuration, not as a + # field, so create_model() fails deep inside Pydantic with an unhelpful TypeError. + raise UserError( + f"Parameter `model_config` in function {func_name} is reserved by Pydantic and cannot" + " be a tool argument. Rename the parameter." + ) 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 1d8325d1a0..8b282c7bed 100644 --- a/tests/test_function_schema.py +++ b/tests/test_function_schema.py @@ -1350,3 +1350,14 @@ def test_to_call_args_allows_kwargs_key_matching_var_positional_param() -> None: args, kwargs_dict = fs.to_call_args(parsed) assert _kwargs_var_positional_name(*args, **kwargs_dict) == ((1,), {"rest": 5}) + + +def test_model_config_parameter_raises_a_clear_user_error(): + """Pydantic treats a ``model_config`` kwarg to create_model() as the configuration, which + surfaced as ``TypeError: 'FieldInfo' object is not iterable``. Name the real problem.""" + + def configure(model_config: int) -> int: + return model_config + + with pytest.raises(UserError, match="`model_config` in function configure is reserved"): + function_schema(configure)