Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/agents/function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)