Skip to content

Commit a08a20e

Browse files
dmontaguDouweM
andauthored
Make ToolDefinition.description optional and fix Bedrock description handling (#1507)
Co-authored-by: Douwe Maan <douwe@pydantic.dev>
1 parent 5f4661f commit a08a20e

21 files changed

+81
-74
lines changed

docs/direct.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def main():
6666
function_tools=[
6767
ToolDefinition(
6868
name=Divide.__name__.lower(),
69-
description=Divide.__doc__ or '',
69+
description=Divide.__doc__,
7070
parameters_json_schema=Divide.model_json_schema(),
7171
)
7272
],

docs/tools.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ print(test_model.last_model_request_parameters.function_tools)
451451
[
452452
ToolDefinition(
453453
name='foobar',
454-
description='This is a Foobar',
455454
parameters_json_schema={
456455
'properties': {
457456
'x': {'type': 'integer'},
@@ -462,6 +461,7 @@ print(test_model.last_model_request_parameters.function_tools)
462461
'title': 'Foobar',
463462
'type': 'object',
464463
},
464+
description='This is a Foobar',
465465
)
466466
]
467467
"""
@@ -591,7 +591,6 @@ print(test_model.last_model_request_parameters.function_tools)
591591
[
592592
ToolDefinition(
593593
name='greet',
594-
description='',
595594
parameters_json_schema={
596595
'additionalProperties': False,
597596
'properties': {

pydantic_ai_slim/pydantic_ai/_function_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FunctionSchema:
3535
"""Internal information about a function schema."""
3636

3737
function: Callable[..., Any]
38-
description: str
38+
description: str | None
3939
validator: SchemaValidator
4040
json_schema: ObjectJsonSchema
4141
# if not None, the function takes a single by that name (besides potentially `info`)

pydantic_ai_slim/pydantic_ai/_griffe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def doc_descriptions(
1919
sig: Signature,
2020
*,
2121
docstring_format: DocstringFormat,
22-
) -> tuple[str, dict[str, str]]:
22+
) -> tuple[str | None, dict[str, str]]:
2323
"""Extract the function description and parameter descriptions from a function's docstring.
2424
2525
The function parses the docstring using the specified format (or infers it if 'auto')
@@ -35,7 +35,7 @@ def doc_descriptions(
3535
"""
3636
doc = func.__doc__
3737
if doc is None:
38-
return '', {}
38+
return None, {}
3939

4040
# see https://github.com/mkdocstrings/griffe/issues/293
4141
parent = cast(GriffeObject, sig)

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def list_tools(self) -> list[tools.ToolDefinition]:
9898
return [
9999
tools.ToolDefinition(
100100
name=self.get_prefixed_tool_name(tool.name),
101-
description=tool.description or '',
101+
description=tool.description,
102102
parameters_json_schema=tool.inputSchema,
103103
)
104104
for tool in mcp_tools.tools

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ async def _map_user_prompt(
416416
def _map_tool_definition(f: ToolDefinition) -> BetaToolParam:
417417
return {
418418
'name': f.name,
419-
'description': f.description,
419+
'description': f.description or '',
420420
'input_schema': f.parameters_json_schema,
421421
}
422422

pydantic_ai_slim/pydantic_ai/models/bedrock.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
SystemContentBlockTypeDef,
6363
ToolChoiceTypeDef,
6464
ToolConfigurationTypeDef,
65+
ToolSpecificationTypeDef,
6566
ToolTypeDef,
6667
VideoBlockTypeDef,
6768
)
@@ -228,14 +229,16 @@ def _get_tools(self, model_request_parameters: ModelRequestParameters) -> list[T
228229

229230
@staticmethod
230231
def _map_tool_definition(f: ToolDefinition) -> ToolTypeDef:
231-
return {
232-
'toolSpec': {
233-
'name': f.name,
234-
'description': f.description,
235-
'inputSchema': {'json': f.parameters_json_schema},
236-
}
232+
tool_spec: ToolSpecificationTypeDef = {
233+
'name': f.name,
234+
'inputSchema': {'json': f.parameters_json_schema},
237235
}
238236

237+
if f.description: # pragma: no branch
238+
tool_spec['description'] = f.description
239+
240+
return {'toolSpec': tool_spec}
241+
239242
@property
240243
def base_url(self) -> str:
241244
return str(self.client.meta.endpoint_url)

pydantic_ai_slim/pydantic_ai/models/gemini.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ class _GeminiFunction(TypedDict):
773773

774774
def _function_from_abstract_tool(tool: ToolDefinition) -> _GeminiFunction:
775775
json_schema = tool.parameters_json_schema
776-
f = _GeminiFunction(name=tool.name, description=tool.description, parameters=json_schema)
776+
f = _GeminiFunction(name=tool.name, description=tool.description or '', parameters=json_schema)
777777
return f
778778

779779

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def _function_declaration_from_tool(tool: ToolDefinition) -> FunctionDeclaration
534534
json_schema = tool.parameters_json_schema
535535
f = FunctionDeclarationDict(
536536
name=tool.name,
537-
description=tool.description,
537+
description=tool.description or '',
538538
parameters=json_schema, # type: ignore
539539
)
540540
return f

pydantic_ai_slim/pydantic_ai/models/groq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def _map_tool_definition(f: ToolDefinition) -> chat.ChatCompletionToolParam:
333333
'type': 'function',
334334
'function': {
335335
'name': f.name,
336-
'description': f.description,
336+
'description': f.description or '',
337337
'parameters': f.parameters_json_schema,
338338
},
339339
}

0 commit comments

Comments
 (0)