core[patch]: Add default None to StructuredTool func (#26324)

This PR was autogenerated using gritql, tests written manually

```shell

grit apply 'class_definition(name=$C, $body, superclasses=$S) where {    
    $C <: ! "Config", // Does not work in this scope, but works after class_definition
    $body <: block($statements),
    $statements <: some bubble assignment(left=$x, right=$y, type=$t) as $A where {
        or {
            $y <: `Field($z)`,
            $x <: "model_config"
        }
    },
    // And has either Any or Optional fields without a default
    $statements <: some bubble assignment(left=$x, right=$y, type=$t) as $A where {
        $t <: or {
            r"Optional.*",
            r"Any",
            r"Union[None, .*]",
            r"Union[.*, None, .*]",
            r"Union[.*, None]",
        },
        $y <: ., // Match empty node        
        $t => `$t = None`,
    },    
}
' --language python .

```
This commit is contained in:
Eugene Yurtsev
2024-09-11 11:12:52 -04:00
committed by GitHub
parent edcd348ce7
commit c417bbc313
2 changed files with 19 additions and 4 deletions

View File

@@ -54,7 +54,6 @@ from langchain_core.tools.base import (
)
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_core.utils.pydantic import PYDANTIC_MAJOR_VERSION, _create_subset_model
from langchain_core.utils.pydantic import TypeBaseModel as TypeBaseModel
from tests.unit_tests.fake.callbacks import FakeCallbackHandler
from tests.unit_tests.pydantic_utils import _schema
@@ -1978,3 +1977,19 @@ def test_imports() -> None:
]
for module_name in expected_all:
assert hasattr(tools, module_name) and getattr(tools, module_name) is not None
def test_structured_tool_direct_init() -> None:
def foo(bar: str) -> str:
return bar
async def asyncFoo(bar: str) -> str:
return bar
class fooSchema(BaseModel):
bar: str = Field(..., description="The bar")
tool = StructuredTool(name="foo", args_schema=fooSchema, coroutine=asyncFoo)
with pytest.raises(NotImplementedError):
assert tool.invoke("hello") == "hello"