Skip to content

Commit 4b4c5f7

Browse files
authored
Set use_attribute_docstrings=True by default on tools (#1605)
1 parent 1a16ca3 commit 4b4c5f7

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

pydantic_ai_slim/pydantic_ai/_pydantic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def function_schema( # noqa: C901
5858
Returns:
5959
A `FunctionSchema` instance.
6060
"""
61-
config = ConfigDict(title=function.__name__)
61+
config = ConfigDict(title=function.__name__, use_attribute_docstrings=True)
6262
config_wrapper = ConfigWrapper(config)
6363
gen_schema = _generate_schema.GenerateSchema(config_wrapper)
6464

tests/test_tools.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99
from pydantic import BaseModel, Field, WithJsonSchema
1010
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
1111
from pydantic_core import PydanticSerializationError, core_schema
12+
from typing_extensions import TypedDict
1213

1314
from pydantic_ai import Agent, RunContext, Tool, ToolOutput, UserError
14-
from pydantic_ai.messages import (
15-
ModelMessage,
16-
ModelRequest,
17-
ModelResponse,
18-
TextPart,
19-
ToolCallPart,
20-
ToolReturnPart,
21-
)
15+
from pydantic_ai.messages import ModelMessage, ModelRequest, ModelResponse, TextPart, ToolCallPart, ToolReturnPart
2216
from pydantic_ai.models.function import AgentInfo, FunctionModel
2317
from pydantic_ai.models.test import TestModel
2418
from pydantic_ai.tools import ToolDefinition
@@ -926,3 +920,37 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
926920
},
927921
]
928922
)
923+
924+
925+
def test_tool_parameters_with_attribute_docstrings():
926+
agent = Agent(FunctionModel(get_json_schema))
927+
928+
class Data(TypedDict):
929+
a: int
930+
"""The first parameter"""
931+
b: int
932+
"""The second parameter"""
933+
934+
@agent.tool_plain
935+
def get_score(data: Data) -> int: ...
936+
937+
result = agent.run_sync('Hello')
938+
json_schema = json.loads(result.output)
939+
assert json_schema == snapshot(
940+
{
941+
'name': 'get_score',
942+
'description': None,
943+
'parameters_json_schema': {
944+
'additionalProperties': False,
945+
'properties': {
946+
'a': {'description': 'The first parameter', 'type': 'integer'},
947+
'b': {'description': 'The second parameter', 'type': 'integer'},
948+
},
949+
'required': ['a', 'b'],
950+
'title': 'Data',
951+
'type': 'object',
952+
},
953+
'outer_typed_dict_key': None,
954+
'strict': None,
955+
}
956+
)

0 commit comments

Comments
 (0)