Skip to content

Allow RunContext to not be documented when require_parameter_descriptions=True as it's not passed to the model anyway #1750

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 5 commits into from
May 22, 2025

Conversation

Kevsnz
Copy link
Contributor

@Kevsnz Kevsnz commented May 17, 2025

This PR refines the behavior of the require_parameter_descriptions=True argument in the @agent.tool and @agent.tool_plain decorators to be more permissive and robust. Previously, this setting exhibited incorrect behavior by raising errors in scenarios where docstrings either omitted descriptions for the RunContext parameter or included descriptions for parameters not present in the function signature. The latter case was particularly problematic as the error message incorrectly indicated missing descriptions when the issue was the presence of descriptions for non-existent parameters.

With this change, the following is now allowed when require_parameter_descriptions=True:

  • Ignoring RunContext Descriptions: The docstring of a tool function can now omit a description for the RunContext parameter. Since RunContext is not typically a parameter directly consumed by the agent's logic, requiring its description was often unnecessary and could clutter docstrings.
  • Silently Ignoring Descriptions for Non-Existent Parameters: Docstrings can now contain descriptions for parameters that do not exist in the function definition. Previously, this scenario would trigger a misleading error message ("Missing parameter descriptions for"). Now, these descriptions are silently ignored during validation, preventing unnecessary errors and providing more flexibility when maintaining docstrings during code refactoring.

Examples:

@agent.tool(require_parameter_descriptions=True)
def add_task(ctx: RunContext[TaskList], description: str) -> Task:
    """Adds a task to the list and returns it.

    :param description: The short description of the task.
    :return: The task that was added.
    """
    return ctx.deps.add_task(description)

Before this PR, with require_parameter_descriptions=True, an error would be raised because the docstring lacked a description for the ctx parameter. This is now allowed, streamlining docstrings for tools utilizing RunContext.

Next, consider a function with a parameter description that doesn't match a function parameter:

@agent.tool_plain(require_parameter_descriptions=True)
def get_time() -> datetime.time:
    """Returns the current time

    :param tz: Timezone
    :return: The current time of the day
    """
    return datetime.datetime.now().time()

Before this fix, the presence of the :param tz: in the docstring would incorrectly trigger the error message:

Missing parameter descriptions for

This was misleading because the issue wasn't a missing description, but rather the presence of a description for a non-existent parameter. Now, the description for the non-existent tz parameter is correctly and silently ignored, and no error is raised.

P.S. Yes, Gemini helped me rewrite and refine this description. However, I encountered the issues described above and made necessary changes in the code myself. Hope you'll find them helpful.

@DouweM
Copy link
Contributor

DouweM commented May 19, 2025

@Kevsnz Thanks! The first change (ctx) makes sense, but in the second case, wouldn't we want to complain if the description lists an argument that doesn't actually exist? Including it could confuse the model.

@Kevsnz
Copy link
Contributor Author

Kevsnz commented May 20, 2025

@Kevsnz Thanks! The first change (ctx) makes sense, but in the second case, wouldn't we want to complain if the description lists an argument that doesn't actually exist? Including it could confuse the model.

Good question. Intuitively I thought that @agent.tool would discard descriptions of non-existent parameters simply because there would be no place to put those descriptions.

To make sure I understand it correctly I ran a quick test with OTLP.
The following tool in the code:

@agent.tool_plain(require_parameter_descriptions=True)
def get_time() -> datetime.time:
    """Returns the current time

    :param bla: That is an unknown parameter!
    :return: The current time of the day
    """
    return datetime.datetime.now().time()

produced this tool in the request to the LLM provider:

{
	"tools": [
		{
			"type": "function",
			"function": {
				"name": "get_time",
				"description": "<summary>Returns the current time</summary>\n<returns>\n<description>The current time of the day</description>\n</returns>",
				"parameters": {
					"additionalProperties": false,
					"properties": {},
					"type": "object"
				}
			}
		}
	]
}

Sure enough, properties field is empty and there is no description of non-existent parameter bla.

So it looks like redundant params in docstrings could be safely ignored.

@DouweM DouweM changed the title Enhance require_parameter_descriptions=True Flexibility in @agent.tool and @agent.tool_plain Allow RunContext to not be documented when require_parameter_descriptions=True as it's not passed to the model anyway May 22, 2025
@DouweM DouweM merged commit ed2a8ad into pydantic:main May 22, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants