Skip to content

Set use_attribute_docstrings=True by default on tools #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
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
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def function_schema( # noqa: C901
Returns:
A `FunctionSchema` instance.
"""
config = ConfigDict(title=function.__name__)
config = ConfigDict(title=function.__name__, use_attribute_docstrings=True)
config_wrapper = ConfigWrapper(config)
gen_schema = _generate_schema.GenerateSchema(config_wrapper)

Expand Down
44 changes: 36 additions & 8 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
from pydantic import BaseModel, Field, WithJsonSchema
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
from pydantic_core import PydanticSerializationError, core_schema
from typing_extensions import TypedDict

from pydantic_ai import Agent, RunContext, Tool, ToolOutput, UserError
from pydantic_ai.messages import (
ModelMessage,
ModelRequest,
ModelResponse,
TextPart,
ToolCallPart,
ToolReturnPart,
)
from pydantic_ai.messages import ModelMessage, ModelRequest, ModelResponse, TextPart, ToolCallPart, ToolReturnPart
from pydantic_ai.models.function import AgentInfo, FunctionModel
from pydantic_ai.models.test import TestModel
from pydantic_ai.tools import ToolDefinition
Expand Down Expand Up @@ -926,3 +920,37 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
},
]
)


def test_tool_parameters_with_attribute_docstrings():
agent = Agent(FunctionModel(get_json_schema))

class Data(TypedDict):
a: int
"""The first parameter"""
b: int
"""The second parameter"""

@agent.tool_plain
def get_score(data: Data) -> int: ...

result = agent.run_sync('Hello')
json_schema = json.loads(result.output)
assert json_schema == snapshot(
{
'name': 'get_score',
'description': None,
'parameters_json_schema': {
'additionalProperties': False,
'properties': {
'a': {'description': 'The first parameter', 'type': 'integer'},
'b': {'description': 'The second parameter', 'type': 'integer'},
},
'required': ['a', 'b'],
'title': 'Data',
'type': 'object',
},
'outer_typed_dict_key': None,
'strict': None,
}
)