From 3800c4b4c1f68440adf2575fc96df1290aec7786 Mon Sep 17 00:00:00 2001 From: ferchobanana <98494417+ferchobanana@users.noreply.github.com> Date: Fri, 4 Jul 2025 05:49:50 -0600 Subject: [PATCH 1/3] Add optional parameter takes_ctx for context dependencies in tool created from Tool.from_schema() method --- pydantic_ai_slim/pydantic_ai/tools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pydantic_ai_slim/pydantic_ai/tools.py b/pydantic_ai_slim/pydantic_ai/tools.py index c22630d72..e3da76e7e 100644 --- a/pydantic_ai_slim/pydantic_ai/tools.py +++ b/pydantic_ai_slim/pydantic_ai/tools.py @@ -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. @@ -291,7 +292,7 @@ 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), ) From ed8c027ea5884274e118946080528b6021fb14d8 Mon Sep 17 00:00:00 2001 From: ferchobanana <98494417+ferchobanana@users.noreply.github.com> Date: Fri, 4 Jul 2025 06:07:27 -0600 Subject: [PATCH 2/3] Add optional parameter takes_ctx for context dependencies in tool created from Tool.from_schema() method --- pydantic_ai_slim/pydantic_ai/tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/tools.py b/pydantic_ai_slim/pydantic_ai/tools.py index e3da76e7e..dc84e5fcb 100644 --- a/pydantic_ai_slim/pydantic_ai/tools.py +++ b/pydantic_ai_slim/pydantic_ai/tools.py @@ -283,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 """ @@ -298,7 +300,7 @@ def from_schema( return cls( function, - takes_ctx=False, + takes_ctx=takes_ctx, name=name, description=description, function_schema=function_schema, From 0152797b61b594f720282ace3c367580c82757f5 Mon Sep 17 00:00:00 2001 From: ferchobanana <98494417+ferchobanana@users.noreply.github.com> Date: Fri, 4 Jul 2025 06:28:58 -0600 Subject: [PATCH 3/3] Add comma to pass CI / lint --- pydantic_ai_slim/pydantic_ai/tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic_ai_slim/pydantic_ai/tools.py b/pydantic_ai_slim/pydantic_ai/tools.py index dc84e5fcb..c1a385161 100644 --- a/pydantic_ai_slim/pydantic_ai/tools.py +++ b/pydantic_ai_slim/pydantic_ai/tools.py @@ -271,7 +271,7 @@ def from_schema( name: str, description: str, json_schema: JsonSchemaValue, - takes_ctx: bool = False + takes_ctx: bool = False, ) -> Self: """Creates a Pydantic tool from a function and a JSON schema.