mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-23 20:23:59 +00:00
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:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user