perf(core): invalidate cached tool_call_schema and args on field mutation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Sydney Runkle
2026-04-21 09:57:07 -04:00
parent 264a82c6ef
commit 0f042990e3
2 changed files with 45 additions and 0 deletions

View File

@@ -555,6 +555,12 @@ class ChildTool(BaseTool):
ignored_types=(functools.cached_property,),
)
def __setattr__(self, name: str, value: Any) -> None:
super().__setattr__(name, value)
if name in {"args_schema", "description", "name"}:
self.__dict__.pop("tool_call_schema", None)
self.__dict__.pop("args", None)
@property
def is_single_input(self) -> bool:
"""Check if the tool accepts only a single input argument.

View File

@@ -3653,3 +3653,42 @@ def test_tool_default_factory_not_required() -> None:
schema = convert_to_openai_tool(some_func)
params = schema["function"]["parameters"]
assert "names" not in params.get("required", [])
def test_tool_call_schema_cache_invalidated_on_field_mutation() -> None:
"""Verify cached tool_call_schema is invalidated when schema-affecting fields change."""
@tool
def my_tool(x: int) -> str:
"""Original description."""
return str(x)
schema_before = my_tool.tool_call_schema
my_tool.description = "Updated description"
schema_after = my_tool.tool_call_schema
assert schema_before is not schema_after
assert schema_after.__doc__ == "Updated description"
def test_args_cache_invalidated_on_args_schema_change() -> None:
"""Verify cached args is invalidated when args_schema changes."""
class SchemaA(BaseModel):
x: int
class SchemaB(BaseModel):
y: str
@tool(args_schema=SchemaA)
def my_tool(x: int) -> str:
"""A tool."""
return str(x)
args_before = my_tool.args
assert "x" in args_before
my_tool.args_schema = SchemaB
args_after = my_tool.args
assert "y" in args_after
assert "x" not in args_after