Skip to content
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

feat: add docstring_format and require_parameter_descriptions #265

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions pydantic_ai_slim/pydantic_ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ def tool(
*,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDeps] | None = None,
docstring_format: Literal["google", "numpy", "sphinx", "auto"] = "auto",
require_parameter_descriptions: bool = False
) -> Callable[[ToolFuncContext[AgentDeps, ToolParams]], ToolFuncContext[AgentDeps, ToolParams]]: ...

def tool(
Expand All @@ -594,6 +596,8 @@ def tool(
*,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDeps] | None = None,
docstring_format: Literal["google", "numpy", "sphinx", "auto"] = "auto",
require_parameter_descriptions: bool = False
) -> Any:
"""Decorator to register a tool function which takes [`RunContext`][pydantic_ai.tools.RunContext] as its first argument.

Expand Down Expand Up @@ -638,13 +642,13 @@ def tool_decorator(
func_: ToolFuncContext[AgentDeps, ToolParams],
) -> ToolFuncContext[AgentDeps, ToolParams]:
# noinspection PyTypeChecker
self._register_function(func_, True, retries, prepare)
self._register_function(func_, True, retries, prepare, docstring_format, require_parameter_descriptions)
return func_

return tool_decorator
else:
# noinspection PyTypeChecker
self._register_function(func, True, retries, prepare)
self._register_function(func, True, retries, prepare, docstring_format, require_parameter_descriptions)
return func

@overload
Expand Down Expand Up @@ -722,10 +726,13 @@ def _register_function(
takes_ctx: bool,
retries: int | None,
prepare: ToolPrepareFunc[AgentDeps] | None,
docstring_format: Literal["google", "numpy", "sphinx", "auto"] = "auto",
require_parameter_descriptions: bool = False
) -> None:
"""Private utility to register a function as a tool."""
retries_ = retries if retries is not None else self._default_retries
tool = Tool(func, takes_ctx=takes_ctx, max_retries=retries_, prepare=prepare)
tool = Tool(func, takes_ctx=takes_ctx, max_retries=retries_, prepare=prepare, docstring_format=docstring_format,
require_parameter_descriptions=require_parameter_descriptions)
self._register_tool(tool)

def _register_tool(self, tool: Tool[AgentDeps]) -> None:
Expand Down
8 changes: 7 additions & 1 deletion pydantic_ai_slim/pydantic_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
from collections.abc import Awaitable
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union, cast
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union, cast, Literal

from pydantic import ValidationError
from pydantic_core import SchemaValidator
Expand Down Expand Up @@ -144,6 +144,8 @@ class Tool(Generic[AgentDeps]):
_validator: SchemaValidator = field(init=False, repr=False)
_parameters_json_schema: ObjectJsonSchema = field(init=False)
current_retry: int = field(default=0, init=False)
docstring_format: Literal["google", "numpy", "sphinx", "auto"] = "auto"
require_parameter_descriptions: bool = False

def __init__(
self,
Expand All @@ -154,6 +156,8 @@ def __init__(
name: str | None = None,
description: str | None = None,
prepare: ToolPrepareFunc[AgentDeps] | None = None,
docstring_format: Literal["google", "numpy", "sphinx", "auto"] = "auto",
require_parameter_descriptions: bool = False
):
"""Create a new tool instance.

Expand Down Expand Up @@ -211,6 +215,8 @@ async def prep_my_tool(
self.name = name or function.__name__
self.description = description or f['description']
self.prepare = prepare
self.docstring_format = docstring_format
self.require_parameter_descriptions = require_parameter_descriptions
self._is_async = inspect.iscoroutinefunction(self.function)
self._single_arg_name = f['single_arg_name']
self._positional_fields = f['positional_fields']
Expand Down
Loading