Skip to content

Add context dependencies to Tool.from_schema() #2130

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions pydantic_ai_slim/pydantic_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def from_schema(
name: str,
description: str,
json_schema: JsonSchemaValue,
takes_ctx: bool = False,
) -> Self:
"""Creates a Pydantic tool from a function and a JSON schema.

Expand All @@ -282,7 +283,9 @@ def from_schema(
description: Used to tell the model how/when/why to use the tool.
You can provide few-shot examples as a part of the description.
json_schema: The schema for the function arguments

takes_ctx: An optional boolean parameter indicating whether the function
accepts the context object as an argument.

Returns:
A Pydantic tool that calls the function
"""
Expand All @@ -291,13 +294,13 @@ def from_schema(
description=description,
validator=SchemaValidator(schema=core_schema.any_schema()),
json_schema=json_schema,
takes_ctx=False,
takes_ctx=takes_ctx,
is_async=_utils.is_async_callable(function),
)

return cls(
function,
takes_ctx=False,
takes_ctx=takes_ctx,
name=name,
description=description,
function_schema=function_schema,
Expand Down
Loading