|
9 | 9 | from pydantic import BaseModel, Field, WithJsonSchema
|
10 | 10 | from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
|
11 | 11 | from pydantic_core import PydanticSerializationError, core_schema
|
| 12 | +from typing_extensions import TypedDict |
12 | 13 |
|
13 | 14 | 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 |
22 | 16 | from pydantic_ai.models.function import AgentInfo, FunctionModel
|
23 | 17 | from pydantic_ai.models.test import TestModel
|
24 | 18 | from pydantic_ai.tools import ToolDefinition
|
@@ -926,3 +920,37 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
|
926 | 920 | },
|
927 | 921 | ]
|
928 | 922 | )
|
| 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